']' 附近的语法不正确.? [英] Incorrect syntax near ']'.?

查看:41
本文介绍了']' 附近的语法不正确.?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对我的 sql 进行了一些调试,但我无法弄清楚我遇到的令人抓狂的错误我已将其缩小到几行我看不出问题所在,请有人给我一些帮助.

I have done some debugging on my sql and I cant figure out the maddening error I am getting I have narrowed it down to a couple of lines which I cant see what the problem is, please someone give me some assistance.

我收到此错误

我在这里2消息 102,级别 15,状态 1,第 1 行附近的语法不正确']'.我在这里3

I am here2 Msg 102, Level 15, State 1, Line 1 Incorrect syntax near ']'. I am here3

 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'

推荐答案

此处缺少一个左方括号:

You're missing an opening square bracket here:

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

此外,我建议您改用QuoteName() 函数 代替:

Furthermore I would recommend you switch to using the QuoteName() function instead:

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

这篇关于']' 附近的语法不正确.?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆