加载中...
查看文章
  • xp_cmdshell
  • 作者:未知 来源:未知 添加时间:2006年7月3日 字体:
  • 84684

    以操作系统命令行解释器的方式执行给定的命令字符串,并以文本行方式返回任何输出。授予非管理用户执行 xp_cmdshell 的权限。


    t'x5


    t'x5

    说明  在 Microsoft® Windows® 98 操作系统中执行 xp_cmdshell 时,将不把 xp_cmdshell 的返回代码设置为唤醒调用的可执行文件的进程退出代码。返回代码始终为 0。


    t'x5


    t'x5
    语法

    t'x5

    xp_cmdshell {'command_string'} [, no_output]


    t'x5
    参数

    t'x5

    'command_string'


    t'x5

    是在操作系统命令行解释器上执行的命令字符串。command_string 的数据类型为 varchar(8000)nvarchar(4000),没有默认值。command_string 不能包含一对以上的双引号。如果由 command_string 引用的文件路径或程序名称中有空格,则需要使用一对引号。如果使用嵌入空格不方便,可考虑使用 FAT 8.3 文件名作为解决办法。


    t'x5

    no_output


    t'x5

    是可选参数,表示执行给定的 command_string,但不向客户端返回任何输出。


    t'x5
    返回代码值

    t'x5

    0(成功)或 1(失败)


    t'x5
    结果集

    t'x5

    执行下列 xp_cmdshell 语句将返回当前目录的目录列表。

    xp_cmdshell 'dir *.exe'
    
    t'x5

    t'x5

    行以 nvarchar(255) 列的形式返回。


    t'x5

    执行下列 xp_cmdshell 语句将返回随后的结果集:

    xp_cmdshell 'dir *.exe', NO_OUTPUT
    
    t'x5

    t'x5

    下面是结果:

    The command(s) completed successfully.
    
    t'x5

    t'x5
    注释

    t'x5

    xp_cmdshell 以同步方式操作。在命令行解释器命令执行完毕之前,不会返回控制。


    t'x5

    当授予用户执行权限时,用户能在 Microsoft Windows NT® 命令行解释器上执行运行 Microsoft SQL Server™ 的帐户有权执行的任何操作系统命令。


    t'x5

    默认情况下,只有 sysadmin 固定服务器角色的成员才能执行此扩展存储过程。但是,也可以授予其他用户执行此存储过程的权限。


    t'x5

    当作为 sysadmin 固定服务器角色成员的用户唤醒调用 xp_cmdshell 时,将在运行 SQL Server 服务的安全上下文中执行 xp_cmdshell。当用户不是 sysadmin 组的成员时,xp_cmdshell 将模拟使用 xp_sqlagent_proxy_account 指定的 SQL Server 代理程序的代理帐户。如果代理帐户不能用,则 xp_cmdshell 将失败。这只是针对于 Microsoft® Windows NT® 4.0 和 Windows 2000。在 Windows 9.x 上,没有模拟,且 xp_cmdshell 始终在启动 SQL Server 的 Windows 9.x 用户的安全上下文下执行。


    t'x5


    t'x5

    说明  在早期版本中,获得 xp_cmdshell 执行权限的用户在 MSSQLServer 服务的用户帐户上下文中运行命令。可以通过配置选项配置 SQL Server,以便对 SQL Server 无 sa 访问权限的用户能够在 SQLExecutiveCmdExec Windows NT 帐户的上下文中运行 xp_cmdshell。在 SQL Server 7.0 中,该帐户称为 SQLAgentCmdExec。现在,不是 sysadmin 固定服务器角色成员的用户将在该帐户上下文中运行命令,而无需再进行配置更改。


    t'x5


    t'x5
    权限

    t'x5

    xp_deletemail 的执行权限默认授予 sysadmin 固定服务器角色的成员,但可以授予其他用户。


    t'x5


    t'x5

    重要  如果为 MSSQLServer 服务选用的 Windows NT 帐户不是本地管理员组的成员,则非 sysadmin 固定服务器角色成员的用户将无法执行 xp_cmdshell


    t'x5


    t'x5
    示例

    t'x5
    A. 返回可执行文件列表

    t'x5

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

    EXEC master..xp_cmdshell 'dir *.exe'
    
    t'x5

    t'x5
    B. 使用 Windows NT net 命令

    t'x5

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

    CREATE PROC shutdown10
    
    t'x5AS
    t'x5EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
    t'x5 in 10 minutes. No more connections allowed.', no_output
    t'x5EXEC xp_cmdshell 'net pause sqlserver'
    t'x5WAITFOR DELAY '00:05:00'
    t'x5EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server shutting down
    t'x5 in 5 minutes.', no_output
    t'x5WAITFOR DELAY '00:04:00'
    t'x5EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server shutting down
    t'x5 in 1 minute. Log off now.', no_output
    t'x5WAITFOR DELAY '00:01:00'
    t'x5EXEC xp_cmdshell 'net stop sqlserver', no_output
    t'x5

    t'x5
    C. 不返回输出

    t'x5

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

    USE master
    
    t'x5EXEC xp_cmdshell 'copy c:\sqldumps\pubs.dmp \\server2\backups\sqldumps',
    t'x5 NO_OUTPUT
    t'x5

    t'x5
    D. 使用返回状态

    t'x5

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

    DECLARE @result int
    
    t'x5EXEC @result = xp_cmdshell 'dir *.exe'
    t'x5IF (@result = 0)
    t'x5 PRINT 'Success'
    t'x5ELSE
    t'x5 PRINT 'Failure'
    t'x5

    t'x5
    E. 将变量内容写入文件

    t'x5

    下例将 @var 变量的内容写入当前服务器目录下名为 var_out.txt 的文件中。

    DECLARE @cmd sysname, @var sysname
    
    t'x5SET @var = 'Hello world'
    t'x5SET @cmd = 'echo ' + @var + ' > var_out.txt'
    t'x5EXEC master..xp_cmdshell @cmd
    t'x5

    t'x5
    F. 将命令的结果捕获到文件

    t'x5

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

    DECLARE @cmd sysname, @var sysname
    
    t'x5SET @var = 'dir/p'
    t'x5SET @cmd = @var + ' > dir_out.txt'
    t'x5EXEC master..xp_cmdshell @cmd
    84681
  • 返回页面顶端
  • 上一篇:SQL Server中的模式匹配
  • 下一篇:SQL语法查询文档
  • 返回上一页 打印本文 加入收藏
  • 页面最后更新时间:2011年5月18日