服务器消息2812,navy887 exec xp_cmdshell 服务器: 消息 2812,级别 16,状态 62,行 1 未能找到存储过程 'xp_cmdshell'。解决办法...

SQL codeA. 返回可执行文件列表

下例显示执行目录命令的 xp_cmdshell 扩展存储过程。

EXEC master..xp_cmdshell 'dir *.exe'

B. 使用 Windows NT net 命令

下例显示 xp_cmdshell 在存储过程中的使用。下例先用 net send 通知用户 SQL Server 即将关闭,然后用 net pause 暂停服务器,最后用 net stop 关闭服务器。

CREATE PROC shutdown10

AS

EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down

in 10 minutes. No more connections allowed.', no_output

EXEC xp_cmdshell 'net pause sqlserver'

WAITFOR DELAY '00:05:00'

EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down

in 5 minutes.', no_output

WAITFOR DELAY '00:04:00'

EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down

in 1 minute. Log off now.', no_output

WAITFOR DELAY '00:01:00'

EXEC xp_cmdshell 'net stop sqlserver', no_output

C. 不返回输出

下例使用 xp_cmdshell 执行命令字符串,且不向客户端返回输出。

USE master

EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',

NO_OUTPUT

D. 使用返回状态

在下例中,xp_cmdshell 扩展存储过程也给出了返回状态。返回代码值存储在变量 @result 中。

DECLARE @result int

EXEC @result = xp_cmdshell 'dir *.exe'

IF (@result = 0)

PRINT 'Success'

ELSE

PRINT 'Failure'

E. 将变量内容写入文件

下例将当前目录内容写入当前服务器目录下名为 dir_out.txt 的文件中。

DECLARE @cmd sysname, @var sysname

SET @var = 'dir /p'

SET @cmd = 'echo ' + @var + ' > dir_out.txt'

EXEC master..xp_cmdshell @cmd

来帖一次