我有三个表OldTable
,NewTable
和MainTable
。我制作了一个触发器,将旧值放在OldTable
中,将新值放在Newtable
中。
我只需要更改数据。
MainTable
EID Ename EAddress Emobile
1 mittal pardi 9876543212
2 vaibhav Porbandar 7456543212
OLD TABLE
ID EID Ename EAddress Emobile CurrentDate
1 2 vaibhav pardi 7456543212 2017-12-21 11:00:23.437
新表
ID EID Ename EAddress Emobile CurrentDate
1 2 vaibhav Porbandar 7456543212 2017-12-21 11:00:23.437
我需要什么:
EID OldValue NewValue
2 pardi Porbandar
可以有多个新旧价值!
我的触发器代码:
ALTER TRIGGER [dbo].[trg_TEST_Upd]
ON [dbo].[MAIN_TABLE]
FOR UPDATE
AS
begin
SELECT * FROM INSERTED -- show data in INSERTED logical table
SELECT * FROM DELETED -- show data in DELETED logical table
insert into New_TABLE(EID,Ename,EAddress,Emobile,CurrentDate)
select EID,Ename,EAddress,Emobile,getdate() from INSERTED
insert into OLD_TABLE(EID,Ename,EAddress,Emobile,CurrentDate)
select EID,Ename,EAddress,Emobile,getdate() from DELETED
End
答案 0 :(得分:0)
我不明白为什么你需要在触发器中选择这个数据?!
但如果您需要将其插入其他地方,那么您可以这样做:
SELECT
i.EID,
MAX(i.Ename) AS newNameValue,
MAX(d.Ename) AS oldNameValue,
MAX(i.EAddress) AS newEAddressValue,
MAX(d.EAddress) AS oldEAddressValue
FROM INSERTED AS i
INNER JOIN DELETED AS d ON i.EID = d.EID
GROUP BY i.EID