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.
Author |
Topic |
sriksdave
Starting Member
5 Posts |
Posted - 2007-05-07 : 15:52:15
|
Hello GuysI need some quick help hereIam trying to write a sql script and put a USE DBNAMEIF EXISTS ( SELECT 1 from ......)BEGINSQL SCRIPTENDEven though the Select statement doest retrieve any data. The loop is still getting executed.Can you tell me whey its doing that?Thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-05-07 : 15:58:25
|
We need to see your actual code as what you have posted is fine.Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
sriksdave
Starting Member
5 Posts |
Posted - 2007-05-07 : 17:27:47
|
[code]IF EXISTS (SELECT name FROM master..sysdatabases WHERE name = 'DBNAME')BEGINUSE DBNAMEexec sp_addrolemember N'db_owner', N'SupportGrp'END[/code]When i run the script in query Analyser it gives me "Could not locate entry in sysdatabases for database 'DBNAME'. No entry found with that name."why is it even running the inside script if the db doesnt exist? |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2007-05-07 : 17:59:07
|
You can't do that. SQL Server is checking for the database in the USE prior to even running the IF. I believe you could do this instead:EXEC DBNAME.dbo.sp...Tara Kizerhttp://weblogs.sqlteam.com/tarad/ |
 |
|
mattyblah
Starting Member
49 Posts |
Posted - 2007-05-07 : 18:12:35
|
USE is not T-SQL. It's a query analyzer command, I believe. |
 |
|
|
|
|