SQL Server从XML路径中删除尾随逗号

时间:2017-12-27 15:13:56

标签: sql-server sql-server-2016 insert-into identity-insert

我有以下代码,用于在我的给定表中提取我的列:

SELECT 
   column_name + ','
FROM   
   information_schema.columns 
WHERE  
   table_name = 'maintReq'
FOR XML PATH('')

我想把它放到我当前的查询中:

SET IDENTITY_INSERT maintReq ON; 
GO

INSERT INTO maintReq  
OUTPUT Inserted.ID 
VALUES ((SELECT ISNULL(MAX(id) + 1, 0) 
         FROM maintReq WITH(SERIALIZABLE, UPDLOCK)                   
        ),'MAYBE', 'true'); 

SET IDENTITY_INSERT maintReq OFF;

我自己试图做以下事情:

SET IDENTITY_INSERT maintReq ON; 
GO
INSERT INTO maintReq (
                      SELECT 
                         column_name + ','
                      FROM   
                         information_schema.columns 
                      WHERE  
                         table_name = 'maintReq'
                      for 
                         xml path('')
                     )
   OUTPUT Inserted.ID 
      VALUES (
              (
               SELECT                                       
                  ISNULL(MAX(id)+1,0) 
               FROM     
                  maintReq WITH(SERIALIZABLE, UPDLOCK)                   
              ),'MAYBE', 'true'
             ); 
SET IDENTITY_INSERT maintReq OFF;

但是我得到的错误是:

  

Msg 156,Level 15,State 1,Line 4
  关键字' SELECT'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 8
  “#”;'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 16
  ','附近的语法不正确。

不确定添加到XML路径输出的额外逗号是否会调用该错误,或​​者是否有其他错误?

我的完整存储过程如下所示:

DECLARE @SQLQuery VARCHAR(MAX);
    SET @SQLQuery = 'SET IDENTITY_INSERT ' + @val1 + ' ON 
                     INSERT INTO ' +
                        @val1 + ' 
                        OUTPUT Inserted.ID 
                     VALUES ' + 
                        '(
                            (
                                SELECT 
                                    ISNULL(MAX(id)+1,0) 
                                FROM 
                                    ' + @val1 + ' WITH(SERIALIZABLE, UPDLOCK)
                            ),''' + @val2 + ''', ''' + @val3 + '''
                        ) ' +
                     'SET IDENTITY_INSERT ' + @val1 + ' OFF;'
    EXEC [dbo].[_chkQ] @SQLQuery 

以上SP是我目前收到此错误的内容:

  

表' maintReq'中的标识列的显式值只可以   在使用列列表且IDENTITY_INSERT为ON时指定。

感谢@Pரதீப்,这是最终的工作查询代码:

SET @SQLQuery = 'SET IDENTITY_INSERT ' + @val1 + ' ON 
                     INSERT INTO ' + @val1 + '(' + 
                        Stuff(
                               (SELECT 
                                    ',' + quotename(column_name) 
                                FROM  
                                    information_schema.columns 
                                WHERE 
                                    table_name = '' + @val1 + '' 
                                FOR xml path('')
                               ), 1, 1, ''
                             ) + 
                     ')
                        OUTPUT Inserted.ID 
                     VALUES ' + 
                        '(
                            (
                                SELECT 
                                    ISNULL(MAX(id)+1,0) 
                                FROM 
                                    ' + @val1 + ' WITH(SERIALIZABLE, UPDLOCK)
                            ),''' + @val2 + ''', ''' + @val3 + '''
                        ) ' +
                     'SET IDENTITY_INSERT ' + @val1 + ' OFF;'

1 个答案:

答案 0 :(得分:2)

您需要使用动态sql

DECLARE @col_list VARCHAR(8000)= ''

SET @col_list = Stuff((SELECT ',' + quotename(column_name) --"quotename" is to escape illegal characters
                       FROM   information_schema.columns
                       WHERE  table_name = 'maintReq'
                       FOR xml path('')), 1, 1, '')
SET IDENTITY_INSERT maintReq ON;

EXEC ('
INSERT INTO maintReq ('+@col_list+')
   OUTPUT Inserted.ID 
      VALUES (
              (SELECT                                       
                  ISNULL(MAX(id)+1,0) 
               FROM     
                  maintReq WITH(SERIALIZABLE, UPDLOCK)                   
              ),''MAYBE'', ''true''
             ); ')

SET IDENTITY_INSERT maintReq OFF;