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 |
|
Rliss
Starting Member
31 Posts |
Posted - 2006-05-12 : 15:54:51
|
| In an after insert trigger, I want to insert the data into another table, If the insert fails(meaning there is already a record) I want to catch the exception, and update the record that is there. Here is how I do it in Oracle:BEGININSERT INTO LINKSET_DAILY_HIGH VALUES (:UPD.Linkset_Name, TO_CHAR(:UPD.Start_Date_Time,'yyyymmdd'), :UPD.Start_Date_Time , :UPD.Hour_Of_Day , :UPD.Sequence_In_Hour, :UPD.Link_Count , :UPD.Trans_ISUP_Oct, :UPD.Trans_SCCP_Oct, :UPD.Trans_Other_Oct, :UPD.Recv_ISUP_Oct , :UPD.Recv_SCCP_Oct , :UPD.Recv_Other_Oct, :UPD.Total_Bandwidth, :UPD.High_PCT_Occupancy);EXCEPTION WHEN DUP_VAL_ON_INDEX THEN UPDATE LINKSET_DAILY_HIGH SET start_date_time = :UPD.start_date_time, hour_of_day = :UPD.hour_of_day, sequence_in_hour = :UPD.sequence_in_hour, link_count = :UPD.link_count, trans_isup_oct = :UPD.trans_isup_oct, trans_sccp_oct = :UPD.trans_sccp_oct, trans_other_oct = :UPD.trans_other_oct, recv_isup_oct = :UPD.recv_isup_oct, recv_sccp_oct = :UPD.recv_sccp_oct, recv_other_oct = :UPD.recv_other_oct, total_bandwidth = :UPD.total_bandwidth, high_pct_occupancy = :UPD.high_pct_occupancy WHERE linkset_name = :UPD.linkset_name AND start_date_text = TO_CHAR(:UPD.start_date_time,'yyyymmdd') AND high_pct_occupancy < :UPD.high_pct_occupancy; RETURN;END;':UPD' is the inserted data. The two things that I need help with are:1. binding to the inserted data for the 'where' clause of the update statement2. catching the exceptionAny insight for this newbee will be greatly appreciated.Thanks.RLissGo |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-12 : 16:06:01
|
| You should start by reading the "CREATE TRIGGER" topic in the "Transact-SQL Reference" section of SQL Server Books Online.The syntax for a trigger is different in SQL Server, so you really need to start there.You should have SQL Server Books Online if you have installed the SQL Server Client Tools. If you don't have them installed, you can also read it online at the MSDN site.CODO ERGO SUM |
 |
|
|
Rliss
Starting Member
31 Posts |
Posted - 2006-05-12 : 16:43:08
|
| I've read plenty of documentation, what it lacks is a real world example of a multifield update with greater criteria than where exists. I'm also just looking for someone's opinion on how best to catsh an exception.RLiss |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-05-12 : 16:57:45
|
You need to get familiary with the concept of the trigger tables (inserted, deleted). You can read about them in SQL Server Books Online. On insert, inserted contains the rows being inserted. On update, deleted contains the before image of the rows and inserted contains the after image of the rows. On delete, deleted contains the rows being deleted.This should get you started though:INSERT INTO LINKSET_DAILY_HIGH SELECT Linkset_Name, Start_Date_Time, Start_Date_Time, Hour_Of_Day, Sequence_In_Hour, Link_Count, Trans_ISUP_Oct, Trans_SCCP_Oct, Trans_Other_Oct, Recv_ISUP_Oct, Recv_SCCP_Oct, Recv_Other_Oct, Total_Bandwidth, High_PCT_OccupancyFROM inserted UPDATE lSET start_date_time = i.start_date_time,hour_of_day = i.hour_of_day,sequence_in_hour = i.sequence_in_hour,link_count = i.link_count,trans_isup_oct = i.trans_isup_oct,trans_sccp_oct = i.trans_sccp_oct,trans_other_oct = i.trans_other_oct,recv_isup_oct = i.recv_isup_oct,recv_sccp_oct = i.recv_sccp_oct,recv_other_oct = i.recv_other_oct,total_bandwidth = i.total_bandwidth,high_pct_occupancy = i.high_pct_occupancyFROM LINKSET_DAILY_HIGH lINNER JOIN inserted iON l.PKColumnGoesHere = i.linkset_name AND l.start_date_text = i.start_date_time AND l.high_pct_occupancy < i.high_pct_occupancy Tara Kizeraka tduggan |
 |
|
|
TimS
Posting Yak Master
198 Posts |
Posted - 2006-05-12 : 16:59:53
|
| I would look at "INSTEAD OF Triggers", "@@ERROR", and if SQL2005 "Using Try/Catch Blocks"Tim S |
 |
|
|
Rliss
Starting Member
31 Posts |
Posted - 2006-05-12 : 17:09:44
|
| Thank You Tara, that worked perfectly. I've been spoiled in Oracle where I didn't have to reference the trigger tables by 'select where exists' or 'inner join'. By force, I am learning quickly. Thanks again, you're the best... RLiss |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-12 : 17:33:25
|
quote: Originally posted by Rliss Thank You Tara, that worked perfectly. I've been spoiled in Oracle where I didn't have to reference the trigger tables by 'select where exists' or 'inner join'. By force, I am learning quickly. Thanks again, you're the best... RLiss
That is why I suggested that you read the "CREATE TRIGGER" topic in the "Transact-SQL Reference" section of SQL Server Books Online. If you had, you might have seen this:"A few special tables are used in CREATE TRIGGER statements: deleted and inserted are logical (conceptual) tables. They are structurally similar to the table on which the trigger is defined, that is, the table on which the user action is attempted, and hold the old values or new values of the rows that may be changed by the user action."Or this example of a trigger using the deleted and inserted tables:CREATE TRIGGER updEmployeeData ON employeeData FOR update AS/*Check whether columns 2, 3 or 4 has been updated. If any or all of columns 2, 3 or 4 have been changed, create an audit record. The bitmask is: power(2,(2-1))+power(2,(3-1))+power(2,(4-1)) = 14. To check if all columns 2, 3, and 4 are updated, use = 14 in place of >0 (below).*/ IF (COLUMNS_UPDATED() & 14) > 0/*Use IF (COLUMNS_UPDATED() & 14) = 14 to see if all of columns 2, 3, and 4 are updated.*/ BEGIN-- Audit OLD record. INSERT INTO auditEmployeeData (audit_log_type, audit_emp_id, audit_emp_bankAccountNumber, audit_emp_salary, audit_emp_SSN) SELECT 'OLD', del.emp_id, del.emp_bankAccountNumber, del.emp_salary, del.emp_SSN FROM deleted del-- Audit NEW record. INSERT INTO auditEmployeeData (audit_log_type, audit_emp_id, audit_emp_bankAccountNumber, audit_emp_salary, audit_emp_SSN) SELECT 'NEW', ins.emp_id, ins.emp_bankAccountNumber, ins.emp_salary, ins.emp_SSN FROM inserted ins ENDGO CODO ERGO SUM |
 |
|
|
|
|
|
|
|