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
 SQL Server Development (2000)
 Confirming USE statement execution

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-06-02 : 08:10:14
Craig Hartzell writes "I am writing a script to modify 7 different databases. I want users to run this script on their server, to update them. Here is a simplification of the script:

USE db1
DROP table1
CREATE table1...
INSERT INTO table1 values (0,1,0)

USE db2
DROP table1
CREATE table1...
INSERT INTO table1 values (0,0,1)

USE db3
DROP table1
CREATE table1...
INSERT INTO table1 values (1,0,0)

The problem is that some users only have some of the databases used, not all. So if a user has db1 and db3, it is executing the script on db1, then TRYING to go to db2, failing, and then executing the db2 script on db1, incorrectly setting the values.

My question is: how do I confirm that it has switched to a given database after the USE statement is executed?"

macka
Posting Yak Master

162 Posts

Posted - 2003-06-02 : 08:27:56
Think you just need a GO after each batch of commands.

eg.

USE db1
DROP table1
CREATE table1...
INSERT INTO table1 values (0,1,0)
GO

USE db2
DROP table1
CREATE table1...
INSERT INTO table1 values (0,0,1)
GO

USE db3
DROP table1
CREATE table1...
INSERT INTO table1 values (1,0,0)
GO

It will still throw errors for databases not found, but it will complete the batch for the database set before the last go.

You may want to add in a check to see if the database exists on the server before you run the script.

Try

if exists(select * from master..sysdatabases where name = 'pubs')
begin
..your code here for pubs
end

macka.

--
There are only 10 types of people in the world - Those who understand binary, and those who don't.

Edited by - macka on 06/02/2003 08:29:16
Go to Top of Page
   

- Advertisement -