SP syntax

  • Hi again.

    With the following syntax in a SP, I get an error that @TableName must be declared:

    =================================

    CREATE PROCEDURE usp_Table_Insert

     @SecondaryKeyValue int

     , @EmpName varchar(150)

     , @PrimaryKeyValue int OUTPUT

     , @ctlFKeyValue int

     , @TableName varchar(50)

     , @PrimaryKeyName varchar(100)

     , @SecondaryKeyName varchar(100)

     

    AS

    IF EXISTS

     ( SELECT

      @PrimaryKeyName  

     FROM @TableName

     WHERE  @SecondaryKeyName = @SecondaryKeyValue

    &nbsp

    RETURN

    =================================

    It looks to me like it's declared. What am I missing?

  • you can't use variable in the select from directly

    you have to do this

    declare

    @sql varchar(1000)

    select @sql = 'SELECT ' + @PrimaryKeyName + ' FROM ' + @TableName + ' WHERE ' + @SecondaryKeyName + ' = ' + @SecondaryKeyValue

    exec (@sql)

  • thanks again!!

    Sam

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

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