【发布时间】:2019-07-06 18:24:26
【问题描述】:
我想比较@tempTable 和tableA,如果匹配则更新@tempTable 的FieldValue 等于tableA 的id;如果不匹配则插入值,并获取tableA的id来更新@tempTable的FieldValue。
以下是我的 SQL 查询:
create table [dbo].[TableA]([Id] int Indentity(1,1),[Data] [sql_variant] NULL)
declare @tempTable table (FieldValue nvarchar(1000),FieldType nvarchar(1000))
insert @tempTable values ('some content','A Type')
merge
tableA
using (
select
FieldValue
from
@tempTable
) x
on tableA.[Data] = x.FieldValue
when not matched then
insert values (x.FieldValue)
when matched then
update set x.FieldValue = tableA.[Id] ;
以下是错误信息:
无法绑定多部分 ID“x.FieldValue”。
这个错误看起来是x.FieldValue和tableA.id的数据类型不同,所以我把它们调整为相同的数据类型,但还是不行,不知道怎么解决。
【问题讨论】:
-
添加到 Schnugo 的响应中 - 您要更新的表是 tableA。您无法更新 x(即 @tempTable)中的列。
-
谢谢,我解决了我的问题并分开了写作。
标签: sql-server tsql sql-server-2008 merge sql-merge