Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Dargon
Starting Member
26 Posts |
Posted - 2005-06-24 : 18:21:10
|
| Hi,I am new in SQL, I need help to convert trigger written on Oracle.It looks that upon entering AuthorizedByit sets AuthorizedDate to sysdate if no date provided, otherwise uses provided date, otherwise sets AuthorizedDate to null.Any help would be appreciated,Here it is:CREATE OR REPLACE TRIGGER trg_optproj_authorizedbyBEFORE INSERT OR UPDATEOF authorizedbyON OPTIMAINPROJECTSREFERENCING NEW AS NEW OLD AS OLDFOR EACH ROWBEGINIF :NEW.authorizedby IS NOT NULL THEN IF :NEW.authorizeddate IS NULL THEN :NEW.authorizeddate := SYSDATE; ELSE :NEW.authorizeddate := :NEW.authorizeddate; END IF; IF :OLD.status <> 'COMPLETED' THEN :NEW.status := 'AUTHORIZED'; END IF;ELSE :NEW.authorizeddate := NULL; IF :OLD.status = 'AUTHORIZED' THEN :NEW.status := 'LOCKED'; END IF;END IF;EXCEPTION WHEN OTHERS THEN RAISE;END;Dargon. |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2005-06-24 : 20:11:46
|
| Where Oracle uses the New and Old virtual tables within a trigger, Microsoft uses the Inserted and Deleted virtual tables. The actual trigger definition systax is slightly different but you can look that up in BOL. Also, MS doesn't loop through the rows. You perform the actual work and associated logic in the query.This is completely untested but should get you headed in the right direction.[Code]CREATE TRIGGER trg_optproj_authorizedbyON OPTIMAINPROJECTSfor INSERT, UPDATEBEGINupdate OPTIMAINPROJECTSset authorizeddate = case when i.authorizedby is Null then Null when i.authorizedby is not Null and i.authorizeddate IS NULL then Current_Timestamp() -- GetDate() if you prefer else authorizeddate end, status = case when i.authorizedby is Null and d.status = 'AUTHORIZED' then 'LOCKED' when i.authorizedby is not Null and d.Status <> 'COMPLETED' then 'AUTHORIZED' else status endfrom OPTIMAINPROJECTS omp join Inserted i on i.PrimaryKey = omp.PrimaryKey join deleted d on i.PrimaryKey = omp.PrimaryKeyif @@Error <> 0 then raiserror '<Your error message>', <Error number reflecting the severity>END[/Code]HTH================================================================='Tis with our judgements as our watches: none Go just alike, yet each believes his own. -Alexander Pope, poet (1688-1744) |
 |
|
|
sanjnep
Posting Yak Master
191 Posts |
|
|
Dargon
Starting Member
26 Posts |
Posted - 2005-06-27 : 11:44:19
|
| Thanks, Bustaz Kool, It works!Actually I had to modify syntax a little bit:create TRIGGER trg_optproj_authorizedbyON OPTIMAINPROJECTSfor INSERT, UPDATEasBEGINdeclare @INauthorizedby varchar(100)declare @INauthorizeddate varchar(100)declare @INstatus varchar(100)set @INauthorizedby=(Select i.authorizedby from inserted i, OPTIMAINPROJECTS op where op.ProjectID=i.ProjectID)set @INauthorizeddate=(Select i.authorizedby from inserted i, OPTIMAINPROJECTS op where op.ProjectID=i.ProjectID)set @INstatus=(Select d.status from deleted d, OPTIMAINPROJECTS op where op.ProjectID=d.ProjectID)update OPTIMAINPROJECTSset authorizeddate = case when @INauthorizedby is Null then Null when @INauthorizedby is not Null and @INauthorizeddate IS NULL then GetDate() else authorizeddate end,status = case when @INauthorizedby is Null and @INstatus = 'AUTHORIZED' then 'LOCKED' when @INauthorizedby is not Null and @INstatus <> 'COMPLETED' then 'AUTHORIZED' else status endENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO |
 |
|
|
|
|
|
|
|