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. |
 |
| |
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 |
 |
| |
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/ |
 |
| |
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 |
 |
| |
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 |
 |
| |
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 :) |
 |
| |
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 |
 |
| |