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
 Stored Procedure question

Author  Topic 

BigSam
Starting Member

30 Posts

Posted - 2006-04-06 : 16:18:26
I want to pass a database_name as a parm to a stored procedure?

I tried
Use @Database
and
Select fileds from @Database

These don't work. Any suggestion to work around this?


FYI: I'm trying to worm my way down through user databases to create DBCC IndexDefrag statements.

Thanks,

BigSam

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-04-06 : 16:50:34
Did u try using dynamic SQL?

eg.
Exec ('Select * from ' + @Database + '.dbo.tablename')

Srinika
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2006-04-06 : 16:58:05
You cannot issue a USE statement within a stored procedure. You have to use Srinika's method of using the database name to qualify the table name.

---------------------------
EmeraldCityDomains.com
Go to Top of Page

BigSam
Starting Member

30 Posts

Posted - 2006-04-06 : 17:06:22
I didn't try the dynamic statement. I probably should have given more details regarding the query. I want to use a cursor with the select statement:
declare c1 cursor local static for
select name
from sysindexes -- where sysindexes in in the @Database
where id = @objid -- @objid is another parm contaning the Table ID
and indid > 0
and indid < 255
and (status & 64)=0
order by name
open c1
fetch c1 into @Index_Name
Go to Top of Page
   

- Advertisement -