SQL 服务器从 XML 路径中删除尾随逗号
SQL Server remove trailing comma from XML path
我有以下代码,可以在我的给定 table 中提取我的专栏名称:
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
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.
不确定该错误是由添加到 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就是我目前得到的这个错误:
An explicit value for the identity column in table 'maintReq' can only
be specified when a column list is used and IDENTITY_INSERT is 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;'
您需要使用动态 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;
我有以下代码,可以在我的给定 table 中提取我的专栏名称:
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
Incorrect syntax near the keyword 'SELECT'.Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.Msg 102, Level 15, State 1, Line 16
Incorrect syntax near ','.
不确定该错误是由添加到 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就是我目前得到的这个错误:
An explicit value for the identity column in table 'maintReq' can only be specified when a column list is used and IDENTITY_INSERT is 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;'
您需要使用动态 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;