Last active
May 26, 2018 17:07
-
-
Save alevyinroc/bea50de2107f118431da6bb394e84d55 to your computer and use it in GitHub Desktop.
Demo for trigger vs. default values #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE Demo; | |
SET NOCOUNT ON; | |
SET STATISTICS TIME, IO OFF; | |
/* Just to prove there's no shenanigans */ | |
DBCC FREEPROCCACHE; | |
DBCC DROPCLEANBUFFERS; | |
GO | |
DROP TABLE IF EXISTS triggertest; | |
DROP TABLE IF EXISTS defaulttest; | |
CREATE TABLE triggertest | |
(id INT IDENTITY(1, 1), | |
Home INT NOT NULL | |
DEFAULT 0, | |
Away INT NOT NULL | |
DEFAULT 0, | |
LastModified DATETIMEOFFSET NULL | |
); | |
CREATE TABLE defaulttest | |
(id INT IDENTITY(1, 1), | |
Home INT NOT NULL | |
DEFAULT 0, | |
Away INT NOT NULL | |
DEFAULT 0, | |
LastModified DATETIMEOFFSET DEFAULT SYSDATETIMEOFFSET() | |
); | |
GO | |
CREATE OR ALTER TRIGGER tr_triggertest ON [triggertest] | |
AFTER INSERT, UPDATE | |
AS | |
UPDATE triggertest | |
SET | |
LastModified = SYSDATETIMEOFFSET() | |
WHERE [id] IN | |
( | |
SELECT [id] | |
FROM inserted | |
); | |
GO | |
SET STATISTICS TIME, IO ON; | |
INSERT INTO triggertest(Home,Away) | |
VALUES(17,21); | |
INSERT INTO defaulttest(Home,Away) | |
VALUES(17,21); | |
SET STATISTICS TIME, IO OFF; | |
select * from triggertest; | |
select * from defaulttest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment