Sunday, March 25, 2012

Calling scripts

I was wondering if it is possible to create a script to call other scripts. I have created the database scripts and wanted to create a script that would call each one, e.g. tables, logins, etc. This will enable us to comment out any that are not required for certain installations. I then just need to be able to run the one script. Could you let me know how this is done in SQL Server.

Thanks

You can take help of OSQL in this case to call those desired scripts and wrap all into a batch file or run on the server on adhoc basis.

Refer to the books online about OSQL in this case.

|||

Hi Sarah,

Let's assume you have a script 1.sql from which you want to call another script, called 2.sql. This can be achieved by calling sqlcmd.exe from 1.sql using xp_cmdshell:

-- 1.sql

exec xp_cmdshell 'sqlcmd.exe -i $(other_script)'

-- 2.sql

print 'Hello from 2.sql'

You would execute 1.sql thus:

sqlcmd -i 1.sql -v other_script="c:\test\2.sql"

Note that I'm using SQLCMD variable substitution to pass script name to 1.sql. If the name of your script never changes, you simply hard-code it in 1.sql.

Also note that for this solution to work sqlcmd.exe must be in your PATH (or else you need to specify full path in 1.sql), and xp_cmdshell must be enabled (which can be done from SQL Server Surface Area Configuration tool)

Artur Laksberg - MSFT

|||

I developed a 'driving' script that would read the folders after a dump from VSS, and then execute what ever script files were found in the folders (db, sproc, function, trigger, data). And there is an allowance for setting priorities when necessary, and dealing with object dependency issues.

Contact me offlist if you are interested in viewing the entire script (I'm not posting it since it is rather lengthly.)

Regards,

Arnie

No comments:

Post a Comment