【问题标题】:Using Table Variables with xp_cmdshell将表变量与 xp_cmdshell 一起使用
【发布时间】:2013-06-09 20:51:57
【问题描述】:

我正在尝试使用连接多个表中的多个值的查询来创建平面文本文件。这个结果集被插入到一个表变量中,我想在 xp_cmdshell 调用中使用它来创建文本文件。这是我的代码示例。

DECLARE @tablevar table (string nvarchar(200))

INSERT INTO @tablevar
SELECT 'test' + column1 + column2
FROM SampleTable

EXEC xp_cmdshell 'bcp "SELECT * FROM @tablevar" queryout "C:\temp\output.txt" -T -c '

我在调用 xp_cmdshell 时收到以下错误:

SQLState = 42000,NativeError = 1087 错误 = [Microsoft][SQL Native Client][SQL Server]必须声明表变量“@outputtable”。 SQLState = 42000,NativeError = 8180 错误 = [Microsoft][SQL Native Client][SQL Server]无法准备语句。 空

当尝试使用临时表时,请使用此代码

EXEC xp_cmdshell 'bcp "Select * From #temp" queryout "C:\temp\outputtable.txt" -T -c '

我收到此错误消息

SQLState = 42S02,NativeError = 208 错误 = [Microsoft][SQL Native Client][SQL Server]无效的对象名称“tempdb.temp”。 SQLState = 42000,NativeError = 8180 错误 = [Microsoft][SQL Native Client][SQL Server]无法准备语句。

是否无法在 xp_cmdshell 中使用临时表或表变量?

【问题讨论】:

    标签: sql


    【解决方案1】:

    您的@table 变量和#temp 表都超出了其他连接的范围,但像##my_global_temp 这样的全局临时表应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-05
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2014-07-18
      • 2021-01-08
      • 2020-08-30
      相关资源
      最近更新 更多