Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
没有为SQL server的“src”合并语句的第1列指定列名_Sql_Sql Server - Fatal编程技术网

没有为SQL server的“src”合并语句的第1列指定列名

没有为SQL server的“src”合并语句的第1列指定列名,sql,sql-server,Sql,Sql Server,我在SQLServer中使用MERGE语句刷新数据,但在MERGE语句的ON子句中重复出现此错误。 代码是 DECLARE @instance varchar(50) DECLARE @db varchar (50) DECLARE @queryEntity nvarchar(max) SET @instance = (select value from Parameter where name = 'SERVERALIAS') SET @db = (s

我在SQLServer中使用MERGE语句刷新数据,但在MERGE语句的ON子句中重复出现此错误。 代码是

    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); 

您还没有为src指定列名-消息非常清楚。试试这个:

MERGE [metadata].[Entity] AS trgt
USING ( VALUES(@queryEntity) ) AS src(EntityId)
--------------------------------------^
我应该指出,这只是开始。src也没有在合并的其余部分中指定的其他列。事实上,它只是一根弦。MERGE不会仅仅因为字符串看起来像查询就执行它

你有三个选择。第一种方法是省去变量并将查询字符串放入合并中。但这似乎不可能,因为您有可变的标识符名称

第二种方法是在合并中使用动态SQL


不过,我的建议是使用动态SQL创建视图或使用规范名称填充表。然后将其用于MERGE语句。

您还没有为src指定列名-消息非常清楚。试试这个:

MERGE [metadata].[Entity] AS trgt
USING ( VALUES(@queryEntity) ) AS src(EntityId)
--------------------------------------^
我应该指出,这只是开始。src也没有在合并的其余部分中指定的其他列。事实上,它只是一根弦。MERGE不会仅仅因为字符串看起来像查询就执行它

你有三个选择。第一种方法是省去变量并将查询字符串放入合并中。但这似乎不可能,因为您有可变的标识符名称

第二种方法是在合并中使用动态SQL


不过,我的建议是使用动态SQL创建视图或使用规范名称填充表。然后将其用于MERGE语句。

我认为op希望他的@queryEntity执行。我认为op希望他的@queryEntity执行