Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Use a variable as a column name - without dynamic SQL?


Use a variable as a column name - without dynamic SQL?

Author
Message
webskater
webskater
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 479

CREATE TABLE #tblItems
(
ItemID int,
ItemDescription varchar(50),
ItemDate smalldatetime
);

INSERT INTO #tblItems
(ItemID, ItemDescription, ItemDate)
SELECT 1, 'Item 1', '01 Jan 2012' UNION ALL
SELECT 2, 'Item 2', '03 Jan 2012' UNION ALL
SELECT 3, 'Item 10', '14 Jan 2012';

SELECT ItemID AS [Item ID] FROM #tblItems

DECLARE @SQLstring nvarchar(500)
DECLARE @ColName varchar(100)
SET @ColName = 'Order ID'
SET @SQLstring = 'SELECT ItemID AS [' + @ColName + '] FROM #tblItems'
EXEC sp_executesql @SQLstring

DROP Table #tblItems



I have a column which for some clients might contain an Order Number. For another client it might contain a Job No etc.

Is it possible to run a SELECT statement in such a way that I can return a Column Name for that column that makes sense to the person viewing it. In the example above the first column is called ItemID. But for Company A I might want to do this:

Select ItemID AS [Order ID] ...

whereas for Company B I might want to do this:

Select ItemID AS [Job Number] ...

Is there a way of doing this without using dynamic SQL?
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36130 Visits: 18744
Not really. You can use a CASE type expressions as

...
If @item = 'Job'
then
select
JobID = OrderID
from #TblItems
else if @item = 'Order'
select OrderID = OrderID
from #tblItems
...



Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9438 Visits: 9517
No, you cannot generally variablize column names, Dynamic SQL is how to do this.

There are some limited ways around it for specific cases, using if statements or other high-level branch techniques to select different queries based on the variable, but they're more trouble than they're worth once it goes beyond more than a couple of choices.

If you do use Dynamic SQL for this, however, you need to make sure that it's protected against SQL Injection.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
webskater
webskater
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 479
Thank you both for your help.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search