我有以下存储过程,它以每个输入完整文件路径字符串的字节为单位返回文件大小。使用调用一个存储过程调用另一个TSQL更新表
我想编写另一个包装存储过程或函数来更新一个有两列的表。 Full_Path列(下面的存储过程的输入)和列大小(应由下面的过程的输出更新)。基本上我想使用下面的过程更新每个文件的大小列(在完整路径列中指定)。
我不知道该怎么做。请指教。
Create proc sp_get_file_size (@fileName varchar(200))
as
begin
declare @ntcmd varchar(200)
declare @detailLine varchar(200)
declare @pos1 int
declare @pos2 int
declare @size int
set nocount on
Create table #res (line varchar(400))
set @ntcmd = 'dir /-C ' + @fileName
insert #res exec xp_CmdShell @ntcmd
select @detailLine = line
from #res where rtrim(ltrim(line)) like '%bytes'
-- if detail Line is null - return -1
if isnull (@detailLine ,'*') = '*' return -1
-- get position of words bytes and File(s)
set @pos1 = charindex ('bytes' ,lower(@detailLine))
set @pos2 = charindex ('(s)' , lower(@detailLine))
-- extract the size value from the details Line
set @size = convert (int, rtrim(ltrim(
substring (@detailLine , @pos2+3,@pos1 - @pos2 - 4))))
return (@size)
set nocount off
end
go
注意:你不应该**为存储过程使用'sp_'前缀。微软已经保留了这个前缀以供自己使用(参见*命名存储过程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你将来有可能冒着名字冲突的风险。 [这对你的存储过程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是简单地避免使用'sp_'并将其他内容用作前缀 - 或者根本没有前缀! –