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 |
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-25 : 12:07:50
|
Hi again gang,I have a table with the following long-winded trigger on it:create trigger tr_UpdateTimeTableBillCityon TimeTablefor insert, updateasupdate ttset tt.city01 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time01 = th.tkt_noupdate ttset tt.city02 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time02 = th.tkt_noupdate ttset tt.city03 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time03 = th.tkt_noupdate ttset tt.city04 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time04 = th.tkt_noupdate ttset tt.city05 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time05 = th.tkt_noupdate ttset tt.city06 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time06 = th.tkt_noupdate ttset tt.city07 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time07 = th.tkt_noupdate ttset tt.city08 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time08 = th.tkt_noupdate ttset tt.city09 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time09 = th.tkt_noupdate ttset tt.city10 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time10 = th.tkt_noupdate ttset tt.city11 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time11 = th.tkt_noupdate ttset tt.city12 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time12 = th.tkt_noupdate ttset tt.city13 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time13 = th.tkt_noupdate ttset tt.city14 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time14 = th.tkt_noupdate ttset tt.city15 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time15 = th.tkt_noupdate ttset tt.city16 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time16 = th.tkt_noupdate ttset tt.city17 = th.bill_cityfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time17 = th.tkt_noupdate ttset tt.Cust01 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time01 = th.tkt_noupdate ttset tt.Cust02 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time02 = th.tkt_noupdate ttset tt.Cust03 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time03 = th.tkt_noupdate ttset tt.Cust04 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time04 = th.tkt_noupdate ttset tt.Cust05 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time05 = th.tkt_noupdate ttset tt.Cust06 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time06 = th.tkt_noupdate ttset tt.Cust07 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time07 = th.tkt_noupdate ttset tt.Cust08 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time08 = th.tkt_noupdate ttset tt.Cust09 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time09 = th.tkt_noupdate ttset tt.Cust10 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time10 = th.tkt_noupdate ttset tt.Cust11 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time11 = th.tkt_noupdate ttset tt.Cust12 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time12 = th.tkt_noupdate ttset tt.Cust13 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time13 = th.tkt_noupdate ttset tt.Cust14 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time14 = th.tkt_noupdate ttset tt.Cust15 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time15 = th.tkt_noupdate ttset tt.Cust16 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time16 = th.tkt_noupdate ttset tt.Cust17 = th.bill_lst_namfrom inserted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time17 = th.tkt_no This trigger works fine when a record is INSERTED or UPDATED(The user insets a ticket number into any of the 'timexx' columns and this trigger updates the corresponding city and name columns), but if a user tries to DELETE a record from the timexx columns, we get "another user has made conflicting changes. cancel your changes and try again." Take off this trigger and it works fine. I am looking to rewrite this trigger into some thing more efficient (if possible) and to fix the DELETE issue.Any help is as always greatly appreciated..Andy There's never enough time to type code right, but always enough time for a hotfix... |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-02-25 : 13:43:42
|
| This trigger should not fire at all on a delete, since it is an insert and update trigger.Do you have more than one trigger on this table?Codo Ergo Sum |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-25 : 14:14:31
|
Hey Michael,When I first got put on this project there were actually 4 triggers on this table, one of them a DELETE trigger that I thought for sure was the problem. At the moment the only trigger on the table is the one posted here. I know it should not be doing anything during a delete, but as long as this trigger is on the table the app pukes and throws the error. I suppose it is possible that this is an app issue, but if I take off this trigger then it works fine.Here is the lowdown:If we insert a svc call, it works, and if we CHANGE the call from one number to another, it works, but if we DELETE the call (set timexx to null) then it blows up. MY thought was this:The trigger is trying to update fields, but since we are setting the timexx value to null the trigger cannot find the appropriate ticket in the JOIN statements to do the update with, and so blows up. What I need is to have this trigger that will update the table when a value is present the way it is written ELSE update the table with null when the timexx column is set to the same.Here is the DELETE trigger that I originally thought was the culprit, and basically is what we need to incorporate(I think) into the first trigger:CREATE trigger tr_deleteTimeTableShipCityon dbo.TimeTablefor deleteas update ttset tt.city01 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time01 = th.tkt_no update ttset tt.city02 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time02 = th.tkt_no update ttset tt.city03 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time03 = th.tkt_no update ttset tt.city04 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time04 = th.tkt_no update ttset tt.city05 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time05 = th.tkt_no update ttset tt.city06 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time06 = th.tkt_no update ttset tt.city07 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time07 = th.tkt_no update ttset tt.city08 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time08 = th.tkt_no update ttset tt.city09 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time09 = th.tkt_no update ttset tt.city10 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time10 = th.tkt_no update ttset tt.city11 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time11 = th.tkt_no update ttset tt.city12 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time12 = th.tkt_no update ttset tt.city13 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time13 = th.tkt_no update ttset tt.city14 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time14 = th.tkt_no update ttset tt.city15 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time15 = th.tkt_no update ttset tt.city16 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time16 = th.tkt_no update ttset tt.city17 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time17 = th.tkt_no update ttset tt.Cust01 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time01 = th.tkt_no update ttset tt.Cust02 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time02 = th.tkt_no update ttset tt.Cust03 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time03 = th.tkt_no update ttset tt.Cust04 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time04 = th.tkt_no update ttset tt.Cust05 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time05 = th.tkt_no update ttset tt.Cust06 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time06 = th.tkt_no update ttset tt.Cust07 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time07 = th.tkt_no update ttset tt.Cust08 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time08 = th.tkt_no update ttset tt.Cust09 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time09 = th.tkt_no update ttset tt.Cust10 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time10 = th.tkt_no update ttset tt.Cust11 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time11 = th.tkt_no update ttset tt.Cust12 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time12 = th.tkt_no update ttset tt.Cust13 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time13 = th.tkt_no update ttset tt.Cust14 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time14 = th.tkt_no update ttset tt.Cust15 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time15 = th.tkt_no update ttset tt.Cust16 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time16 = th.tkt_no update ttset tt.Cust17 = nullfrom deleted iinner join timetable tton i.NAM = tt.NAMand i.SVC_DAT = tt.SVC_DATinner join ps_tkt_hdr thon tt.time17 = th.tkt_no Any Ideas? There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-02-25 : 14:16:15
|
| Holy Denormalized batmanI would normalize my table and lose that trigger...What are you trying to do?"Initialize" a row to the database?Why put "empty" data in for data's sake?Also if it's an INSERT, how can the row already be in TimeTable? Don't you need an AFTER Trigger?Brett8-) |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-25 : 15:00:59
|
Hey Brett..Yeah, isn't this some frightening stuff? The table is denormalized as hell, here's how it works:the table is created from a procedure. What happens is the proc selects every user who is a servicer, takes a date range supplied to the proc, and inserts into the table one row for each servicer per day. the columns time01,time04, etc. are where the ticket number goes for each service call. In reality the whole mess was written for just these timexx columns(which of course negates the trigger), but the app being used doesn't allow for lookup on tables other than the one being used, and as such the scheduler needs to see the name of the customer and the city that they live in.SOOOOOO....... I have been put on the project from hell. I'm not real thrilled about it, but I gotta try...I'm wondering if we can't solve this issue using the AFTER trigger idea, or maybe a proc called from a trigger?I mean, what the hell, we're already running against the grain with this!AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2005-02-25 : 17:01:56
|
Maybe what you could do is normalize the data, and then create a view that cross tabs the data.Then you could forget about fixing the trigger from hell.Of course, that's easier said than done. quote: Originally posted by steamngn Hey Brett..Yeah, isn't this some frightening stuff? The table is denormalized as hell, here's how it works:the table is created from a procedure. What happens is the proc selects every user who is a servicer, takes a date range supplied to the proc, and inserts into the table one row for each servicer per day. the columns time01,time04, etc. are where the ticket number goes for each service call. In reality the whole mess was written for just these timexx columns(which of course negates the trigger), but the app being used doesn't allow for lookup on tables other than the one being used, and as such the scheduler needs to see the name of the customer and the city that they live in.SOOOOOO....... I have been put on the project from hell. I'm not real thrilled about it, but I gotta try...I'm wondering if we can't solve this issue using the AFTER trigger idea, or maybe a proc called from a trigger?I mean, what the hell, we're already running against the grain with this!AndyThere's never enough time to type code right, but always enough time for a hotfix...
Codo Ergo Sum |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-02-25 : 17:09:40
|
| You need to run SQL Profiler to determine what the application is doing on delete as that trigger isn't the problem. I realize you say if you remove it then everything works fine. Perhaps you could generate the DDL for your TimeTable and include all triggers.Tara |
 |
|
|
PW
Yak Posting Veteran
95 Posts |
Posted - 2005-02-25 : 17:35:31
|
| Original post stated:>>but if a user tries to DELETE a record from the timexx columnsYou can't DELETE from a COLUMN.Do you mean DELETE record, or do you mean UPDATE record and set some values NULL ? |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-25 : 18:34:55
|
| Hey Tara, PW,I THINK what the app is doing isn't really a DELETE but an UPDATE as you suggest. Tara is right, I need to get on profiler and see what's what here. I'm pretty sure that If I change the trigger to do something different if the inserted value is null that will solve the problem. Just for ha-ha's I sent a note to the app vendor telling them we need to have lookups based on views...What a dreamer I am! Anyway, I will jump back into it tomorrow and see if I can't shed some more light on this disaster.AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-26 : 09:20:29
|
Good Morning Boys and Girls,Ran through Profiler this AM, and sure enough, this is an UPDATE using NULL as the value(DUH! After I thought about it a minute, of course it is. The rowset isn't being removed, just the data in one column!). So, The first trigger will be ok if it takes NULL into account. Since the trigger is blowing up on a NULL update, it looks like the JOIN is where the problem is....And We're off!! who will be the first to cross the finish line with this marvelous piece o' code?might-not-be-me,Andy There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-02-27 : 14:18:59
|
This was almost TOO easy:CREATE TRIGGER updatecityandname ON [dbo].[TimeTable] FOR INSERT, UPDATE, DELETE ASupdate ttset city01=(select bill_city from ps_tkt_hdrwhere tkt_no=time01),cust01=(select bill_nam from ps_tkt_hdrwhere tkt_no=time01)from timetable ttupdate ttset city04=(select bill_city from ps_tkt_hdrwhere tkt_no=time04),cust04=(select bill_nam from ps_tkt_hdrwhere tkt_no=time04)from timetable ttupdate ttset city07=(select bill_city from ps_tkt_hdrwhere tkt_no=time07),cust07=(select bill_nam from ps_tkt_hdrwhere tkt_no=time07)from timetable ttupdate ttset city10=(select bill_city from ps_tkt_hdrwhere tkt_no=time10),cust10=(select bill_nam from ps_tkt_hdrwhere tkt_no=time10)from timetable ttupdate ttset city13=(select bill_city from ps_tkt_hdrwhere tkt_no=time13),cust13=(select bill_nam from ps_tkt_hdrwhere tkt_no=time13)from timetable ttupdate ttset city16=(select bill_city from ps_tkt_hdrwhere tkt_no=time16),cust16=(select bill_nam from ps_tkt_hdrwhere tkt_no=time16)from timetable tt The problem was that the app AND the trigger were performing an update on the table using INSERTED. This trigger has resolved the conflict, although the table is still denormalized and ugly...AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
|
|
|
|
|