Now let’s update the row and select the results. However, if the row is updated, the ModifiedDate‘s value should change. In this case, both the CreatedDate and ModifiedDate columns contain the same value. This is displayed using vertical output in order to make it easier to read (so that you don’t have to scroll sideways to read all columns). Let’s insert a row and select it to see the result of the DEFAULT constraint: INSERT INTO Books (BookName) WHERE BookId IN (SELECT DISTINCT BookId FROM inserted) Insert a Row ModifiedDate datetime DEFAULT CURRENT_TIMESTAMPĬREATE TRIGGER trg_Books_UpdateModifiedDate CREATE TABLE dbo.Books (īookId int IDENTITY(1,1) NOT NULL PRIMARY KEY,ĬreateDate datetime DEFAULT CURRENT_TIMESTAMP, The following code creates a table, as well as a trigger that updates the ModifiedDate column whenever there’s an update. This statement can be used to create a DML, DDL, or logon trigger. You can use the CREATE TRIGGER statement to create a trigger when using T-SQL. In SQL Server, you can use a trigger to perform this update.Ī trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. Each time the row is updated, the date is updated to reflect the date and time of that update. Some database tables include a “last modified” column, which stores the date and time that the row was last updated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |