Get Results from XP_CMDSHELL

后端 未结 2 1605
小鲜肉
小鲜肉 2020-12-16 13:23

I have been searching the web some and it seems like the only way to get the results from XP_CMDSHELL is to store them into a temp table. Is there really no easier way?

2条回答
  •  攒了一身酷
    2020-12-16 13:58

    There is no easier way to capture STDOUT/STDERR feedback from xp_cmdshell; there is at least one alternative but it couldn't be classed as easier:
    It would be possible to redirect the output of the command to a text file as part of the command, then read the text file using OPENROWSET.

    BTW there is at least one error in the script posted above. The docs for xp_cmdshell state that it returns command output as nvarchar(255).
    Also, the temp table ought to have an identity column, otherwise the results may not be displayed in the correct order:

    ...
    create table #output (id int identity(1,1), output nvarchar(255) null)
    insert #output (output) exec @rc = master..xp_cmdshell @cmd
    select * from #output where output is not null order by id
    drop table #output
    

提交回复
热议问题