Solved

running a complex sql with begin tran commit tran

Posted on 2011-09-15
3
485 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
ID: 36543168
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
ID: 36543219
Thanks jogos. How about cursors? can cursors be used with begin/commit tran.
0
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 36543424
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

792 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