']'附近的语法不正确。?

时间:2013-11-01 14:58:07

标签: sql sql-server tsql syntax-error ssms

我已经对我的sql做了一些调试,我无法弄清楚我得到的令人抓狂的错误我把它缩小到几行,我不知道问题是什么,请有人给我一些帮助。

我收到此错误

我在这里2 Msg 102,Level 15,State 1,Line 1 语法不正确  ']'。 我在这里3

 Print 'I am here2'
                        SET IDENTITY_INSERT c365online_script1.dbo.tCompany ON
                        declare @cols2 varchar(max)
                        select @cols2 = (Select Stuff((Select '],[' + C.COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS As C Where C.TABLE_SCHEMA = T.TABLE_SCHEMA And C.TABLE_NAME = T.TABLE_NAME Order By C.ORDINAL_POSITION For Xml Path('')), 1, 2, '') As Columns From INFORMATION_SCHEMA.TABLES As T WHERE T.TABLE_NAME = @tablename)
                        EXEC('INSERT INTO [' + @Destination_Database_Name + '].[dbo].[' + @tablename + '] (' + @cols2 + ']' + ') SELECT ' + @cols2 + ']' + ' FROM [' + @Source_Database_Name + '].[dbo].[' + @tablename + ']');
                         Print 'I am here3'

3 个答案:

答案 0 :(得分:7)

你在这里错过了一个方括号:

... ') SELECT ' + @cols2 + ']' + ' FROM ...

此外,我建议您切换到使用the QuoteName() function代替:

... ') SELECT ' + QuoteName(@cols2) + ' FROM...

答案 1 :(得分:0)

据我所知,你错过了3个左括号

select @cols2 = (Select Stuff((Select '],[ 

 (' + @cols2 + ']' // -> either [' + @cols2 + ']' or (' + @cols2 + ')'

 + ') SELECT ' + @cols2 + ']'  -- -> either ') SELECT [' + @cols2 + ']'  or ') SELECT (' + @cols2 + ')' 

答案 2 :(得分:0)

declare @cols2 nvarchar(max)
select @cols2 = (Select Stuff((Select ',[' + C.COLUMN_NAME  + ']'
                From INFORMATION_SCHEMA.COLUMNS As C 
                Where C.TABLE_SCHEMA = T.TABLE_SCHEMA 
                And C.TABLE_NAME = T.TABLE_NAME 
                Order By C.ORDINAL_POSITION For Xml Path('')), 1, 1, '') As Columns 
                From INFORMATION_SCHEMA.TABLES As T 
                WHERE T.TABLE_NAME = @tablename)

修改

DECLARE @Destination_Database_Name NVARCHAR(128) = 'Trg_DataBaseName'
DECLARE @tablename NVARCHAR(128) = 'AgressoIFCGLItems'
DECLARE   @Source_Database_Name NVARCHAR(128) = 'Configsandpit'

Print 'I am here2'

--SET IDENTITY_INSERT c365online_script1.dbo.tCompany ON
declare @cols2 varchar(max)
select @cols2 = (Select Stuff((Select  ',' + QUOTENAME(C.COLUMN_NAME)  [text()]
                From INFORMATION_SCHEMA.COLUMNS As C 
                Where C.TABLE_SCHEMA = T.TABLE_SCHEMA 
                And C.TABLE_NAME = T.TABLE_NAME 
                Order By C.ORDINAL_POSITION For Xml Path('')), 1, 1, '') As Columns 
                From INFORMATION_SCHEMA.TABLES As T 
                WHERE T.TABLE_NAME = @tablename)
DECLARE @sql NVARCHAR(MAX) 
SET @sql = 'INSERT INTO ' + QUOTENAME(@Destination_Database_Name) + '.[dbo].' + QUOTENAME(@tablename) + ' (' + @cols2 +   ') SELECT ' + @cols2  + ' FROM ' + QUOTENAME(@Source_Database_Name) + '.[dbo].' + QUOTENAME(@tablename) + '';

EXECUTE sp_Executesql @sql 

在使用QOUTENAME函数后需要更改@Sql语句,因为它为您添加了方形子条,您不需要在Sql语句中合并方形条。

<强>结果

INSERT INTO [Trg_DataBaseName].[dbo].[TableName] ([Col1],[Col2],[Col3],[Col4],[Col5]) SELECT [Col1],[Col2],[Col3],[Col4],[Col5] FROM [Src_Database].[dbo].[TableName]