October 17, 2006 at 2:43 pm
Hi,
I'd like to know how people out there handle the following:
USE myTESTdb
ELSE
GOTO QuitWithRollback
ROLLBACK TRANSACTION
October 17, 2006 at 3:47 pm
Make your "... some more tsql ..." a SP in myTESTdb and run dynamic SQL:
EXEC('USE myTESTdb
_____________
Code for TallyGenerator
October 17, 2006 at 5:54 pm
But I would do just this:
IF EXISTS(SELECT * FROM MASTER..SYSDATABASES WHERE NAME = 'myTESTdb')
EXEC ('exec myTESTdb.dbo.SomeMoreTsqlProcedure')
_____________
Code for TallyGenerator
October 18, 2006 at 5:43 am
This compiles and executes just fine on my server :
IF EXISTS (SELECT * FROM MASTER..SYSDATABASES WHERE NAME = 'FakeDB')
begin
EXEC FakeDB.dbo.SomeProc
end
October 24, 2006 at 4:34 am
USE myTESTdb
ELSE
GOTO QuitWithRollback
ROLLBACK TRANSACTION
-------------------------------------------------------------------------
Normal chaos will be resumed as soon as possible. :crazy:
October 24, 2006 at 6:17 am
But what happens if myTESTdb doesn't exist? Parsing fails.
I am looking for a solution that jumps to the QuitWithRollback if the database doesn't exist.
Thanks for your suggestion John.
October 24, 2006 at 6:38 am
Can you please tell me why my solution is not acceptable for you needs? As far as I know you have a working solution and I can't offer any more help without more details of what you need to do.
October 24, 2006 at 7:04 am
Your suggestion above does work however it doesn't quite fit into what I am trying to do, so if you can help me further I would very much appreciate it.
Basically I have a number of scripts that need to be applied to all our new servers. Depending on the server function (determined by the instance name) the script will either run as intended or it will simply drop out - I achieve this with a simple IF statement.
My problem comes when I try to run one of my scripts on a server that the script isn't intended for - because the USE statement contains a database name that doesn't exist within the instance so the script fails on parsing. If the database does exist then the script does not fail.
I need to be able to run all of our scripts against any sql instance and for the script to correctly determine if it should run the main contents against the instance. You should also presume the sql instance has no user databases at the time of execution to cover this situation. And also presume the current database focus is MASTER so that a USE has to be initiated so that focus changes to the database we intend to make changes to.
I hope this makes sense. Basically it is a requirement for all our scripts at work. People have got round this in different ways but I would like to know if it is possible to do it with the USE statement as I have mentioned above.
Thanks for your interest and help.
Joe
October 24, 2006 at 7:11 am
Have you considered this one :
For each server
exec sp_msforeachdb 'IF ''?'' IN ('Pubs', 'DBA', 'Production') BEGIN use ? exec ?.dbo.USPDemo END'
next
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply