Jump to content

Sql 2005 Insert & Update Triggers


DaNuGai
 Share

Recommended Posts

I've used the code below for a MySQL database and it worked fine. However, now I am using an SQL 2005 database and I am trying to replicate the code below but I am having trouble converting the triggers. I understand that I can't use TIMESTAMP and instead I have to go with DATETIME data type but how do you write the same trigger for SQL 2005 database so it functions similar to the way it did in MySQL database.Thanks in advance.

CREATE TABLE EVENT (   EventID INT UNSIGNED NOT NULL AUTO_INCREMENT,   DateFrom DATE NOT NULL,   DateTo DATE NOT NULL,   EventTitle VARCHAR(50) NOT NULL,   Event TEXT NOT NULL,   Created TIMESTAMP DEFAULT '0000-00-00 00:00:00',   Updated TIMESTAMP DEFAULT '0000-00-00 00:00:00',   PRIMARY KEY(EventID));CREATE TRIGGER EVENT_TIME_CREATED   BEFORE INSERT ON EVENT   FOR EACH ROW SET NEW.Created = NOW(),    NEW.Updated = '0000-00-00 00:00:00';CREATE TRIGGER EVENT_TIME_UPDATED    BEFORE UPDATE ON EVENT   FOR EACH ROW SET NEW.Updated = NOW(),    NEW.Created = OLD.Created;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...