You must Sign In to post a response.
  • Category: SQL Server

    Is it possible to send database name as parameter to Stored procedure ?

    Is it possible to send database name as parameter to Stored procedure ?
  • #730445
    Hi,

    Yes, you can pass database name as parameter to Stored procedure

    Try like this

    Create Proc dbo.MyTestPRoc
    @dbname Varchar(25)
    AS
    USE @dbname

    SELECT * FROM YourTable --Table from your selected database
    GO

    //If you want to pass dbname and also table name
    Create Proc dbo.MyTestPRoc
    @dbname Varchar(25),
    @tblname Varchar(25)
    AS
    USE @dbname

    SELECT * FROM @tblname
    GO

  • #730498
    You can Put the Database or User name in a paramter and can use it as


    USE @DBName
    GO

    DECLARE @AccountName nvarchar(260)
    SET @AccountName = 'DotnetSpider\Anil.Pandey'

    if not exists (select name from syslogins where name = @AccountName and hasaccess = 1 and isntname = 1)
    BEGIN
    EXEC sp_grantlogin @AccountName
    END
    GO

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #730526

    Hai Ultimaterengan,
    Yes, you can pass the database name as the column name from the stored procedure and based on the database name, you an execute your queries.
    You an go through the below link for the workaround:

    http://stackoverflow.com/questions/3943823/sql-server-how-to-get-a-database-name-as-a-parameter-in-a-stored-procedure

    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #730951
    Mahesh Durgam ,

    a USE database statement is not allowed in a procedure, function or trigger. so your answer is totally wrong.



    Thanks & Regards
    G.Renganathan
    Nothing is mine ,Everything is yours!!!

  • #731651
    Hi

    Try this

    Create Proc TestProcedure
    @DataBasename nVarchar(Max)
    AS
    USE @DataBasename

    SQL Server Programmers and Consultants


  • Sign In to post your comments