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 2000 Forums
 Transact-SQL (2000)
 conditional trigger execution

Author  Topic 

CorpDirect
Yak Posting Veteran

92 Posts

Posted - 2005-10-07 : 19:30:34
Good day,

I need to write a trigger in MSSQL for the first time. This will be an AFTER INSERT trigger, and must fire only when a specified value exists in one of the inserted columns. I have looked at trigger syntax in BOL and some examples, but do not have any idea how to make a trigger fire conditionally...

In detail, the trigger needs to create a record in the test_dest table when a record with item = 'THEVAL' is inserted in the test_source table. The inserted values for order_no and line_no must be carried to test_dest. (I know these can be gotten from the "virtual" table INSERTED after insert.) When item != 'THEVAL' the trigger action must not fire.

Here is a test scenario:
SET NOCOUNT ON

-- the source table
CREATE TABLE test_source (
order_no VARCHAR(10) NOT NULL
, line_no INT NOT NULL
, item VARCHAR(10) NOT NULL
, descrip VARCHAR(50) NOT NULL
)
GO

-- the destination table
CREATE TABLE test_dest (
order_no VARCHAR(10) NOT NULL
, line_no INT NOT NULL
, other_col VARCHAR(50) NULL
)
GO

-- CREATE TRIGGER -- not sure how to write conditional trigger so will leave this off

-- some inserts for testing purposes
INSERT INTO test_source VALUES ('145',1,'ITEM1','First Item')
INSERT INTO test_source VALUES ('145',2,'SOMITEM','Some Item')
INSERT INTO test_source VALUES ('145',3,'THEVAL','Triggering Item 1')
INSERT INTO test_source VALUES ('145',4,'THEVAL','Triggering Item 2')
INSERT INTO test_source VALUES ('145',5,'ITEM5','Fifth Item')
INSERT INTO test_source VALUES ('145',6,'THEVAL','Triggering Item 3')
INSERT INTO test_source VALUES ('145',7,'LSTITEM','Last Item')

-- check for created records in destination table
SELECT * FROM test_dest

-- clean up
DROP TABLE test_source
GO

DROP TABLE test_dest
GO

SET NOCOUNT OFF

So, with that as a basis, can anyone get me started in the right direction?

Thanks for any assistance,

Daniel

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-10-07 : 20:06:11
This should do it:

create trigger TR_test_source
on
test_source
for
insert
as

insert into test_dest
(
order_no,
line_no
)
select
order_no,
line_no
from
inserted
where
item = 'THEVAL'




CODO ERGO SUM
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-10-08 : 05:36:50
What happens if that row is deleted in test_source, and then re-inserted later? The insert into test_dest will fail.

Also, what do you want to have happen if a row is inserted which does NOT have item = 'THEVAL' and a later UPDATE changes item to become 'THEVAL' - should it then Insert into test_dest?

Just checking!

Kristen
Go to Top of Page

CorpDirect
Yak Posting Veteran

92 Posts

Posted - 2005-10-08 : 19:43:16
Thanks Michael -- I didn't realize using a WHERE clause against INSERTED would limit the adding of rows, but it makes sense now that I see it. I'll give this a try as soon as I'm back in the office.

Kristen, the source application never actually deletes rows -- just marks them as deleted with a 'status' column. Also from the source application updates to the 'value' column are not permitted once a record is created (I think).

I'll post back my results once I've had a chance to test. Thanks!

Daniel
Go to Top of Page

CorpDirect
Yak Posting Veteran

92 Posts

Posted - 2005-10-09 : 23:20:09
Hello Michael,

The concept works beatifully..and it's so simple when you know how! Thanks for the help.

Kristen, I went back and checked the app, and I was mostly correct: records are marked for deletion by the app rather than actually deleting. There is a maintenance routine run periodically that is supposed to clear out deleted records, but I checked some history and ran the routine, and found that these records actually are not deleted..ever, it seems!

Any rate I'll review this further and decide whether to remove records in my target table when a row is marked for deletion in the app's source table.

As for changing records, it seems the app also does not allow editing of line items once they are created. Either they can be deleted, or the whole order can be deleted, but through the app there is no way to edit any order line item. Don't have to worry about that, then, unless something changes in future. Thanks for thinking of it though!

Regards,

Daniel
Go to Top of Page
   

- Advertisement -