Creating a database from set of TSQLl files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Annonymous Coward

    Creating a database from set of TSQLl files

    I have written a set of sql files that make my entire database schema. I
    have divided the schema into seperate files mostly for "manageabil ity"
    (for example, I can check error codes as I go along, and terminate the
    process as soon as an error occurs).

    I have several tsql files in a directory, and I want to be able to write
    a batch script (or something similar), which allows me to:

    1). Process each of the individual files (i.e. execute the commands in
    the file)
    2). report succes or failure for the file
    3). Abort the entire process if a command in one of the files failed

    The batch part I can largely figure out myself. So my question basically
    can be simplified as:

    How can I read and execute TSQL commands from a file - AND return an
    success/error code ?
  • Dan Guzman

    #2
    Re: Creating a database from set of TSQLl files

    The batch part I can largely figure out myself. So my question basically
    can be simplified as:
    >
    How can I read and execute TSQL commands from a file - AND return an
    success/error code ?
    You can execute scripts using SQLCMD command-line utility with the -b
    parameter. This will cause the utility to exit with a non-zero return code
    if an error with a severity 10 is raised. For example:

    SQLCMD -E -S MyServer -d MyDatabase -b -i MyScript.sql
    IF NOT %errorlevel% == 0 GOTO :MyErrorLabel

    --
    Hope this helps.

    Dan Guzman
    SQL Server MVP


    "Annonymous Coward" <me@home.comwro te in message
    news:TuqdneB7p6 Ea_P7VRVnytAA@b t.com...
    >I have written a set of sql files that make my entire database schema. I
    >have divided the schema into seperate files mostly for "manageabil ity" (for
    >example, I can check error codes as I go along, and terminate the process
    >as soon as an error occurs).
    >
    I have several tsql files in a directory, and I want to be able to write a
    batch script (or something similar), which allows me to:
    >
    1). Process each of the individual files (i.e. execute the commands in the
    file)
    2). report succes or failure for the file
    3). Abort the entire process if a command in one of the files failed
    >
    The batch part I can largely figure out myself. So my question basically
    can be simplified as:
    >
    How can I read and execute TSQL commands from a file - AND return an
    success/error code ?

    Comment

    Working...