1、存儲過程建立語句以下:
sql
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <moki> -- Create date: <20140610> -- Description: <獲取磁盤文件大小> -- @mode值爲0表示輸出覆蓋原有文件的內容 -- 值不爲0表示輸出不覆蓋原有文件內容 -- ============================================= CREATE PROCEDURE sp_GetFileSize -- Add the parameters for the stored procedure here @resourcefilepath VARCHAR(1024), @resultpath VARCHAR(1024), @mode INT=0 AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; --肯定xp_cmdshell是可用的 -- --------------------------------------------------------- -- EXEC master.dbo.sp_configure 'show advanced options', 1 -- GO -- RECONFIGURE -- GO -- EXEC master.dbo.sp_configure 'xp_cmdshell', 1 -- GO -- RECONFIGURE -- GO --------------------------------------------------------- DECLARE @bat VARCHAR(1024) DECLARE @result INT DECLARE @ret INT EXEC master.dbo.xp_fileexist @resourcefilepath,@result out SET @ret=0 IF @result=1 BEGIN IF @mode=0 BEGIN SET @bat='for /f %i in ('+'"'+@resourcefilepath+'"'+') do @echo %~ni%~xi:%~zi>'+@resultpath END ELSE BEGIN SET @bat='for /f %i in ('+'"'+@resourcefilepath+'"'+') do @echo %~ni%~xi:%~zi>>'+@resultpath END EXEC master.dbo.xp_cmdshell @bat SET @ret=0 END ELSE BEGIN PRINT @resourcefilepath+'不存在' SET @ret=-1 END RETURN @ret END GO
2、使用舉例以下:shell
declare @filepath varchar(1024) declare @resultpath varchar(1024) set @filepath='D:\useful\批處理製做wifi熱點\批處理製做wifi熱點.docx' set @resultpath='c:\sqlresult.txt' EXEC MASTER.DBO.sp_GetFileSize @filepath,@resultpath,0