April 7, 2014 at 1:56 pm
I have a set of 5 queries as 5 rows in a table. I am looping 1 by 1 and execute the queries, 5 in all, using sp_exectuesql
what is happening now is .. one query executes and before it completes, the loop goes to the next query and starts running.. It is causing problems like blocking, incorrect results etc.
How this can be avoided so that one query is executed only after the previous quey completes.. like in successsion 1 then 2 then 3 then 4 then 5.
April 7, 2014 at 2:42 pm
balasach82 (4/7/2014)
I have a set of 5 queries as 5 rows in a table. I am looping 1 by 1 and execute the queries, 5 in all, using sp_exectuesqlwhat is happening now is .. one query executes and before it completes, the loop goes to the next query and starts running.. It is causing problems like blocking, incorrect results etc.
How this can be avoided so that one query is executed only after the previous quey completes.. like in successsion 1 then 2 then 3 then 4 then 5.
Why are you storing sql in a table?
I take it you have these queries stored and then you create a cursor to pull all the information and then execute them one at a time? What are you doing with the results? Without more details it is difficult to guess what you are trying to do. However, I am pretty sure we can come up with a better approach.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
April 7, 2014 at 3:47 pm
Since we can't see what you see, hard to say. However, since sp_exectuesql is a synchronous call, I would be curious to know how another one can start executing if the first one is still running.
April 7, 2014 at 4:27 pm
balasach82 (4/7/2014)
I have a set of 5 queries as 5 rows in a table. I am looping 1 by 1 and execute the queries, 5 in all, using sp_exectuesqlwhat is happening now is .. one query executes and before it completes, the loop goes to the next query and starts running.. It is causing problems like blocking, incorrect results etc.
A loop and sp_ExecuteSQL wouldn't do such a thing. Please post the code you're using.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 7, 2014 at 6:09 pm
post the code...ideally it should not
April 8, 2014 at 12:55 am
I am using cursor for the loop. It is an rebuild operation query. I am also testing the same logic for taking backup/restore, file cleanup from folders.
I faced this error--
Could not proceed with index DDL operation on table 'some table' because it conflicts with another concurrent operation that is already in progress on the object. The concurrent operation could be an online index operation on the same object or another concurrent operation that moves index pages like DBCC SHRINKFILE.
April 8, 2014 at 1:01 am
There's another session from somewhere else which is causing this. Identify where that other session is coming from.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
April 8, 2014 at 1:31 am
Awesome. A job ws running which was running the query. Thanks for pointing to the problem.
So, its 100% sure that an sp_execsql does not pass control untill the query is completed?
April 8, 2014 at 4:53 am
balasach82 (4/8/2014)
Awesome. A job ws running which was running the query. Thanks for pointing to the problem.So, its 100% sure that an sp_execsql does not pass control untill the query is completed?
Yes.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply