Passing the field name as a paramter to the select SQL? - MittyKo

05-Nov-07 12:59:00
Hi All

I would like to pass the field name as a paramter to the below SQL to get
all the values for that filed:

--Table  #Tb1
Create table #Tb1 ( A int IDENTITY(1,1) ,B varchar(10), C varchar(10), D
varchar(10))
insert into #Tb1 (B,C) values ('a1','')
insert into #Tb1 (B,D) values ('b2','')
insert into #Tb1 (C,D) values ('','d3')

--SQL
declare  @Column char(5)
set @Column = 'D'
select @Column   from #Tb1
--Values
D
D
D

--What i am trying to achieve by passing the fieldname as a parameter.
--select D from #Tb1
--Right values
D
Null

d3

I get Ds instead of the real values, but when i run 'select D from #Tb1' i
get the real values. What am i doing wrong or is there a way to get the real
values for column D by passing the field name as a parameter to the SQL?

Thank you in advance
reply
 
 

Passing the field name as a paramter to the select SQL? - Mike C#

05-Nov-07 01:16:10
You need to use dynamic SQL to get what you want out of this.  Check out the
EXEC statement and the sp_executesql procedure in BOL.
reply
 

Passing the field name as a paramter to the select SQL? - Steve Dassin

05-Nov-07 05:21:50
You're thinking right, the problem is sql isn't :-)
To pass an argument you have to declare a type for it, computer science 101.
A column 'type' doesn't really make sense. But a table 'type' does.

http://beyondsql.blogspot.com/2007/06/dataphor-13-passing-table-as-parameter.html
http://beyondsql.blogspot.com/2007/08/dataphor-creating-super-function.html

Because sql only recognizes (loosely) numbers and strings as types your
SOL and forced to make up for this huge hole with dynamic sql :( :)

www.beyondsql.blogspot.com
reply
 

Passing the field name as a paramter to the select SQL? - M

05-Nov-07 06:15:00
As Mike C# mentioned in his post one option is dynamic SQL.  I strongly
suggest reading this fine article by Erland Sommarskog on the subject:
http://www.sommarskog.se/dynamic_sql.html

On the other hand, if you explain your actual goal (in business terms) in
more detail perhaps we can help you find an even more favorable solution.


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
reply
 

Passing the field name as a paramter to the select SQL? - Madhivanan

06-Nov-07 05:24:16
On Nov 5, 10:59 pm, MittyKom <Mitty...@>

You should avoid passing object names as parameter
Make sure you read sommarskog's article fully
reply
 

Passing the field name as a paramter to the select SQL? - Madhivanan

06-Nov-07 05:25:21
Steve

All of your links are redirected to the same page
reply
 

Passing the field name as a paramter to the select SQL? - Steve Dassin

06-Nov-07 05:54:18
Hi,

Yep, just scroll down to the article. The link will be the article after all
the titles/labels :)
reply
 

Passing the field name as a paramter to the select SQL? - Mikhail Berlyant

06-Nov-07 12:56:42
At least for fun try this:

CREATE TABLE #Tb1 ( A int IDENTITY(1,1) ,B varchar(10), C varchar(10), D
varchar(10))
INSERT INTO #Tb1 (B,C) VALUES ('a1','')
INSERT INTO #Tb1 (B,D) VALUES ('b2','')
INSERT INTO #Tb1 (C,D) VALUES ('','d3')

DECLARE @Column char(5)
SET @Column = 'D'

DECLARE @x xml
SET @x = (SELECT * FROM #Tb1 FOR XML PATH, TYPE)
SELECT
t.c.value('(./../A/text())[1]', 'int') as a ,
t.c.value('(./text())[1]', 'varchar(10)') as r
FROM
@x.nodes('/row/*') as t(c)
WHERE
t.c.value('local-name(.)', 'varchar(5)') = @Column

DROP TABLE #Tb1

--
Mikhail Berlyant
Senior Data Architect
MySpace.com
reply
 
Should be a simple datetime issue...
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class