How to create a trigger for on update timestamp in Teradata table?

不羁岁月 提交于 2019-12-11 12:52:52

问题


I am using Teradata 14.00

I have a table with columns (id, name,city,mytime) in Teradata 14.00.

If we update the name, the timestamp in 'mytime' column should be updated automatically, without having to specify it in the Update statement.

How to use a trigger for this.

Any suggestions.
Thanks in advance


回答1:


Here is a very good article http://teradatafaqs.blogspot.in/p/tutorial.html for studying triggers.

For your ease here is the step by step creating of the trigger.

Sample table

Temp Table for your refernce :-
drop table DB_SOK.EMPLOYEE;
CREATE MULTISET TABLE DB_SOK.EMPLOYEE
( Employeeid INTEGER,
Employeename CHAR(30),
Salary INTEGER,
city varchar(100),
mytime timestamp(0));

insert into DB_SOK.EMPLOYEE values(1,'happy',100,'NYC',current_timestamp(0));

Select * from DB_SOK.EMPLOYEE;

Employeeid  Employeename    Salary  city    mytime
1               1           happy    100    NYC 11/7/2015 08:51:05

Then Create a Trigger for update for each row.

Replace  TRIGGER DB_SOK.EmpUpdate
AFTER UPDATE OF (Employeename) ON DB_SOK.EMPLOYEE
REFERENCING OLD ROW as EMPLOYEE_old_row
 NEW row  as EMPLOYEE_new_row
 FOR EACH ROW
update DB_SOK.EMPLOYEE
set mytime=current_timestamp(0)

Fire Update:-

update DB_SOK.EMPLOYEE
set  Employeename ='Hello Happy'
where Employeeid=1;

Select Data again to see data is update or not;

Select * from DB_SOK.EMPLOYEE;

Employeeid  Employeename    Salary  city    mytime
1   1   Hello Happy                     100 NYC 11/7/2015 09:02:23


来源:https://stackoverflow.com/questions/33579648/how-to-create-a-trigger-for-on-update-timestamp-in-teradata-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!