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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Problem with Trigger

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 insert
AS
BEGIN
-- 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 here
delete orderrebate
FROM OrderRebate inner join inserted on inserted.ord_type = OrderRebate.ord_type and
inserted.ord_no = OrderRebate.ord_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.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.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-06-24 : 14:06:13
It would be best if you illustrated your issue with sample data.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-24 : 14:10:28
Try this


delete t
FROM
(
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 t
inner join inserted on inserted.ord_type = t.ord_type and
inserted.ord_no = t.ord_no and inserted.line_seq_no = t.line_seq_no
where inserted.ord_type = t.ord_type and inserted.ord_no = t.ord_no
and inserted.line_seq_no = t.line_seq_no and inserted.cd_tp = '3'




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2008-06-24 : 14:14:05
Records being inserted into OrderRebate

Ord_type Ord_no Item_no line_seq_no cd_tp
O 55 part2 1 1
O 55 part2 1 3
O 56 part3 1 3

Since 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
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-24 : 14:15:37
quote:
Originally posted by Vack

Records being inserted into OrderRebate

Ord_type Ord_no Item_no line_seq_no cd_tp
O 55 part2 1 1
O 55 part2 1 3
O 56 part3 1 3

Since 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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Vack
Aged Yak Warrior

530 Posts

Posted - 2008-06-24 : 14:21:58
I received this error when trying to execute the trigger

Msg 4418, Level 16, State 1, Procedure DeletefromOrderRebate2, Line 34
Derived table 'orderrebate' is not updatable because it contains aggregates.
Go to Top of Page

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?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-24 : 14:42:26
Also try

delete OrderRebate
FROM OrderRebate
inner join inserted on inserted.ord_type = OrderRebate.ord_type and
inserted.ord_no = OrderRebate.ord_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.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
)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-24 : 14:44:11
or simply
delete OrderRebate
FROM OrderRebate
where 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 tested

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 OrderRebate
FROM OrderRebate
where 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
)
Go to Top of Page

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.
Go to Top of Page

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 Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -