Read SQL script file name during execution within the script

  • I searched the whole internet, yet nothin' 🙂

    Scripts are currently being registered in a log table within a database using a line such as:

    exec sp_reg_script '12.34_ProductA_Add_Customer_table.sql'

    This means that every script needs to be revisited and have the above line filled in prior to execution.

    I'm trying to find a process that would retrieve the script file name, store it in a variable and then execute a new script registration line, such as

    exec sp_reg_script @script_file_name

    The process would be part of every script file that is run against the database. Any ideas are greatly appreciated. Thank you.

  • Assuming that the script file names are static, please try storing them in a table. Construct the query something like @sql = 'sp_reg_script ' + scriptfilenamefromtable. Then use the exec (@sql) to execute the constructed query

    Pradeep Adiga
    Blog: sqldbadiaries.com
    Twitter: @pradeepadiga

  • Thanks Pradeep. That would not really work, as the script name would have to be 1st) inserted into the table, 2nd) retrieved with a query using WHERE criteria to single out the correct row.

    It would be more difficult than to hardcode the script name in the original document.

Viewing 3 posts - 1 through 2 (of 2 total)

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