Wrox Home  
Search P2P Archive for: Go

  Return to Index  

sql_language thread: How to refernce a column name froma variable?


Message #1 by "Andrew Haslett" <scooter@p...> on Wed, 18 Apr 2001 01:15:26
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: I have a stored procedure which accepts a parameter for the column name.
: ie Create Proc sp_addtoleaderboard @round int, @score int
:
: The sp updates a record in the leaderboard table and adds the score
: (@score) to the specified round (@round) depending upon what round it is
: (eg round 1, 2, 3, 4 etc).
:
: So to update the record i figured (excuse the coding as Ive just included
: the important bits):
: UPDATE leaderboard SET (@round) VALUES (@score)
:
: However 'SET @round' is obviously tryign to set the actual variable value
: to the score rather than the field that the variable is referring to.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Build a string in your proc, and then Exec() it:

Create sp_TestProc as

    @round varchar(x),
    @score varchar(x)

AS

    Declare @strSQL varchar(100)

    @strSQL = "UPDATE leaderboard SET (' + @round + ') VALUES (' @score ')'
    Exec(@strSQL)

HTH

Cheers
Ken



  Return to Index