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)
 Parameter usage in a stored procedure

Author  Topic 

istock1
Starting Member

4 Posts

Posted - 2008-01-08 : 15:10:03
All,

I have recieved alot of help here in the past few days (thanks Spirit), but I have one more questions.

I would like to know if there is a way to use a stored procedure parameter in a database name. For example, I am trying to do something like this:

CREATE PROCEDURE spTest (@dbname VARCHAR(50))
AS
SELECT *
FROM @dbname.dbo.sysindexes

But I get an "incorrect syntax near '.'" error. Can anyone help me?

Thanks!

dinub
Starting Member

3 Posts

Posted - 2008-01-09 : 00:26:18
Try this
declare @sqlstmt varchar(100)
set @sqlstmt=@dbname+'.dbo.sysindexes'
exec (@sqlstmt)

Dinu
Go to Top of Page

dinub
Starting Member

3 Posts

Posted - 2008-01-09 : 00:30:22
It's better now

declare @sqlstmt varchar(100)
set @sqlstmt='select * from '+@dbname+'.dbo.sysindexes'
exec (@sqlstmt)

Dinu
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-09 : 02:22:14
quote:
Originally posted by istock1

All,

I have recieved alot of help here in the past few days (thanks Spirit), but I have one more questions.

I would like to know if there is a way to use a stored procedure parameter in a database name. For example, I am trying to do something like this:

CREATE PROCEDURE spTest (@dbname VARCHAR(50))
AS
SELECT *
FROM @dbname.dbo.sysindexes

But I get an "incorrect syntax near '.'" error. Can anyone help me?

Thanks!


Make sure you read this fully
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -