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 |
madhulatha_b
Starting Member
22 Posts |
Posted - 2006-06-15 : 00:09:27
|
I have written a stored procedure to delete old records from multiple tables in database by using TransactionsBy mistake I forgot one child table. With out deleting the records from that table I tried to remove records from master.So While executing the query I have got error (foreign key constraint). After that also records got deleted from other tablesMy question here is when using Begin trans and Commit trans, if error comes it should not commit the results .. but in my case even though the error came it has deleted records from all the tables.My stored procedure is belowI have got problem while deleting the records from ORDER table. After that it has executed the next delete statementsPlease suggest if there is any problem with my stored procedure*******************************************************************Create Procedure DeleteOldProjects asDECLARE @treenode char(255)Create table #tmp_treenode (got_id uniqueidentifier, ID_ varchar(255))Create table #CODE (got_id uniqueidentifier)DECLARE Treenode_Cur CURSOR FOR SELECT treenode FROM nodeattributes1 where datediff(day,project_Releasedate,getdate()) > 365 OR project_releasedate is NULLOPEN Treenode_CurFETCH NEXT FROM Treenode_Cur INTO @treenodeWHILE(@@FETCH_STATUS=0)BEGINBegin Tran Tran1 INSERT INTO #tmp_treenode SELECT got_id,ID_ FROM treenode t2 where t2.got_id = @treenode INSERT INTO #code SELECT got_id FROM treenode t2 where t2.got_id = @treenode WHILE(SELECT COUNT(*) FROM treenode t2 WHERE t2.got_parent in (select got_id from #code))>0 BEGIN INSERT INTO #tmp_treenode SELECT got_id,ID_ FROM treenode t2 WHERE t2.got_parent in (select got_id from #code) SELECT got_id into #code1 from treenode t2 WHERE t2.got_parent in (select got_id from #code) DELETE FROM #code INSERT INTO #code SELECT * FROM #code1 --SELECT * INTO #code FROM #code1 DROP TABLE #code1 END Commit Tran Tran1FETCH NEXT FROM Treenode_Cur into @treenodeENDBegin Tran Tran2SELECT got_id into #tmp_order FROM got..orders WHERE treenode in (SELECT got_id FROM #tmp_treenode) DELETE ProjectMembers WHERE treenode in (select got_id from #tmp_treenode) DELETE cbschinanodeinfo WHERE treenode in (select got_id from #tmp_treenode) DELETE Got..Orders WHERE Treenode in (select got_id from #tmp_treenode) DELETE NonOrderableProductRepository WHERE Parent_ID in (select ID_ from #tmp_treenode) DELETE NodeAttributes1 WHERE treenode in (select got_id from #tmp_treenode) DELETE TreeNode WHERE Got_id in (select got_id from #tmp_treenode) Commit Tran Tran2DROP TABLE #tmp_treenodeDROP TABLE #codeCLOSE Treenode_CurDEALLOCATE Treenode_CurGO |
|
Kristen
Test
22859 Posts |
Posted - 2006-06-18 : 02:54:25
|
Your problem, as I understand it, is that the first SQL Error (FK constraint violation in your case) will abort the transaction - the rest of the "job" is then running with NO transaction :-( (unless the very next thing is a rollback)We get around this by havingSET XACT_ABORT ONis ALL (absolutely no exceptions!) our SProcs which causes the WHOLE Sproc to abort on the first error.Kristen |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|