Chosing database name by a parameter

  • I need to run a stored procedure with two parameter:

    spu_testing (ID_Number, DataBase)

    I need what the DataBase changes following the parameter what was sended...

    Example :

    spu_testing (ID_Number, BD_TTT) will execute select * from BD_TTT.dbo.Clients

    spu_testing (ID_Number, BD_WWW) will execute select * from BD_WWW.dbo.Clients

    How I have hundreds of lines in my stored, I just want use IF if there is no another choice.

    How do I make this?

    Thanks in advice.

    Alex

     

  • Why not simply create two different procedures and execute the correct one based on which database you want to use?

  • hi,

    If you know all the possible values of the parameter that will hold the database name you could have a set of IF statements at the beginning of the procedure like this:

    If (@parameter = 'database1') begin

           use datbase1

    end

    else If(@parameter = 'database2') begin

           use database2

    end

    etc etc

    or use a case statement in place of the IF's.

    cheers

    Dave

  • Thanks, Dave.

    This really works well in my case.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply