我想知道为什么以下声明不起作用:
insert into #temp (ip, ping) values ('ip', exec xp_cmdshell 'ping ip')我希望得到结果集,在其中我将在一个列中拥有ip地址,并从该服务器进行ping。上面的查询返回错误:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'exec'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.谢谢你的解释。
发布于 2013-10-21 13:09:49
不能在INSERT语句值列表中执行任何操作。你需要这样执行:
insert into #temp
execute xp_cmdshell 'ping ip'这假设#temp表有一个与xp_cmdshell的结果集匹配的列结构。
还可以创建一个中间临时表,以包含存储过程返回的所有列,然后只将所需的列插入到最终表中。
发布于 2013-10-21 13:14:23
嗯,insert ... exec是出了名的不灵活。首先,它不适用于values子句。更令人烦恼的是,它不支持列列表。表中的列必须与存储过程的输出完全匹配。
使用insert ... exec的唯一方法是:
insert TableName exec(...)
-- ^^^--> no column list!下面是一个例子:
if exists (select * from tempdb.sys.tables where name like '#temp__%')
drop table #temp
create table #temp (output varchar(max))
insert into #temp execute xp_cmdshell 'ping pong'
select * from #temphttps://stackoverflow.com/questions/19495398
复制相似问题