views:

87

answers:

6

I am just wondering if it is possible to call multiple SQL scripts from a single startup.sql script.

i.e.

StartUp.sql calls:

CreateDatabase.sql CreateStoreProcedureFirst.sql CreateStoreProcedureSecond.sql InsertDummyValues.sql otherscripts.sql... etc

At the moment I am loading and running each file one at a time.. there are more scripts I run also and sometimes miss a script or do it in the wrong order!

Hope this meakes sense

Thanks

+1  A: 

Number them and use a tool like http://code.google.com/p/simplescriptrunner/ or http://code.google.com/p/tarantino/

mcintyre321
A: 

This tip may help you out:

http://www.devx.com/tips/Tip/15132

Alex LE
returned the following error: Msg 15281, Level 16, State 1, Procedure xp_cmdshell, Line 1SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', see "Surface Area Configuration" in SQL Server Books Online.Dont really want to play with the security settings so will leave as is. Thanks
Belliez
xp_cmdshell doesn't address the poster's requirement, and is a security risk
onupdatecascade
In the example xp_cmdshell is used to perform a command a la C's system("") function, to execute the script.
Alex LE
A: 

Both Oracle and MySQL have command line tools and from experience i know you can run them thus

mysql database_name < yoursql.sql > output.tab

however this means running from the CLI not from an original SQL statement so may not be what you are looking for. I don't believe you can make calls to the system from MySQL

PurplePilot
A: 

mcintyre321's links look handy...

I used powershell to do something similar. I'd name all of my setup scripts with a prefix and a 1.x in whatever order they needed to be compiled.

I then named all my teardown scripts 3.x in the proper order.

The command in cmd window:

PS builddir:\> .\buildsql.ps1 -currentbuilddir "C:\Documents and Settings\SGreene\My Documents\svn\Ticketing" -buildfile "sqlbuild.sql" -teardownfile
"teardown.sql"

The powershell script (buildsql.ps1)

param($currentbuilddir,$buildfile1,$teardownfile)

new-psdrive -name builddir -PSProvider filesystem -Root (resolve-path $currentbuilddir)

cd builddir:

rm $buildfile1
rm $teardownfile


Get-item COM_ENCRYPT_1* | ForEAch-object {cat $_ >> $buildfile1; "GO --SYSTEM INSERTED GO--------------" >> $buildfile1} 

Get-item COM_ENCRYPT_3* | ForEAch-object {cat $_ >> $teardownfile; "GO --SYSTEM INSERTED GO------------" >> $teardownfile} 

My first time doing this, but it seemed to work OK.

Sam
+1  A: 

Yes - you can use SQLCMD (SQL Server 2005 or later) to make T-SQL scripts a little more flexible, with the equivalent of includes and basic variables.

The SQLCMD command for include is :r as in:

:r c:\someFolder\script1.sql
:r c:\someFolder\script2.sql

etc.

See http://www.mssqltips.com/tip.asp?tip=1543

onupdatecascade
worked perfectly, thank you
Belliez
A: 

Have you used or heard of dynamic sql? This is how i would do it...

DECLARE @SQL1PROCEDURE nvarchar(4000) DECLARE @SQL2PROCEDURE nvarchar(4000)

SET @SQL1Procedure = ' CREATE PROC sp1 AS BEGIN blah blah blah END '

SET @SQL2Procedure = ' INSERT DUMMY TABLE (FIELD1,FIELD2) VALUES (VALUE 1,VALUE2)'

EXEC @SQL1Procedure EXEC @SQL2Procedure

Kyle