有人可以告诉我为什么这个插入失败但是没有给我一个错误?我该如何解决这个问题?
merge table1 as T1
using(select p.1,p.2,p.3,p.4,p.5 from @parameters p
inner join table1 t2
on p.1 = t2.1
and p.2 = t2.2
and p.3 = t2.3
and p.4 = t2.4) as SRC on SRC.2 = T1.2
when not matched then insert (p.1,p.2,p.3,p.4,p.5)
values (SRC.1,SRC.2,SRC.3,SRC.4,SRC.5)
when matched then update set t1.5 = SRC.5;
T1表当前为空,因此无法匹配。参数表中包含数据。我只需要修改这个合并,以便在决定做什么之前检查所有4个字段。
答案 0 :(得分:0)
您无法从变量from @parameters
请参阅以下帖子:Using a variable for table name in 'From' clause in SQL Server 2008
答案 1 :(得分:0)
实际上,您可以使用变量表。看看:
MERGE Target_table AS [Target]
USING @parameters AS [Source]
ON (
[Target].col1 = [Source].col1
AND [Target].col2 = [Source].col2
AND [Target].col3 = [Source].col3
AND [Target].col4 = [Source].col4
)
WHEN NOT MATCHED BY TARGET
THEN INSERT (col1,col2,col3,col4,col5)
VALUES (
[Source].col1
,[Source].col2
,[Source].col3
,[Source].col4
,[Source].col5
)
WHEN MATCHED
THEN UPDATE SET [Target].col5 = [Source].col5;