【发布时间】:2012-06-19 06:38:22
【问题描述】:
我有一个很好用的查询:
CREATE Procedure BCP_Text_File
(
@table varchar(100),
@FileName varchar(100)
)
AS
If exists(Select * from information_Schema.tables where table_name=@table)
Begin
Declare @str varchar(1000)
set @str='Exec Master..xp_Cmdshell ''bcp "Select * from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c'''
Exec(@str)
end
else
Select 'The table '+@table+' does not exist in the database'
但我需要在其中添加:
select column_name
from information_schema.columns
where table_name = @table
order by ordinal_position
到目前为止我有:
alter Procedure BCP_Text_File
(
@table varchar(100),
@FileName varchar(100)
)
AS
If exists(Select * from information_Schema.tables where table_name=@table)
Begin
Declare @str varchar(1000)
set @str='Exec Master..xp_Cmdshell ''bcp "
select column_name
from information_schema.columns
where table_name = '+db_name()+'..'+@table+'
order by ordinal_position
Select * from '+db_name()+'..'+@table+'" queryout "'+@FileName+'" -c'''
Exec(@str)
end
else
Select 'The table '+@table+' does not exist in the database'
但我认为我放错了单引号和/或双引号。我正在添加此选择语句,以便我的结果将字段名称作为第一行。
非常感谢您的帮助或指导。
【问题讨论】:
-
调试 101:将
EXEC(@str)更改为PRINT(@str) -
那些
'..'真的在查询中吗? -
@AaronBertrand 很抱歉,我无法更改它。我无法编译它
-
你实际上想用这个来完成什么?如果您将代码out 拉出存储过程,您可以更好地调试。然后编译不会阻止您查看问题所在。如果您启用了 IntelliSense,它们甚至可能使自己变得明显。
标签: sql sql-server sql-server-2008 tsql ssms