Monday, March 26, 2012

question about osql

I have two .sql files (f1.sql and f2.sql) with T-SQL inside them. The last part of the first one is a statement USE MyDatabase. I have a batch file to use Osql to call these two file like
Osql /E /if1.sql
Osql /E /if2.sql
I assume the T-SQL in f2 will be executed against MyDatabse. But I found it is executed against master database, not MyDatabasse unless I move the USE MyDatabase statement from the end of f1.sql to the beginning of f2.sql. Why?
The f2.sql is a static file and the f1.sql is a dynamic one which means I will update MyDatabase dynamically. How do I handle this?
Roy,
When your first OSQL script completed, the batch, the connection (in other
words: everything) was closed. When the second script started, it
reconnected and is using the defaults, in this case 'master' database.
You could do this:
copy f1.sql+f2.sql MyScript.sql
Osql /E /iMyScript.sql
Russell Fields
"Roy" <Roy@.discussions.microsoft.com> wrote in message
news:2621E094-53B8-453E-B9ED-61EEE7439285@.microsoft.com...
> I have two .sql files (f1.sql and f2.sql) with T-SQL inside them. The last
part of the first one is a statement USE MyDatabase. I have a batch file to
use Osql to call these two file like
> Osql /E /if1.sql
> Osql /E /if2.sql
> I assume the T-SQL in f2 will be executed against MyDatabse. But I found
it is executed against master database, not MyDatabasse unless I move the
USE MyDatabase statement from the end of f1.sql to the beginning of f2.sql.
Why?
> The f2.sql is a static file and the f1.sql is a dynamic one which means I
will update MyDatabase dynamically. How do I handle this?

No comments:

Post a Comment