pass table variable to stored procedure

  • Hello All:

    I would like to pass a variable to a stored procedure,

    that would be a piece of a concatenated string to make up

    the table name.  The code I have so far is below.  The

    problem I'm having is in created the concatenated string

    (tbl??Product??final) for the table name and adding it to

    the SQL statement.  Can this be done? 

    CREATE PROCEDURE product

    (

    @Product varchar(10),

    @TestPlan varchar(30)

    )

    AS

    SELECT * FROM  tbl??Product??final

    WHERE strTestPlan =  @TestPlan

    GO

    Any help is appreciated.

    Thanks,

    Joe

  • hey joe,

    you would have to resort to dynamic SQL to be able to do this.

    so your proc would look something like this:

    CREATE PROCEDURE dbo.usp_product

    (

      @Product VARCHAR(10),

      @TestPlan VARCHAR(30)

    )

    AS

    BEGIN

      DECLARE @sqlStmt AS NVARCHAR(500)

      SET @sqlStmt = N'SELECT * FROM tbl' + CAST( @Product AS NVARCHAR(10) ) + N'final WHERE strTestPlan = @TestPlan'

      EXEC sp_executesql @sqlStmt, N'@TestPlan VARCHAR(30)', @TestPlan

    END

    JP

Viewing 2 posts - 1 through 1 (of 1 total)

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