简体   繁体   English

与查询中的引号和双引号混淆

[英]confusion with quotes and double quotes in a query

i have a query that works beautifully: 我有一个非常有效的查询:

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'

but i need to add this in there: 但我需要在那里添加:

select column_name 
from information_schema.columns
where table_name = @table
order by ordinal_position

so far i have: 到目前为止我有:

   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'

but i think i am missplacing the single quotes and/or double quotes. 但我想我错过了单引号和/或双引号。 i am adding this select statement so that my result has the field names as the first row. 我正在添加此select语句,以便我的结果将字段名称作为第一行。

thanks so much for any help or guidance. 非常感谢任何帮助或指导。

Perhaps this is what you want? 也许这就是你想要的? This assumes that (a) none of your column names have commas in them, and (b) the output of each column, when implicitly converted to a string, is okay. 这假定(a)您的列名中没有逗号,(b)每个列的输出在隐式转换为字符串时是可以的。

ALTER PROCEDURE dbo.BCP_Text_File
    @table    NVARCHAR(255),  
    @filename VARCHAR(100)  
AS
BEGIN
  SET NOCOUNT ON;

  IF OBJECT_ID(@table) IS NOT NULL
  BEGIN
    DECLARE 
        @sql NVARCHAR(MAX), 
        @cols NVARCHAR(MAX) = N'';

    SELECT @cols += ',' + name
      FROM sys.columns
      WHERE [object_id] = OBJECT_ID(@table)
      ORDER BY column_id;

    SELECT @cols = STUFF(@cols, 1, 1, '');

    SET @sql = N'EXEC master..xp_cmdshell ''bcp "SELECT ''''' 
        + REPLACE(@cols, ',', ''''',''''') + ''''' UNION ALL SELECT ' 
        + 'RTRIM(' + REPLACE(@cols, ',', '),RTRIM(') + ') FROM ' 
        + DB_NAME() + '..' + @table + '" queryout "' + @filename + '" -c''';  

    EXEC sp_executesql @sql;
  END
  ELSE
  BEGIN
    SELECT 'The table '+@table+' does not exist in the database';
  END
END
GO

But I have to agree with the advice you've gotten from others on this and other questions - this approach is very brittle. 但是我必须同意你从这些问题和其他问题上得到的建议 - 这种方法非常脆弱。 You're trying to crack open a pistachio with a steamroller. 你正试图用压路机打开一个开心果。

PS I removed references to INFORMATION_SCHEMA , because I think the catalog views are more reliable and more consistent . PS我删除了对INFORMATION_SCHEMA引用,因为我认为目录视图更可靠,更一致

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM