Solved

running a complex sql with begin tran commit tran

Posted on 2011-09-15
3
482 Views
Last Modified: 2012-05-12
I have a complex sql that has several steps to eventually update some records in a table. To identify the records it pulls some records in three temp tables (#Priority, #pi, #he) and then finds distinct common rows by joining those three temp tables into a fourth temp table #cross, then fires an update  and a delete command which looks like the code copied below, and then it uses cursors to update some columns in another table.

UPDATE OMC
		SET SCID = c.PChart,
			SCProductID = 9,
			SCHREID = c.HREID,
			ETypeID = 2,
			ChangeID = System_User,
			ChangeDate = getdate()	
		FROM dbo.OMChart OMC
		JOIN #cross c
		ON   OMC.MCID = c.HDC
		WHERE SCID is null


DELETE FROM OMChart
		WHERE MCID IN (SELECT mc.MCID
									FROM dbo.OMChart mc
									JOIN #cross c
									  ON mc.MCID = c.PChart
									WHERE  mc.SCID IS NULL)

Open in new window



My question is that will this sql that involves all these temp tables in memory and cursors run with begin tran and commit tran. I want to see how many rows are inserted in the temp tables and how many rows are updated in each step before i commit the tran.

Thanks,


0
Comment
Question by:patd1
  • 2
3 Comments
 
LVL 25

Expert Comment

by:jogos
Comment Utility
temp tables can be used within begin/commit tran

How much records are affected in your last statement you can by using the @@ROWCOUNT directly after that statement. cfr http://msdn.microsoft.com/en-us/library/ms187316(v=SQL.110).aspx
0
 

Author Comment

by:patd1
Comment Utility
Thanks jogos. How about cursors? can cursors be used with begin/commit tran.
0
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
Comment Utility
within  a transaction mus just be vallid sql

but off-topic  try to avoid using a cursor and especialy when nested, it's slow and gets slower the more records gets into your tables
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now