Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
hi every onei have this 2 tables and i want to make cascade UPDATE and cascade DELETE i tried using relations but it failedthen i used triggersthis is the script i an using:============================================================CREATE TABLE Table1 ( TransSN [int] NOT NULL , TransName [nvarchar] (50) NOT NULL , CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED (TransSN) ON [PRIMARY] ) ON [PRIMARY]CREATE TABLE Table2 ( MasterSN [int] NOT NULL , SlaveSN [int] NOT NULL , CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED (MasterSN,SlaveSN) ON [PRIMARY] ) ON [PRIMARY]goCREATE TRIGGER trgUpdateMasterSnInTable2 ON Table1FOR UPDATEASset nocount ondeclare @NewSN int , @OldSN intselect @NewSN = inserted.TransSN from insertedselect @OldSN = deleted.TransSN from deletedupdate Table2 set MasterSN = @NewSNwhere MasterSN = @OldSNupdate Table2 set SlaveSN = @NewSNwhere SlaveSN = @OldSNset nocount offgo==================================================================NOWthis script is working as cascade UPDATE but only for one row updatingmy question is :how can i modify this script to make it work as cascade update FOR EACH ROW ???please help
Kristen
Test
22859 Posts
Posted - 2007-10-28 : 06:22:53
Triggers do not fire for each record, they fire for each statement, so you must write your trigger to handle multiple rows. Probably something like this:
update Uset MasterSN = I.TransSNFROM deleted AS D JOIN inserted AS I ON I.MyPK = D.MyPK JOIN Table2 AS U ON U.MasterSN = D.TransSNupdate Uset SlaveSN = I.TransSNFROM deleted AS D JOIN inserted AS I ON I.MyPK = D.MyPK JOIN Table2 AS U ON U.SlaveSN = D.TransSN
Kristen
mznSQL
Starting Member
2 Posts
Posted - 2007-10-28 : 07:14:32
Dear Kristenthanx alot for your reply and it is realy appreciatedbut the problem is:when you made join between inserted table and deleted table you used (MyPK field) which is invalidi know its a primary key field but in my sample the primary key in Table1 is (TransSN) and this field should be related to both (MasterSN & SlaveSN) in Table2 and this relation should be CASCADE UPDATE & CASCADE DELETEi am tring to attach an image about what i want because i do it in ACCESS but i dont know how to attach imagethank U again & i hope i get what i am looking formazen...
Kristen
Test
22859 Posts
Posted - 2007-10-29 : 05:37:16
You will HAVE to have a unique field (or multi-field combination which is unique) that does NOT change between Old/New data. An IDENTITY column or UNIQUEIDENTITY (GUID) would do for that purpose. (Otherwise there is NO way to associate the Old Row with the New Row in the trigger)Very VERY bad form to change the PK on a record that has dependencies ... so if that is what you are trying to do you'll be finding out that one the hard way!!If you have a Foreign Key Constraint on this relationship you will NOT be able to change the Key Field on the Parent You will have to:Insert duplicate parent record with new Key value (which may cause duplicates in other unique constraints)Update Child record to the new Key valueDelete old Parent recordall of which is a PITA and why people go down the route of using surrogate keys ...Did I say it was a Bummer! already? Kristen