Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is there any equivalent of indirection (macro) in T-SQL?
Message
From
31/01/2007 14:35:40
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Is there any equivalent of indirection (macro) in T-SQL?
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01191074
Message ID:
01191074
Views:
76
I am writing some stored procedures in SQL2000. I don't like T-SQL much but nothing I can do about it :)

This is a complex procedure for a complex C# 2.0 (ASP.NET) program.

To simplify the question, say I have a table with a key and various value fields, like this sample:
Key int,
Val1 int,
Val2 int,
val3, int,
--
--
val40 int
The actual table has a ton of columns with weird names. I did not create the table, nor the program, just inherited and must finish it what somebody else has left.

I created a lookup cross-ref table that with some complex code gets me to find what Val..n do I need at any point.
Something like:
DECLARE	@Val1 INT, @Val2 INT, @Val3 INT, @Val4 INT, @Val5 INT

SELECT @Val1=Val1, @Val2=Val2, @Val3=Val3, @Val4=Val4, @Val5=Val5  -- and a lot more
FROM table WHERE -- condition
If I am now passsed a parameter: @Column2Get CHAR(20)
I need to rerwite the query to just get (the equivalent of VFP's)
SELECT @(whatever_var)=(whatever_column) FROM table WHERE -- condition
Sure, I could do an IF.ELSE
like:
IF @Column2Get='Val1'
BEGIN
  SELECT @Val1=Val1 FROM table WHERE -- condition
END
ELSE
BEGIN
  IF @Column2Get='Val1'
  BEGIN
    SELECT @Val2=Val2 FROM table WHERE -- condition
  END
  ELSE
  -- another 40 times!
END
Is there a more elegant solution to build the query on the fly, retrieving the proper column, in a stored procedure?

TIA


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Next
Reply
Map
View

Click here to load this message in the networking platform