Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine name of current DB

Status
Not open for further replies.

gny

Programmer
Jun 3, 2001
116
SE
Hi!
I need to pass the name of the DB as a parameter from a stored proc to another, but for some reason I can't get it to work.

When I run the code below, the string 'db_name' is passed, not the database name. It seems SQL server converts
Code:
db_name
to 'db_name'. Any ideas?

Code:
EXEC dbo.usp_Logout db_name

When I try to print
Code:
db_name
instead of passing it, I get the following error message:

"The name 'db_name' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted."
 
If i'm not mistaken, this might work:

declare @str varchar(300)
set @str = db_name()
exec dbo.usp_Logout @str

Hope this helps
LFCfan
 
Firstly, db_name is a function so you need to include parentheses after it:

Code:
PRINT db_name()

To send it as a parameter you need to assign it to a variable first:

Code:
DECLARE @db sysname
SET @db = db_name()
EXEC usp_logout @db
--James
 
Thanks alot guys, it's working now. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top