﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / passing a column name to function / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sat, 18 May 2013 15:37:55 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: passing a column name to function</title><link>http://www.sqlservercentral.com/Forums/Topic499300-8-1.aspx</link><description>First, the function you have uses a cursor/while loop and it's going to be relatively slow.  See the following for a couple of tips how to do it another way while avoiding some common pitfalls...[url]http://www.sqlservercentral.com/articles/Test+Data/61572/[/url]Second, to make the column a parameter would require the use of Dynamic SQL.  That requires an EXEC in one form or another and only extended stored procedures can be executed with EXEC from within a function.  In English, what you ask cannot be done in a function.</description><pubDate>Mon, 12 May 2008 23:07:10 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>passing a column name to function</title><link>http://www.sqlservercentral.com/Forums/Topic499300-8-1.aspx</link><description>Dear AllI have the following function that the returns the value with comma delimitorex:FormCode          ITem-CodeRS-001             IT-0001,IT-0002,IT-003 CREATE FUNCTION dbo.ItemList_fn     (  @FormId int  )     RETURNS varchar(1000)  AS     BEGIN        DECLARE @TempOEDetails table           ( FormCodeDetails varchar(1000)  )        DECLARE @FormCodeList varchar(1000)      DECLARE @TempFormCodeList varchar(1000)  	        SET @FormCodeList = ''            INSERT INTO @TempOEDetails            SELECT Items.ITemCode     	  FROM MainTable  	  Inner Join Items on MainTable  .FormId  = Items.FormId            WHERE MainTable.FormId = @FormId  	  And Items.StuffingId is not null       IF @@ROWCOUNT &amp;gt; 0            UPDATE @TempOEDetails               SET @FormCodeList = ( @FormCodeList + FormCodeDetails + ', ' )      IF(len(@FormCodeList)&amp;gt;0	)      BEGIN	  	      Set @TempFormCodeList= substring( @FormCodeList, 1, ( len( @FormCodeList ) - 1 ))         END       ELSE	BEGIN	    SET @TempFormCodeList = ''		END	RETURN @TempFormCodeList   END  But now i want to have pass the column name to the functioneg: if i passes the to functionselect Formid,dbo.dbo.ItemList_fn (FormId,'Color') Item Nameit should return me FormCode          ITem-CodeRS-001             Bule,Green,Yellow andselect Formid,dbo.dbo.ItemList_fn (FormId,'Quantity') Item Nameit should return me FormCode          ITem-CodeRS-001             5,10,15Please help me to solve this . </description><pubDate>Mon, 12 May 2008 22:47:00 GMT</pubDate><dc:creator>Prakash-485822</dc:creator></item></channel></rss>