1

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

我收到这个错误

我在这里2 消息 102,级别 15,状态 1,第 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'
4

3 回答 3

7

你在这里缺少一个方括号:

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

此外,我建议您改用QuoteName()功能

... ') SELECT ' + QuoteName(@cols2) + ' FROM...
于 2013-11-01T14:59:19.240 回答
0

据我所知,您缺少大约 3 个左括号

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

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

 + ') SELECT ' + @cols2 + ']'  -- -> either ') SELECT [' + @cols2 + ']'  or ') SELECT (' + @cols2 + ')' 
于 2013-11-01T15:15:05.500 回答
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]
于 2013-11-01T16:12:53.487 回答