使用merge,报错信息是There is an incorrect syntax near the keyword 'into'
Use merge,the error message is There is an incorrect syntax near the keyword 'into'
我这样使用合并:
BEGIN TRANSACTION
merge into TD_1 tar
using (select Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified from TSupply_2 ) source
on (tar.SupplyTitle=source.SupplyTitle and tar.EmployeeCode=source.EmployeeCode)
when matched then update set tar.modified=getdate()
when not matched then
insert (Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified)
values(source.Title,source.AnnouncementID,source.SupplyTitle,source.EmployeeCode,getdate(),getdate());
COMMIT TRANSACTION
但是错误是:
There is an incorrect syntax near the keyword 'into'.
There is an incorrect syntax near 'source'.
我该如何解决?
合并语法:
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_matched> ];
但是你正在使用 into
:
merge into TD_1 tar
另外source
是SQL的保留字。如果你愿意,你可以更换它。
试着看看这个tutorial。
现在我知道哪里错了,COMPATIBILITY_LEVEL
在我的数据库中是80。
所以我不能使用 merge
.
我的数据库版本是Microsoft SQL Server 2008
然后我修改了COMPATIBILITY_LEVEL
:
ALTER DATABASE DB1
SET COMPATIBILITY_LEVEL = 100
然后就没有报错了
我这样使用合并:
BEGIN TRANSACTION
merge into TD_1 tar
using (select Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified from TSupply_2 ) source
on (tar.SupplyTitle=source.SupplyTitle and tar.EmployeeCode=source.EmployeeCode)
when matched then update set tar.modified=getdate()
when not matched then
insert (Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified)
values(source.Title,source.AnnouncementID,source.SupplyTitle,source.EmployeeCode,getdate(),getdate());
COMMIT TRANSACTION
但是错误是:
There is an incorrect syntax near the keyword 'into'.
There is an incorrect syntax near 'source'.
我该如何解决?
合并语法:
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_matched> ];
但是你正在使用 into
:
merge into TD_1 tar
另外source
是SQL的保留字。如果你愿意,你可以更换它。
试着看看这个tutorial。
现在我知道哪里错了,COMPATIBILITY_LEVEL
在我的数据库中是80。
所以我不能使用 merge
.
我的数据库版本是Microsoft SQL Server 2008
然后我修改了COMPATIBILITY_LEVEL
:
ALTER DATABASE DB1
SET COMPATIBILITY_LEVEL = 100
然后就没有报错了