create table #tb(re varchar(8000))insert #tb EXEC xp_cmdshell 'dir *.exe'declare @i varchar(8000)declare cursor_insert cursor for select * from #tb open cursor_insert fetch cursor_insert into @i while @@fetch_status=0 begin print @i fetch cursor_insert into @i end close cursor_insert deallocate cursor_insert drop table #tb
但是如下语句@b得出结果不全。(我需要把结果付值给一变量,之后程序可对结果进行处理)那是因为你只取了第一条记录的数,改用游标逐条读取处理就行了. --创建一个临时表,用来保存执行结果: create table #tb(re varchar(8000)) --得到结果: insert #tb EXEC xp_cmdshell 'dir *.exe' --处理结果 declare @b varchar(8000) declare #pr cursor for select re from #tb --定义游标 open #pr --打开游标 fetch next from #pr into @b --从游标中取数进行处理 while @@fetch_status=0 begin print @b --此处换为你的处理语句 fetch next from #pr into @b --从游标中取数进行处理 end close #pr deallocate #pr --删除临时表 drop table #tb
create table #table(id int identity,txt varchar(1000)) insert into #table(txt) exec master..xp_cmdshell 'dir c:\*.' select * from #table
insert #a EXEC xp_cmdshell 'dir *.exe'
select * from #adrop table #a
ELSE
PRINT 'Failure'
是显示在消息里的你可以在前台程序中接收xp_cmdshell 的结果集
Insert #tmp exec XP_CmdShell 'Dir D:\'
Select * from #tmp
create table #tb(re varchar(8000))--得到结果:
insert #tb EXEC xp_cmdshell 'dir *.exe'--显示结果
select * from #tb--删除临时表
drop table #tb
create table #tb(re varchar(8000))
--得到结果:
insert #tb EXEC xp_cmdshell 'dir *.exe'
--显示结果
declare @b varchar(8000)
select @b=re from #tb
--删除临时表
print @b
drop table #tb
open cursor_insert
fetch cursor_insert into @i
while @@fetch_status=0
begin print @i fetch cursor_insert into @i
end
close cursor_insert
deallocate cursor_insert
drop table #tb
--创建一个临时表,用来保存执行结果:
create table #tb(re varchar(8000))
--得到结果:
insert #tb EXEC xp_cmdshell 'dir *.exe'
--处理结果
declare @b varchar(8000)
declare #pr cursor for select re from #tb --定义游标
open #pr --打开游标
fetch next from #pr into @b --从游标中取数进行处理
while @@fetch_status=0
begin
print @b --此处换为你的处理语句
fetch next from #pr into @b --从游标中取数进行处理
end
close #pr
deallocate #pr
--删除临时表
drop table #tb
insert into #table(txt) exec master..xp_cmdshell 'dir c:\*.'
select * from #table