Variable Database Name in Stored Procedures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssouhrada
    New Member
    • Jun 2008
    • 12

    Variable Database Name in Stored Procedures

    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_proc edure_name
    AS

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

    Begin

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

    end
    GO
Working...