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
 General SQL Server Forums
 New to SQL Server Programming
 USE DatabaseName!

Author  Topic 

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-21 : 09:10:25
Sorry if this is too vague!
If we are using the same database; How many times a keyword
USE DATABASENAME
can be used within one script?
Is always good to reinforce the usage of that keyword or not?

I'm working on an old script where I find the person who wrote it, reiterate the key word 3 times within the same script, and I dont see a strong reason why it is sitting there!!

Comments are welcome :)!

--------------------------
Joins are what RDBMS's do for a living

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-21 : 09:31:09
It's probably there because the script was concatnated.

What happens if the database doesn't exist or the user doesn't have permission? Does the script carry on?

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-21 : 09:57:14
Concatenated, maybe Yes. but looking at the flow it is the same database.
This script is a one time run script, it does use only one database and sometimes within it I call some xtended procedures....like:
exec master..xp....
. Anyways, I really don't see any usage for that keyword unless we are switching databases to work on them all.

I agree to Use is on the beginning of the script once, but not more than that. this will answer the question if the databases does not exist.
I didn't not fall into the case of the users' permission yet, but for now it is acting fine because i have full privileges.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-21 : 10:04:30
quote:
Originally posted by xhostx

Concatenated, maybe Yes. but looking at the flow it is the same database.
This script is a one time run script, it does use only one database and sometimes within it I call some xtended procedures....like:
exec master..xp....
. Anyways, I really don't see any usage for that keyword unless we are switching databases to work on them all.

I agree to Use is on the beginning of the script once, but not more than that. this will answer the question if the databases does not exist.
I didn't not fall into the case of the users' permission yet, but for now it is acting fine because i have full privileges.

--------------------------
Joins are what RDBMS's do for a living


its required only one

also there's no necessity for it to be used to switch db context in between for using other db objects in batch.
you can always refer other db objects in the same server using fully referenced name like OtherDb.dbo.Table ,OtherDB.dbo.Procname etc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-21 : 10:06:43
I would say it's dangerous to have it even once.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-21 : 10:08:54
quote:
Originally posted by nigelrivett

I would say it's dangerous to have it even once.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


Thanks Visakh and nigelrivett.
nigelrivett!
Just curious! why once could be dangerous and what is the substitute to it? :) thanks
Thanks

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-21 : 10:10:29
quote:
Originally posted by nigelrivett

I would say it's dangerous to have it even once.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


its required once on the top in some cases

we've had new starters who create object creation scripts and after executing complaining that they cant see the object in the database.
Upon analysis one could see the objects been created in master rather than in required db as they've tendency to connect to server and just run the script without even looking at db context which would be master by default. So in such cases its highly recommended unless you get accustomed to SSMS and realises on db context

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-21 : 10:22:42
quote:
Originally posted by visakh16

quote:
Originally posted by nigelrivett

I would say it's dangerous to have it even once.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.


its required once on the top in some cases

we've had new starters who create object creation scripts and after executing complaining that they cant see the object in the database.
Upon analysis one could see the objects been created in master rather than in required db as they've tendency to connect to server and just run the script without even looking at db context which would be master by default. So in such cases its highly recommended unless you get accustomed to SSMS and realises on db context
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Thank you all.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-08-21 : 11:04:09
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2012-08-21 : 11:30:44
quote:
Originally posted by nigelrivett

I would say it's dangerous to have it even once.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.

+1
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-08-21 : 11:36:17
>> Just curious! why once could be dangerous and what is the substitute to it?

If the use statement fails and is not checked then the script will carry on and do everything in whichever database you happen to be connected to.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

xhostx
Constraint Violating Yak Guru

277 Posts

Posted - 2012-08-21 : 11:52:43
Yes, you're right.
It does make more sense.
Thanks a lot.

--------------------------
Joins are what RDBMS's do for a living
Go to Top of Page
   

- Advertisement -