all groups > sql server programming > february 2006 >
You're in the

sql server programming

group:

create trigger doesnt work


create trigger doesnt work jen
2/20/2006 11:41:02 PM
sql server programming:
pls help! i have created the following trigger to check if a new record in
table B doesnt exist in table C, it will insert that record into table C. but
it only works when i did a manual insert, but not for imported data using the
import utility (new record in table B is import from another database).

if exists (select name from sysobjects where name = 'op_t1' and type = 'TR')
drop trigger op_t1
go

create trigger op_t1
on table B
after insert
as
if (select field1+field2 from inserted) not in
(select field1+field2 from table C)
begin
insert into table C
(field1, field2, creation_date, created_by)
select field1, field2, getdate(), '0' from inserted
end

i tried to change from inserted to table B, but it get worst; it doesnt even
work manual insert.

Re: create trigger doesnt work Jens
2/20/2006 11:53:08 PM
Trigger are fired on a command not on a row basis, so the commadn which
updates or inserts more than 1 row in your table willbreak your code.
Better use a setbased solution rather than handling each row. This will
improve as well performance as your coding structure.

insert into table C
(field1, field2, creation_date, created_by)
select field1, field2, getdate(), '0' from inserted
WHERE field1+field2 NOT IN
(
SELECT field1+field2 FROM TableC
)

HTH; Jens Suessmeyer.
Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
Jens
Yep , but what if the field1+field2 is NULL?


insert into table C
(field1, field2, creation_date, created_by)
select field1, field2, getdate(), '0' from inserted
WHERE field1+field2 NOT IN
(
SELECT field1+field2 FROM TableC ---ADD WHERE field1+field2 IS NOT NULL
)

My two cents



[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
jen
How about

insert into table C select * from inserted where
not exists ( select * from C c where c.PK=inserted.PK)

PK-Primery Keys


[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
Sorry
PK-Primary Keys




[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
Yes, It is that I was thinking




[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
Try this one
create table c (col1 char(1),col2 char(1))
create table b (col1 char(1),col2 char(1))

insert into c values ('a','b')
insert into c values ('a','c')


insert into b values ('a','b')
insert into b values ('b','b')
insert into b values ('t','a')




create trigger my_tr on b
for insert
as
insert into c select * from inserted
where not exists (select * from c c1 where
c1.col1+c1.col2=inserted.col1+inserted.col2)


insert into b values('a','c')
insert into b values('a','t')


drop table c,b



[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
jen
[quoted text, click to view]

Its sample data. Please post your DDL + sample data+ expected result?





[quoted text, click to view]

Re: create trigger doesnt work Uri Dimant
2/21/2006 12:00:00 AM
What don't you understand?

[quoted text, click to view]

Re: create trigger doesnt work jen
2/21/2006 12:05:26 AM
but i didnt set any primary key for inserted.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 12:06:28 AM
thanks jens, i am trying the code now. will keep you update.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 12:06:28 AM
no worries, field1 and field2 are not null fields.

[quoted text, click to view]
Re: create trigger doesnt work Jens
2/21/2006 12:12:21 AM
OK, but then it would be more interesting to compare the values one by
one, because the concatenation of NULL with a string would be NULL, so
the query should be more like:

insert into table C
(field1, field2, creation_date, created_by)
select field1, field2, getdate(), '0' from inserted I
WHERE NOT EXISTS
(
SELECT * FROM TableC C
WHERE I.field1 = C.Field1 AND I.field2 = C.Field2
)

HTH, Jens Suessmeyer.
Re: create trigger doesnt work jen
2/21/2006 12:43:26 AM
look like it still doesnt work for imported data. what shld i do next? pls
help.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 1:40:27 AM
thanks! i am trying it now.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 1:46:28 AM
sorry, i don't understand the portion on the insert values of a, b, c, and t.
can advise on that? thanks.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 2:16:32 AM
i know this sounds stupid but i really don't understand.

[quoted text, click to view]
Re: create trigger doesnt work jen
2/21/2006 2:40:23 AM
i have amended my trigger with ur suggestion :-

if exists (select name from sysobjects where name = 'op_t1' and type = 'TR')
drop trigger op_t1
go

create table c (field1 char(4),field2 char(30))
create table b (field1 char(4),field2 char(30))

insert into c values ('a','b')
insert into c values ('a','c')

insert into b values ('a','b')
insert into b values ('b','b')
insert into b values ('t','a')

create trigger my_tr on b
for insert
as
insert into c select field1, field2 from inserted
where not exists (select field1, field2 from c c1 where
c1.field1+c1.field2=inserted.field1+inserted.field2)

insert into b values('a','c')
insert into b values('a','t')

drop table c,b

i don't know what to subsitute the values for a, b, c, and t. pls advise.

[quoted text, click to view]
Re: create trigger doesnt work ML
2/21/2006 4:16:30 AM
If you need help on implementing the suggested solution into your schema,
please post DDL and sample data. It's impossible to design a trigger for your
specific case without seeing the table.

Read this:
http://www.aspfaq.com/etiquette.asp?id=5006


ML

---
Re: create trigger doesnt work jen
2/21/2006 4:46:27 PM
thanks, ML. i will read the link you have provided and see if there's any
clue. thank you once again.

[quoted text, click to view]
AddThis Social Bookmark Button