Doubt on Dynamic SQL

  • 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.

  • 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_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.

    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/

  • 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.

  • 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_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.

    A loop and sp_ExecuteSQL wouldn't do such a thing. Please post the code you're using.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • post the code...ideally it should not

  • 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.

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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?

  • 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