| Author |
Topic |
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-09-04 : 09:19:02
|
| What is the best way to insert data into a table if data does not exist in another table? I was thinking of an IF not Exists statement but I am unsure on how to code it.I am using 3 tables. oelin, OR, and ORH.All three tables contain ord_type,ord_no,item_no,and line#. If a record in the OR table does not exist in the oelin I want to delete it from the OR table and move it to the ORH table. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 09:50:07
|
you can use LEFT JOIN for deleting this:-DELETE oFROM OR oLEFT JOIN oelin oeON oe.PK=o.PKWHERE oe.PK IS NULL then put a delete trigger for putting record in ORHCREATE TRIGGER CaptureHistoryON [OR]AFTER DELETEASINSERT INTO [ORH]SELECT * FROM DELETEDGO |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-09-04 : 10:48:51
|
| The delete did not seem to do the trick. Here is what I used:delete orderrebatefrom orderrebateleft join oeordlin_sql on oeordlin_sql.ord_type = orderrebate.ord_type andoeordlin_sql.ord_no = orderrebate.ord_no and oeordlin_sql.item_no = orderrebate.item_no andoeordlin_sql.line_seq_no = orderrebate.line_seq_nowhere oeordlin_sql.ord_type is null and oeordlin_sql.ord_no is null and oeordlin_sql.item_no is null andoeordlin_sql.line_seq_no is null |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 10:51:37
|
quote: Originally posted by Vack The delete did not seem to do the trick. Here is what I used:delete orderrebatefrom orderrebateleft join oeordlin_sql on oeordlin_sql.ord_type = orderrebate.ord_type andoeordlin_sql.ord_no = orderrebate.ord_no and oeordlin_sql.item_no = orderrebate.item_no andoeordlin_sql.line_seq_no = orderrebate.line_seq_nowhere oeordlin_sql.ord_type is null and oeordlin_sql.ord_no is null and oeordlin_sql.item_no is null andoeordlin_sql.line_seq_no is null
do you mean it didnt delete? |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-09-04 : 10:52:56
|
| I was just about to reply again. The script runs fine, but I get 0 rows affected.So yes, the record did not delete. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 11:00:40
|
quote: Originally posted by Vack I was just about to reply again. The script runs fine, but I get 0 rows affected.So yes, the record did not delete.
that menas you dont have any records in orderrebate that dont exists in oeordlin_sql |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-09-04 : 11:02:19
|
| I just double checked. Order # 612745 does not exist in the oeordlin table. But it does exist in the orderrebate table. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 11:09:40
|
quote: Originally posted by Vack I just double checked. Order # 612745 does not exist in the oeordlin table. But it does exist in the orderrebate table.
what's the relationship b/w two tables? |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2008-09-04 : 11:13:51
|
| I'm sorry you were right. I retested and it did remove the records when they did not exist. Thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-04 : 11:32:39
|
quote: Originally posted by Vack I'm sorry you were right. I retested and it did remove the records when they did not exist. Thank you
you're welcome i also hope trigger also worked great |
 |
|
|
|