dbTalk Databases Forums  

Update field in new record based on value of another field in thesame record

microsoft.public.sqlserver.clients microsoft.public.sqlserver.clients


Discuss Update field in new record based on value of another field in thesame record in the microsoft.public.sqlserver.clients forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Richard
 
Posts: n/a

Default Update field in new record based on value of another field in thesame record - 10-23-2010 , 11:44 AM






I want to update a field when a new timecard record comes in based
upon the value in another field on the same record and the value of
another field in a different table.

If I were writing a SQL statement for after the fact it would be:

update timecard set tudef1='JP0' where tudef5='JP' and tmatter in
(select mmatter from matter where mloc<>'30')

but I sure would like to do it with a trigger when the original input
happens.

Thanks in advance.

Rich

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Update field in new record based on value of another field in the same record - 10-23-2010 , 02:01 PM






Richard (richard.mogy (AT) gmail (DOT) com) writes:
Quote:
I want to update a field when a new timecard record comes in based
upon the value in another field on the same record and the value of
another field in a different table.

If I were writing a SQL statement for after the fact it would be:

update timecard set tudef1='JP0' where tudef5='JP' and tmatter in
(select mmatter from matter where mloc<>'30')

but I sure would like to do it with a trigger when the original input
happens.
CREATE TRIGGER timecard_tri ON timecard AFTER INSERT, UPDATE AS

UPDATE timecard
SET tudefl = 'JPO'
FROM timecard t
WHERE EXISTS (SELECT *
FROM inserted i
WHERE t.key = i.key
AND i.tudef5 = 'JP'
AND i.tmatter in (select m.mmatter
from m.matter
where m.mloc<>'30'))


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.