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.
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.
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] "Jens" <Jens@sqlserver2005.de> wrote in message news:1140508388.179175.19970@g43g2000cwa.googlegroups.com... > 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. >
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] "jen" <jen@discussions.microsoft.com> wrote in message news:333A06C5-DB7A-4665-BDE8-05A8683CFF71@microsoft.com... > 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. > >
Sorry PK-Primary Keys [quoted text, click to view] "Uri Dimant" <urid@iscar.co.il> wrote in message news:utaCnsrNGHA.2696@TK2MSFTNGP14.phx.gbl... > 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 > > > "jen" <jen@discussions.microsoft.com> wrote in message > news:333A06C5-DB7A-4665-BDE8-05A8683CFF71@microsoft.com... >> 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. >> >> > >
Yes, It is that I was thinking [quoted text, click to view] "Jens" <Jens@sqlserver2005.de> wrote in message news:1140509541.342502.255200@o13g2000cwo.googlegroups.com... > 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. >
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] "jen" <jen@discussions.microsoft.com> wrote in message news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... > look like it still doesnt work for imported data. what shld i do next? pls > help. > > "jen" wrote: > >> thanks jens, i am trying the code now. will keep you update. >> >> "Jens" wrote: >> >> > 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. >> > >> >
jen [quoted text, click to view] > sorry, i don't understand the portion on the insert values of a, b, c, and > t.
Its sample data. Please post your DDL + sample data+ expected result? [quoted text, click to view] "jen" <jen@discussions.microsoft.com> wrote in message news:3F41262E-47EF-46D8-AEA9-15650CBA0257@microsoft.com... > sorry, i don't understand the portion on the insert values of a, b, c, and > t. > can advise on that? thanks. > > "Uri Dimant" wrote: > >> 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 >> >> >> >> "jen" <jen@discussions.microsoft.com> wrote in message >> news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... >> > look like it still doesnt work for imported data. what shld i do next? >> > pls >> > help. >> > >> > "jen" wrote: >> > >> >> thanks jens, i am trying the code now. will keep you update. >> >> >> >> "Jens" wrote: >> >> >> >> > 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. >> >> > >> >> > >> >> >>
What don't you understand? [quoted text, click to view] "jen" <jen@discussions.microsoft.com> wrote in message news:112816B1-4FF2-4385-AB8B-848522D69797@microsoft.com... >i know this sounds stupid but i really don't understand. > > "Uri Dimant" wrote: > >> jen >> > sorry, i don't understand the portion on the insert values of a, b, c, >> > and >> > t. >> >> Its sample data. Please post your DDL + sample data+ expected result? >> >> >> >> >> >> "jen" <jen@discussions.microsoft.com> wrote in message >> news:3F41262E-47EF-46D8-AEA9-15650CBA0257@microsoft.com... >> > sorry, i don't understand the portion on the insert values of a, b, c, >> > and >> > t. >> > can advise on that? thanks. >> > >> > "Uri Dimant" wrote: >> > >> >> 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 >> >> >> >> >> >> >> >> "jen" <jen@discussions.microsoft.com> wrote in message >> >> news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... >> >> > look like it still doesnt work for imported data. what shld i do >> >> > next? >> >> > pls >> >> > help. >> >> > >> >> > "jen" wrote: >> >> > >> >> >> thanks jens, i am trying the code now. will keep you update. >> >> >> >> >> >> "Jens" wrote: >> >> >> >> >> >> > 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. >> >> >> > >> >> >> > >> >> >> >> >> >> >> >> >>
but i didnt set any primary key for inserted. [quoted text, click to view] "Uri Dimant" wrote: > 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 > > > "jen" <jen@discussions.microsoft.com> wrote in message > news:333A06C5-DB7A-4665-BDE8-05A8683CFF71@microsoft.com... > > 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. > > > > > >
thanks jens, i am trying the code now. will keep you update. [quoted text, click to view] "Jens" wrote: > 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. >
no worries, field1 and field2 are not null fields. [quoted text, click to view] "Uri Dimant" wrote: > 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 > > > > "Jens" <Jens@sqlserver2005.de> wrote in message > news:1140508388.179175.19970@g43g2000cwa.googlegroups.com... > > 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. > > > >
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.
look like it still doesnt work for imported data. what shld i do next? pls help. [quoted text, click to view] "jen" wrote: > thanks jens, i am trying the code now. will keep you update. > > "Jens" wrote: > > > 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. > >
thanks! i am trying it now. [quoted text, click to view] "Uri Dimant" wrote: > 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 > > > > "jen" <jen@discussions.microsoft.com> wrote in message > news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... > > look like it still doesnt work for imported data. what shld i do next? pls > > help. > > > > "jen" wrote: > > > >> thanks jens, i am trying the code now. will keep you update. > >> > >> "Jens" wrote: > >> > >> > 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. > >> > > >> > > >
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] "Uri Dimant" wrote: > 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 > > > > "jen" <jen@discussions.microsoft.com> wrote in message > news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... > > look like it still doesnt work for imported data. what shld i do next? pls > > help. > > > > "jen" wrote: > > > >> thanks jens, i am trying the code now. will keep you update. > >> > >> "Jens" wrote: > >> > >> > 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. > >> > > >> > > >
i know this sounds stupid but i really don't understand. [quoted text, click to view] "Uri Dimant" wrote: > jen > > sorry, i don't understand the portion on the insert values of a, b, c, and > > t. > > Its sample data. Please post your DDL + sample data+ expected result? > > > > > > "jen" <jen@discussions.microsoft.com> wrote in message > news:3F41262E-47EF-46D8-AEA9-15650CBA0257@microsoft.com... > > sorry, i don't understand the portion on the insert values of a, b, c, and > > t. > > can advise on that? thanks. > > > > "Uri Dimant" wrote: > > > >> 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 > >> > >> > >> > >> "jen" <jen@discussions.microsoft.com> wrote in message > >> news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... > >> > look like it still doesnt work for imported data. what shld i do next? > >> > pls > >> > help. > >> > > >> > "jen" wrote: > >> > > >> >> thanks jens, i am trying the code now. will keep you update. > >> >> > >> >> "Jens" wrote: > >> >> > >> >> > 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. > >> >> > > >> >> > > >> > >> > >> > >
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] "Uri Dimant" wrote: > What don't you understand? > > "jen" <jen@discussions.microsoft.com> wrote in message > news:112816B1-4FF2-4385-AB8B-848522D69797@microsoft.com... > >i know this sounds stupid but i really don't understand. > > > > "Uri Dimant" wrote: > > > >> jen > >> > sorry, i don't understand the portion on the insert values of a, b, c, > >> > and > >> > t. > >> > >> Its sample data. Please post your DDL + sample data+ expected result? > >> > >> > >> > >> > >> > >> "jen" <jen@discussions.microsoft.com> wrote in message > >> news:3F41262E-47EF-46D8-AEA9-15650CBA0257@microsoft.com... > >> > sorry, i don't understand the portion on the insert values of a, b, c, > >> > and > >> > t. > >> > can advise on that? thanks. > >> > > >> > "Uri Dimant" wrote: > >> > > >> >> 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 > >> >> > >> >> > >> >> > >> >> "jen" <jen@discussions.microsoft.com> wrote in message > >> >> news:104CEE26-9BF7-432D-91C8-920C26C48DC0@microsoft.com... > >> >> > look like it still doesnt work for imported data. what shld i do > >> >> > next? > >> >> > pls > >> >> > help. > >> >> > > >> >> > "jen" wrote: > >> >> > > >> >> >> thanks jens, i am trying the code now. will keep you update. > >> >> >> > >> >> >> "Jens" wrote: > >> >> >> > >> >> >> > 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. > >> >> >> > > >> >> >> > > >> >> > >> >> > >> >> > >> > >> > >> > >
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 ---
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] "ML" wrote: > 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 > > ---
Don't see what you're looking for? Try a search.
|