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. SQL Server Function Dynamic Column

SQL Server Function Dynamic Column

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
2 Posts 2 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.
  • S Offline
    S Offline
    smarttom99
    wrote on last edited by
    #1

    I am trying to creat a function in SQL Server where it returns a the value of a column in a table where the column is passed as a parameter. Below is the function where I want to return the value in the user table for column @field. Thanks for your help. CREATE FUNCTION [dbo].[USER_RETURNCOLUMN] (@userid decimal, @field varchar(200)) RETURNS varchar(2000) AS BEGIN DECLARE @RETURNVALUE varchar(100) SELECT @RETURNVALUE = '' SELECT @RETURNVALUE = @field FROM USER_TABLE WHERE USER_ID = @userid RETURN @RETURNVALUE END

    R 1 Reply Last reply
    0
    • S smarttom99

      I am trying to creat a function in SQL Server where it returns a the value of a column in a table where the column is passed as a parameter. Below is the function where I want to return the value in the user table for column @field. Thanks for your help. CREATE FUNCTION [dbo].[USER_RETURNCOLUMN] (@userid decimal, @field varchar(200)) RETURNS varchar(2000) AS BEGIN DECLARE @RETURNVALUE varchar(100) SELECT @RETURNVALUE = '' SELECT @RETURNVALUE = @field FROM USER_TABLE WHERE USER_ID = @userid RETURN @RETURNVALUE END

      R Offline
      R Offline
      r stropek
      wrote on last edited by
      #2

      If you ask me no chance to do this in a user defined function. You would need dynamic SQL with exec( @stmt ) and that is not allowed in UDFs. Can't you use a stored proc? You can use dynamic SQL there. Regards, Rainer. Rainer Stropek Visit my blog at http://www.cubido.at/rainers

      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