September 5, 2009 at 6:29 am
Please provide me sample codes for using trigger. And please provide me trigger interview questions.
Thanks in advance.
September 5, 2009 at 6:35 am
well, here's an example of a trigger...in this case, my trigger is keeping track of the last time a column was updated.
for trigger interview questions, I'm afraid I can't help you. If you are not familiar with the syntax and usage,,coaching questions will not get you over that; only experience will.
Practice with this trigger, use the search to find other forum posts on triggers, and most importantly, copy/paste/test the examples!
that's the only way to garner experience.
CREATE TABLE WHATEVER(
WHATEVERID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
DESCRIP VARCHAR(30)
)
INSERT INTO WHATEVER(DESCRIP)
SELECT 'APPLES' UNION
SELECT 'ORANGES' UNION
SELECT 'BANANAS' UNION
SELECT 'GRAPES' UNION
SELECT 'CHERRIES' UNION
SELECT 'KIWI'
ALTER TABLE WHATEVER ADD INSERTDT DATETIME DEFAULT GETDATE()WITH VALUES,
UPDATEDDT DATETIME DEFAULT GETDATE() WITH VALUES
SELECT * FROM WHATEVER
CREATE TRIGGER TR_WHATEVER
ON WHATEVER
FOR INSERT,UPDATE
AS
UPDATE WHATEVER
SET UPDATEDDT = GETDATE()
FROM INSERTED
WHERE WHATEVER.WHATEVERID=INSERTED.WHATEVERID
INSERT INTO WHATEVER(DESCRIP)
SELECT 'CANTALOUPE' UNION
SELECT 'TANGARINES' UNION
SELECT 'PLUMS' UNION
SELECT 'PEACHES' UNION
SELECT 'NECTARINES'
SELECT * FROM WHATEVER
UPDATE WHATEVER SET DESCRIP = DESCRIP + ' ' WHERE WHATEVERID IN (4,5)
SELECT * FROM WHATEVER
Lowell
September 5, 2009 at 10:26 am
You'll have to do some work. I might start here:
http://msdn.microsoft.com/en-us/library/aa214450%28SQL.80%29.aspx
September 6, 2009 at 7:55 am
Yes! all of us should do search in BOL before asking here/other places 😉 Books Online are really good, they have good examples (with code samples), and in many cases you can find ther in depth description).
On my notebook I have BOL to sql 2000/2005/2008.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply