创建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]
参数
'command_string'
是在操作系统命令行解释器上执行的命令字符串。command_string 的数据类型为 varchar(255) 或 nvarchar(4000),没有默认值。command_string 不能包含一对以上的双引号。如果由 command_string 引用的文件路径或程序名称中有空格,则需要使用一对引号。如果使用嵌入空格不方便,可考虑使用 FAT 8.3 文件名作为解决办法。
no_output
是可选参数,表示执行给定的 command_string,但不向客户端返回任何输出。
返回代码值
0(成功)或 1(失败)
结果集
执行下列 xp_cmdshell 语句将返回当前目录的目录列表。
xp_cmdshell 'dir *.exe'
行以 nvarchar(255) 列的形式返回。
执行下列 xp_cmdshell 语句将返回随后的结果集:
xp_cmdshell 'dir *.exe', NO_OUTPUT
由于没有了解xp_cmdshell的参数定义,将@cmd参数设置为nvarchar(max),导致执行时出现错误“过程需要类型为'varchar'的参数'command_string'”。需要将@cmd定义为长度小于或等于4000的nvarchar类型变量。
文章来源:http://www.itnose.net/detail/6023571.html
更多文章:http://www.itnose.net/type/96.html
相关推荐
`xp_cmdshell`是一个系统扩展存储过程,允许SQL Server执行操作系统命令。 ### 具体步骤详解 #### 格式文件创建 ```sql -- 创建格式文件 select top 1 @fmtfile = rtrim(reverse(filename)) from master.dbo....
此步骤使用`XP_CMDSHELL`扩展存储过程来执行Windows命令行操作,具体是映射网络驱动器以便后续的备份文件可以直接写入远程服务器上的指定文件夹。 ### 三、执行数据库备份 ```sql BACKUP DATABASE MASTER TO DISK ...
EXEC MASTER..XP_CMD_SHELL @cmd1; -- 删除10天前的备份文件 SET @i = @i + 1; END; -- 创建备份文件存放目录 EXEC MASTER..XP_CMD_SHELL 'IF NOT EXISTS (E:\DATABACKUP) MD E:\DATABACKUP'; -- 执行备份...
CMD:Windows NT,OS/2的命令文件;DOS CD/M命令文件;dBASEⅡ程序文件 CPI:Microsoft MS-DOS代码页信息文件 CPL:控制面板扩展名,Corel颜色板 CPP:C++代码文件 CPR:Corel提供说明书文件 CPT:Corel 照片-...