【发布时间】:2015-12-23 10:22:04
【问题描述】:
我有两张桌子:
目标表:Specialisation (id , name , description)
源表TempSpecialisation(id , name , description)
如果名称匹配,我想将TempSpe.description 复制到Specialisation.Description,或者在Specialisation 中插入一条包含所有列的新记录。
由于Specialisation.name 末尾的空格,我得到了重复的条目。
我的程序是:
USE [TempDatabase]
GO
/****** Object: StoredProcedure [dbo].[TempDatabase2] Script Date: 23/12/2015 3:46:49 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TempDatabase2]
WITH EXECUTE AS owner
as
BEGIN
MERGE Specialisation as T
USING TempSpecialisation as S
ON s.Name = T.Name
WHEN NOT MATCHED BY Target
THEN INSERT(id ,Name, Description1) VALUES(S.id ,S.Name, S.Description1)
WHEN MATCHED and t.name =s.name
THEN UPDATE SET T.Description1 = S.Description1
OUTPUT $action, inserted.*;
End
表专业化
id Name Descriptions
545454 "Allergist " null
Table TEMp专业化
id Name Descriptions
1 "Allergist" This is a doctor who helps with allergies.
我需要从 TEMpSpecialisation 更新专业化描述。 但它会给出像
这样的输出id Name Descriptions
1 "Allergist" This is a doctor who helps with allergies.
545454 "Allergist " null
【问题讨论】:
-
id列 - 身份? -
如果此行执行正确的更新操作 THEN UPDATE SET T.Description1 = S.Description1 我认为这应该是 THEN UPDATE SET S.Description1 =T.Description1
标签: sql sql-server trim