Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Scripts within scripts

Author  Topic 

Dev Null
Starting Member

39 Posts

Posted - 2005-11-15 : 01:13:08
I'm sure there must be a simple solution to this, but there are so many ways to describe it that I can't seem to find how to search for the answer in the help or the archives here...

I'm running an sql script via osql, and I'd like to include the content of another script from within the first; can anyone tell me how?

Basically, I have 4 databases which are set up _almost_ identically. I have one script which contains everything that they have in common and I'd like to run it in each of the 4 databases. I could do this easily enough with 4 different osql calls, but then the caller has to know the database names, which I'd rather avoid. Instead I'd like a single script which did something like:

use db1
go
run common_install.sql
run db1_install.sql
go
use db2
go
run common_install.sql
run db2_install.sql
...

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-11-15 : 01:16:09
You could have an osql script that calls the other four osql scripts. The user would then just have to call it. Why are you doing this through osql, and who are the users doing this?

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

Dev Null
Starting Member

39 Posts

Posted - 2005-11-15 : 19:19:29
quote:
Originally posted by derrickleggett

You could have an osql script that calls the other four osql scripts. The user would then just have to call it. Why are you doing this through osql, and who are the users doing this?



Thats exactly what I want to do, but I don't know how; how do I call an osql script from inside an osql script? I'm sure there must be a fairly simple way, but its one of those situations where the language is so overloaded that its difficult to find anything to search for that doesn't appear a million other times meaning something completely different...

I'm doing it through osql because its part of a product installation, and osql is easy for InstallShield to call without having to know much about the internals of the system. The "user" is really InstallShield, but the human using InstallShield has no idea what the database looks like... and shouldn't have to.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-11-15 : 19:22:30
Use the -d switch of osql.exe to use a different database that way you don't need the USE statement in the file.

osql.exe -Sserver1 -E -iC:\SomeScript.sql -dDB1 -oC:\SomeOutputFile.txt
osql.exe -Sserver1 -E -iC:\SomeScript.sql -dDB2 -oC:\SomeOutputFile.txt
osql.exe -Sserver1 -E -iC:\SomeScript.sql -dDB3 -oC:\SomeOutputFile.txt
osql.exe -Sserver1 -E -iC:\SomeScript.sql -dDB4 -oC:\SomeOutputFile.txt

Tara Kizer
aka tduggan
Go to Top of Page

Dev Null
Starting Member

39 Posts

Posted - 2005-11-15 : 22:42:16
quote:
Originally posted by tkizer

Use the -d switch of osql.exe to use a different database that way you don't need the USE statement in the file.

I'd thought of that, but it means that either the calling app has to know the names of my databases - which I'd rather avoid just to keep my interfaces clean - or else I have to wrap the calls in a batch file or some such. That works, and I'll use it if I have to, but it ends up looking a bit of a mess (as most batch files do, in my experience) and I think it'd be an easier read and more maintainable if I could call the one sql script directly from inside the other.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-11-16 : 12:42:54
You can do that via xp_cmdshell.

Tara Kizer
aka tduggan
Go to Top of Page

Dev Null
Starting Member

39 Posts

Posted - 2005-11-16 : 18:32:45
Brilliant! Thanks for that.
Go to Top of Page
   

- Advertisement -