[英]No column name was specifies for column 1 of 'src' Merge statement SQL server
I am using the MERGE statement in SQL server to refresh the data but I am repeatedly getting this error int the ON clause of the MERGE statement. 我在SQL Server中使用MERGE语句刷新数据,但是在MERGE语句的ON子句中反复出现此错误。 The code is
该代码是
DECLARE @instance varchar(50)
DECLARE @db varchar (50)
DECLARE @queryEntity nvarchar(max)
SET @instance = (select value from Parameter where name = 'SERVERALIAS')
SET @db = (select value from Parameter where name = 'SERVERDB')
SET @queryEntity = 'Select EntityId,EntityName,CreatedDate,ModifiedDate,Active,TENANTID,PriorityId From [' + @instance + '].[' + @db + '].metadata.Entity Where TENANTID = 1'
MERGE [metadata].[Entity] AS trgt
USING ( VALUES(@queryEntity) ) AS src
ON ( **trgt.EntityId = src.EntityId** )
WHEN matched
--AND trgt.ModifiedDate <= src.ModifiedDate
THEN
-- if the master has a row newer than the client
-- update the client
UPDATE SET trgt.EntityId = src.EntityId,
trgt.EntityName = src.EntityName,
trgt.Createddate = src.CreatedDate,
trgt.ModifiedDate = src.ModifiedDate,
trgt.Active = src.Active,
trgt.TENANTID = src.TENANTID,
trgt.PriorityId = src.PriorityId
WHEN NOT matched BY SOURCE
THEN
DELETE
WHEN NOT matched BY TARGET
THEN
INSERT ( EntityId, EntityName, CreatedDate, ModifiedDate, Active, TENANTID, PriorityId)
VALUES ( src.EntityId, src.EntityName, src.CreatedDate, src.ModifiedDate, src.Active, src.TENANTID, src.PriorityId);
You haven't specified a column name for src
-- the message is pretty clear. 您尚未为
src
指定列名-消息很清楚。 Try this: 尝试这个:
MERGE [metadata].[Entity] AS trgt
USING ( VALUES(@queryEntity) ) AS src(EntityId)
--------------------------------------^
I should point out that this is only the beginning. 我应该指出,这仅仅是开始。
src
also doesn't have a host of other columns, specified in the rest of the MERGE
. src
也没有在MERGE
的其余部分中指定的其他许多列。 In fact, it is merely a string. 实际上,它仅仅是一个字符串。
MERGE
doesn't execute a string just because it looks like a query. MERGE
不仅仅因为它看起来像查询就执行字符串。
You have three options. 您有三个选择。 The first is to dispense with the variable and put the query string in the
MERGE
. 第一种是省去变量,并将查询字符串放入
MERGE
。 But that doesn't seem possible because you have variable identifier names. 但这似乎不可能,因为您具有可变的标识符名称。
The second is to use dynamic SQL with the MERGE
. 第二种是将动态SQL与
MERGE
一起使用。
My recommendation, though, is to use dynamic SQL to create a view or to populate a table with a canonical name. 不过,我的建议是使用动态SQL创建视图或使用规范名称填充表。 Then use that for the
MERGE
statement. 然后将其用于
MERGE
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.