How does a SQL Script Execute

  • I have a project that I have inherited and the previous caretaker has made a SQL Query that should be used when updating an old system to a new, and changed, system. So the SQL Query contains adding columns, indexes, etc.

    My question is do an SQL Query execute sequentially, from top to bottom?

    If so, then the changes need to be added at the end of the query not, as they have done it here at the beginning.
    The query has all the latest changes first in the query!

    //Anders

  • Not sure I understand your question.

    A script of multiple queries will execute sequentially.

    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
  • Although you have answered my question, I will try to explain why I ask.

    As I said there is a SQL Query that contains all the changes to the database structure that has occurred during it life time. This query has the first change last in the file and the last change at the top of the file.
    E. g. the code to add an index is before adding the column...

    After getting "some" errors when running the query I started to wonder about the execution of a query,...

    As a beginner I thought that the execution was  sequential, top to bottom. Which you now have confirmed.
    So I need to reverse the order of the query

    BTW, by query I mean the actual file that you run. In this file there are many actual queries.

    Thank you very much for your answer
    // Anders

  • What was confusing was your use of 'query'. A script (multiple queries) executes top to bottom, a single query doesn't.

    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
  • OK. So the file is called a script and can contain multiple queries!

    Thanks!

  • If the script is all "backwards" as you report, it probably isn't checking to see if things exist before trying to create them either.  You should check if a table exists before adding a column to it, and check if the column already exists.

    I'm guessing your previous user kept the same script and added new things as they came up.  Need a new index?  OK, add it at the top of the script, and then run only that portion of the script.

  • That is a good idea!
    There is a lot of changes I need to do to make this work "perfectly"

    Thanks for the suggestion!

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply