我在更新触发器后创建,它从子节点创建Parent。
因此,在更新后,如果联系人不存在帐户,则触发器将创建帐户,其名称为“firstname + lastname + Household”。对于连接,我在存储contact_ID的帐户上使用了额外的列。插入后,我在联系表上运行更新,我将创建的帐户ID放入适当的列。有没有办法避免使用额外的列?我如何获得account_id?
BEGIN
if not exists (select * from Account as A
join inserted as I on a.account_ID=i.account_fk where a.deleted=0)
begin
insert into account
(
Auto_Create_ID
,Name
)
select
i.contact_ID AS OLD_ID**strong text**
,isnull(i.firstname,'') + ' ' + i.lastname + ' Household' AS Name
from inserted AS I
where isnull(i.lastname,'')<>''
update C
set C.Account_FK=a.Account_ID
from Contact AS C
join Account AS A on C.Contact_ID=a.Auto_Create_ID
where a.Deleted=0 and c.Contact_ID in
(select Contact_ID from inserted)
end
谢谢
答案 0 :(得分:0)
如果您对IDENTITY
使用Auto_Create_ID
列,则在插入语句之后,您可以使用SCOPE_IDENTITY()
获取新插入行的键。