By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

Variable Database Name in Stored Procedures

P: 12
I am trying to create a stored procedure that runs a smaller query first to determine the database to run the second query on. Below is a sample query that is similar to what I would be doing. I'm not interested in creating new Stored Procedures as their are hundreds that are like this so I don't want to write new statements like...

EXEC( "Select * from " + @TableName + " where " + CAST(@Param1 as varchar) +
" = " + @Param2
GO

as that would be too many queries to update. Any thoughts?

Thanks


CREATE PROCEDURE dbo.stored_procedure_name
AS

DECLARE @database varchar(50)
SET @database = (SELECT * FROM smc_web.currentDatabase)

Begin

set nocount on
select * from (@database).dbo.table_name

end
GO
Jun 27 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.