我一直在网上搜索一下,似乎从XP_CMDSHELL获取结果的唯一方法是将它们存储到临时表中.真的没有更简单的方法吗?
来自专家交流:
不,xp_cmdshell不会从exe返回任何信息.如果您不在master数据库中运行它,则必须使用以下语法.master..xp_cmdshell.您必须授予您的用户在master数据库中执行此过程的权限.您必须让您的exe自己插入信息,因为它无法将信息返回给调用它的进程.
和...
虽然@result只从xp_cmdshell获取返回值,但您可以通过直接插入表中来捕获命令的结果......如下所示:
因人而异...
set nocount on
declare @filepath varchar(255),
@cmd varchar(255),
@rc int
select @filepath = 'c:\temp\'
select @cmd = 'dir ' + @filepath + '~*.tmp'
create table #output (output varchar(255) null)
insert #output exec @rc = master..xp_cmdshell @cmd
select * from #output where output is not null
drop table #output
Run Code Online (Sandbox Code Playgroud)