all groups > sql server programming > october 2007 >
You're in the

sql server programming

group:

Trigger to update a value when a certian value is entered into the database



Trigger to update a value when a certian value is entered into the database Lord Kelvan
10/12/2007 5:09:10 PM
sql server programming: ok i cannot for the life of me remember how to do this nor can i find
what i am looking for

i am trying to update my database when a value is updated into the
database

in one table i have somevalues and a two other peices of information
resolved and dateresolved i want my trigger to update the date
resolved informaion using getdate() when a 1 is entered into the bit
field resolved

-- ================================================
-- Template generated from Template Explorer using:
-- Create Trigger (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- See additional Create Trigger templates for more
-- examples of different Trigger statements.
--
-- This block of comments will not be included in
-- the definition of the function.

=====================================================
CREATE TRIGGER dbo.addresolveddate
ON dbo.ISSUE
FOR UPDATE
AS
BEGIN
IF resolved = 1
BEGIN
update issue set dateresolved = getdate()
END

END
=====================================================

all of that works of course except the if command because it does not
kwow what resolved is at that time which under programming logics is
correct please help me get that to work so when the interface updates
that column with a 1 it runs said trigger

regards
Kelvan
Re: Trigger to update a value when a certian value is entered into the database Lord Kelvan
10/12/2007 5:50:58 PM
never heard of inserted ... thx i will test it
Re: Trigger to update a value when a certian value is entered into the database Andrew J. Kelly
10/12/2007 8:45:55 PM
You can not refer directly to a column unless it is in a DML statement. You
also must account for the fact more than 1 row at a time can be updated and
triggers only fire once per update. Try this:

update issue set dateresolved = getdate()
WHERE exists ( SELECT * FROM inserted where inserted.PK = issue.pk and
inserted.resolved = 1)


--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


[quoted text, click to view]
Re: Trigger to update a value when a certian value is entered into the database sloan
10/12/2007 11:22:11 PM

in addition to Andrew, I think you can check for the specfic column as well.

see
http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/af92ec70c1febf6b/6cf93cbe4776f30e?hl=en&lnk=st&q=inserted+Employee+trigger+sloan#6cf93cbe4776f30e
for the original post..

here is a sample, you could alter to fit your need.

Notice the "if update(HireDate)"

Notice (as Andrew mentioned) that the trigger runs ONCE per set. So if you
update 10 rows, the trigger runs once (this is the most common error I see)

And finally, Notice that the inserted table gets populated on INSERTS or
UPDATES ( if you define it as such with the "For Update, Insert" clause).



Here is a generic example. This runs for Employees, and the HireDate of
"01/01/2000". (Obviously a silly example, but you can figure out your own
needs based on the sample)
This assumes a dbo.Employee table with a EmployeeUUID (guid, primary key)
and HireDate (datetime) column.


CREATE TRIGGER trgExample1
ON dbo.Employee
FOR UPDATE , INSERT
AS


BEGIN


IF UPDATE(HireDate)
BEGIN


--print '..(inserted).............................................'
-- select * from inserted
--print '----------------------------------------------------------'


Select *
FROM
dbo.Employee emp INNER JOIN inserted ins
ON ins.EmployeeUUID = emp.EmployeeUUID

AND inserted.HireDate = '01/01/2000' --<< this would be your
resolved flag here


--print ''
--print '/Start Trigger Info/'
--print 'Trigger Fired (Should only fired once, even for bulk update)'
--print @updateCount
--print '/End Trigger Info/'
--print ''


END


END


Go




[quoted text, click to view]

Re: Trigger to update a value when a certian value is entered into the database Lord Kelvan
10/13/2007 5:13:37 PM
problem with that is that i think the interface builder for my
database is updateing the database records with their own information
if other things are updated and well that would work even if a 0 is
written the first one worked fine thx andrew
AddThis Social Bookmark Button