sql server使用xp_cmdshell动态创建文件夹

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

创建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_c

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值