Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. T-SQL Store Procedure Table Variable

T-SQL Store Procedure Table Variable

Scheduled Pinned Locked Moved Database
helpdatabasesql-serversysadmintutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joe Scuds
    wrote on last edited by
    #1

    Hi, Running SQL server 2000 and working on a store procedure. Basically, I need to get the tbPayRate id in order to insert into another table but having several payrate tables for different tasks and it means @table varies. But got an error that I must declare the variable '@table'. Does anyone know how to get round this issue? DECLARE @table varchar(30) SET @table='tbPayRate' SELECT MAX(id) AS id FROM @table WHERE employee=5 Thanks, Joe

    Y J 2 Replies Last reply
    0
    • J Joe Scuds

      Hi, Running SQL server 2000 and working on a store procedure. Basically, I need to get the tbPayRate id in order to insert into another table but having several payrate tables for different tasks and it means @table varies. But got an error that I must declare the variable '@table'. Does anyone know how to get round this issue? DECLARE @table varchar(30) SET @table='tbPayRate' SELECT MAX(id) AS id FROM @table WHERE employee=5 Thanks, Joe

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      unfortunately you can not use variables for table names in sql statement. The work around is as follows

      declare @sql varchar(255)
      decalre @table varchar(30)

      set @table = 'tbPayRate'
      set @sql = 'SELECT MAX(id) AS id FROM ' + @table + 'WHERE employee=5'

      Now that you constructed your sql statement dynamically, you can execute it

      Exec(@sql)

      will do it

      Yusuf May I help you?

      1 Reply Last reply
      0
      • J Joe Scuds

        Hi, Running SQL server 2000 and working on a store procedure. Basically, I need to get the tbPayRate id in order to insert into another table but having several payrate tables for different tasks and it means @table varies. But got an error that I must declare the variable '@table'. Does anyone know how to get round this issue? DECLARE @table varchar(30) SET @table='tbPayRate' SELECT MAX(id) AS id FROM @table WHERE employee=5 Thanks, Joe

        J Offline
        J Offline
        Jondo24shoots
        wrote on last edited by
        #3

        You must use a schema or database name to differentiate the different table E.g schemaname.tbPayRate or Databasename.SchemaName.Tablename you First declare a variable and set the what u r select to the value of the variable as shown below Declare @tbPayRateID varchar(50) set @tbPayRateID = (Select MAX(id) AS ID FROM schemaname.tbPayRate Where Employee = 5 Try this out

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups