| Author |
Topic |
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 13:10:49
|
Have the following trigger that will delete any record with a code type 3 when two records are inserted with the same ord_no, ord_type,item_no,line_seq_no and one of them has code type 1 and the other a code type 3.Problem I have now is if there is only a code type 3 record being inserted it gets deleted also. Need to modify the trigger so the code type 3 record will stay if there is no corresponding code type 1. ALTER TRIGGER [DeletefromOrderRebate2] ON [dbo].[OrderRebate] after insertASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. --SET NOCOUNT ON; -- Insert statements for trigger here -- Insert statements for trigger heredelete orderrebateFROM OrderRebate inner join inserted on inserted.ord_type = OrderRebate.ord_type andinserted.ord_no = OrderRebate.ord_no and inserted.line_seq_no = OrderRebate.line_seq_nowhere inserted.ord_type = OrderRebate.ord_type and inserted.ord_no = OrderRebate.ord_noand inserted.line_seq_no = OrderRebate.line_seq_no and inserted.cd_tp = '3'END--GO |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 14:04:26
|
| I think if I do a group by ord_type, ord_no, item_no, line_seq_no then only perform the delete if the count is greater than 1. But I'm not quite sure how to add that logic. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-24 : 14:10:28
|
Try thisdelete tFROM ( select ord_type, ord_no, item_no, line_seq_no from OrderRebate group by ord_type, ord_no, item_no, line_seq_no having count(*)>1) as tinner join inserted on inserted.ord_type = t.ord_type andinserted.ord_no = t.ord_no and inserted.line_seq_no = t.line_seq_nowhere inserted.ord_type = t.ord_type and inserted.ord_no = t.ord_noand inserted.line_seq_no = t.line_seq_no and inserted.cd_tp = '3' MadhivananFailing to plan is Planning to fail |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 14:14:05
|
| Records being inserted into OrderRebateOrd_type Ord_no Item_no line_seq_no cd_tp O 55 part2 1 1 O 55 part2 1 3 O 56 part3 1 3Since order 55 had two records where the first 4 fields are identical I want the record with cd_tp 3 deleted. But I do not want to delete ord_no 56 which has a cd_tp of 3 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-24 : 14:15:37
|
quote: Originally posted by Vack Records being inserted into OrderRebateOrd_type Ord_no Item_no line_seq_no cd_tp O 55 part2 1 1 O 55 part2 1 3 O 56 part3 1 3Since order 55 had two records where the first 4 fields are identical I want the record with cd_tp 3 deleted. But I do not want to delete ord_no 56 which has a cd_tp of 3
Did you try the method I suggested?MadhivananFailing to plan is Planning to fail |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-24 : 14:18:27
|
| I would use an INSTEAD OF trigger so that I wouldn't have to delete any rows. But that's if I didn't have control over the actual insert. Can't you modify the source so that it inserts what you want?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 14:21:58
|
| I received this error when trying to execute the triggerMsg 4418, Level 16, State 1, Procedure DeletefromOrderRebate2, Line 34Derived table 'orderrebate' is not updatable because it contains aggregates. |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 14:26:39
|
| To answer tkizer:Wouldn't I run into the same problem with my insert?It would find two records in a rebate table and want to write them both. When an order is entered a rebate table is read which contains info by cus_no and item_no and cus_type and Item_no. Some customers have the possiblity of falling under both categories. When that happens I only want cd_tp 1 used(cust_no and item_no)which is easier? deleting the extra record after it is inserted or being able to select the correct one when inserting? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-24 : 14:42:26
|
Also trydelete OrderRebateFROM OrderRebateinner join inserted on inserted.ord_type = OrderRebate.ord_type andinserted.ord_no = OrderRebate.ord_no and inserted.line_seq_no = OrderRebate.line_seq_nowhere inserted.ord_type = OrderRebate.ord_type and inserted.ord_no = OrderRebate.ord_noand inserted.line_seq_no = OrderRebate.line_seq_no and inserted.cd_tp = '3'and exists ( select ord_type, ord_no, item_no, line_seq_no from OrderRebate where inserted.ord_type = ord_type and inserted.ord_no = item_no and inserted.ord_no = ord_no and inserted.line_seq_no = line_seq_no group by ord_type, ord_no, item_no, line_seq_no having count(*)>1) MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-24 : 14:44:11
|
or simplydelete OrderRebateFROM OrderRebatewhere exists ( select ord_type, ord_no, item_no, line_seq_no from OrderRebate where inserted.ord_type = ord_type and inserted.ord_no = item_no and inserted.ord_no = ord_no and inserted.line_seq_no = line_seq_no and inserted.cd_tp = '3' group by ord_type, ord_no, item_no, line_seq_no having count(*)>1) Note that it is not testedMadhivananFailing to plan is Planning to fail |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 14:57:24
|
| The first suggesion did not work. Still inserted two records. The 2nd suggestion deleted everything but I had to modify it so that the syntax was correct. But I may have changed something I shouldn't have. Here is the 2nd one after my modifications.delete OrderRebateFROM OrderRebatewhere exists ( select inserted.ord_type, inserted.ord_no, inserted.item_no, inserted.line_seq_no from OrderRebate join inserted on inserted.ord_type = orderrebate.ord_type and inserted.ord_no = orderrebate.ord_no and inserted.item_no = orderrebate.item_no and inserted.line_seq_no = orderrebate.line_seq_no where inserted.ord_type = orderrebate.ord_type and inserted.ord_no = orderrebate.ord_no and inserted.item_no = orderrebate.item_no and inserted.line_seq_no = orderrebate.line_seq_no group by inserted.ord_type, inserted.ord_no, inserted.item_no, inserted.line_seq_no having count(*)>1) |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-06-24 : 15:01:00
|
| I did notice this was missing from the where clause:inserted.cd_tp = '3'but once I put that back in I was back to where I started. Both records were inserted. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-24 : 15:02:24
|
quote: Originally posted by Vack To answer tkizer:Wouldn't I run into the same problem with my insert?It would find two records in a rebate table and want to write them both.
Yes but you'd only run the insert instead of insert followed by delete.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|