创建sql server存储过程动态创建文件夹,文件夹名根据创建时间决定
create procedure sp_createdir
@dir nvarchar(4000),
as
begin
declare @cmd nvarchar(4000)
declare @now datetime
set @now = getdate()
set @dir = @dir + '\' +replace(replace(replace(convert(varchar, @now, 120), '-',''), ' ', ''),':', '')
set @cmd = 'mkdir ' + @dir
exec sp_configure 'show advanced options', 1 --允许配置高级选项
reconfigure --重新配置
exec sp_configure 'xp_cmdshell', 1 --启用xp_cmdshell
reconfigure --重新配置
exec xp_cmdshell @cmd
exec sp_configure 'xp_cmdshell', 0 --执行完成后出于安全考虑可以将xp_cmdshell关闭
end
xp_cmdshell语法
xp_cmdshell {'command_string'} [, no_output]

本文介绍了如何在SQL Server中利用存储过程和xp_cmdshell扩展存储过程动态创建文件夹,文件夹名基于创建时间。讨论了xp_cmdshell的参数使用,特别是'command_string'的限制,提醒在处理包含空格的路径时,应正确使用双引号,并且变量@cmd的类型需限制在nvarchar(4000)以内,以避免执行错误。

3956

被折叠的 条评论
为什么被折叠?



