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.
| Author |
Topic |
|
yossibaram
Yak Posting Veteran
82 Posts |
Posted - 2002-12-04 : 07:11:49
|
| Hi,I need to create a log table that should combine fields from 2 tables.For every action of Insert to table_1 a trigger will be called to fill the Log table with the records that were added.The same routine is done for table_2.1.with the folloing SP I do insert of records from LanTable to table_1:CREATE PROCEDURE Lan_Insert_Productsas insert into tabel_1 (Product_Num,Sticker_type)Select LanTable.ProductNum,LanTable.StickerType From LanTable left Join tabel_1 p On LanTable.ProductNum=p.Product_Num where p.Product_Num is null tabel_1:Product_Num Sticker_type 11111111111 22 22222222222 33 55555555555 12 etc..The trigger inserts records into the Log table.LogTable:Product_Num Sticker_type Second_Num 11111111111 22 Null22222222222 33 Null55555555555 12 Null2.I am doing the same thing for table_2table_2:Product_Num Second_Num11111111111 101010101022222222222 202020202055555555555 5050505050The result of all my actions is to create the following Log table:Product_Num Sticker_type Second_Num 11111111111 22 101010101022222222222 33 202020202055555555555 12 3030303030After running the following trigger:CREATE TRIGGER ocdb_history_trigger_SecProduct ON table_2FOR INSERT, UPDATE, DELETENOT FOR REPLICATIONASDeclare @Sec_Products varchar(200)select @Sec_Products=deleted.SecondNum from deletedUPDATE LogTableSET Second_Num = @Sec_ProductsFROM deleted,LogTableWHERE LogTable.Product_Num = deleted.Product_NumI get the following LogTable:Product_Num Sticker_type Second_Num 11111111111 22 303030303022222222222 33 303030303055555555555 12 3030303030I need the Second_Num to be according to table_2What did i do wrong?ThanksYossi |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-12-04 : 07:26:48
|
| Declare @Sec_Products varchar(200) select @Sec_Products=deleted.SecondNum from deleted this will just get the one value from deleted - the last one it accesses in the table.It then updates all entries to that value.you need something more likeUPDATE LogTable SET Second_Num = deleted.SecondNum FROM deleted,LogTable WHERE LogTable.Product_Num = deleted.Product_Num ==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
yossibaram
Yak Posting Veteran
82 Posts |
Posted - 2002-12-04 : 07:46:18
|
quote: Declare @Sec_Products varchar(200) select @Sec_Products=deleted.SecondNum from deleted this will just get the one value from deleted - the last one it accesses in the table.It then updates all entries to that value.you need something more likeUPDATE LogTable SET Second_Num = deleted.SecondNum FROM deleted,LogTable WHERE LogTable.Product_Num = deleted.Product_Num ==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
Tahnks alot, It worksYossi |
 |
|
|
|
|
|