Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to have tablename as a parameter in a query!!!

kapiloo7kapiloo7 Member Posts: 45
Hi,
I have an interview question.

how can we have a parameter for table name in an SQL query. so that the table name will be decided at run time.

this can be done in oracle by lexical parameter. how to do it in SQL server.

plz help me.

thanks

Comments

  • sumit_chauhansumit_chauhan Member Posts: 4
    Hi

    Create stored procedure, pass parameter as Input type and use dynamic query to build the string and execute it. For ex


    CREATE PROCEDUER Proc_Name
    @tblName NVARCHAR(100)
    AS
    Begin
    Declare @Str Nvrarchar(100)

    Set @Str = 'Select * from ' + @tblName

    exec(@Str)
    End

    to execute the sp : exec proc_name(tblname)

    Hope this will help u a lot

    rahulchauhanmail@rediffmail.com





    : Hi,
    : I have an interview question.
    :
    : how can we have a parameter for table name in an SQL query. so that the table name will be decided at run time.
    :
    : this can be done in oracle by lexical parameter. how to do it in SQL server.
    :
    : plz help me.
    :
    : thanks
    :

Sign In or Register to comment.