我有一个很好用的查询:
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'
但我认为我放错了单引号和/或双引号。我正在添加此选择语句,以便我的结果将字段名称作为第一行。
非常感谢任何帮助或指导。