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


Use Variable as SQL column Name in query


Use Variable as SQL column Name in query

Author
Message
NoraG
NoraG
Old Hand
Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)Old Hand (360 reputation)

Group: General Forum Members
Points: 360 Visits: 916
Hi all,

Is it poosible to use a variable as a column name in SQL?

EXAMPLE:

DECLARE @ColumnName VARCHAR(100)

set @ColumnName= 'Date Received '+ GETDATE()



SELECT Datecolumn as @ColumnName
FROM TABLE1



Quiet obviously I am getting an error here...
I found a way to do it but that by keeping the select query in a diffrent variable like this:
SET @SQLQUERY = N'SELECT DISTINCT (' + QUOTENAME(@COLUMNNAME) + ') FROM TABLE1'


but It doesn't satisfy my need, as I wanna use it in my select query directly...
I'm not sure if its possible but my googling so far ........

thanks for ur help


---------------------------------------------------

Thanks

Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)SSChasing Mays (603 reputation)

Group: General Forum Members
Points: 603 Visits: 1226
I'm not really sure what your requirement is. You will need to use some form of dynamic sql.


declare @ColumnName varchar(50)
declare @sql nvarchar(max)

set @ColumnName = 'SalesData_' + convert(varchar(2),datepart(dd,getdate()))
set @sql = 'select ' + @ColumnName + ' from SalesTable'
print @sql

EXEC sp_sqlexec @sql



Converting oxygen into carbon dioxide, since 1955.


chaitukadivella
chaitukadivella
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 45
This works fine when the table is in default schema that is dbo. Can you specify how we can make this query work when the table is in some other schema.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
chaitukadivella (8/7/2012)
This works fine when the table is in default schema that is dbo. Can you specify how we can make this query work when the table is in some other schema.


Is the other schema known and static or can it change as well?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24177 Visits: 37948
chaitukadivella (8/7/2012)
This works fine when the table is in default schema that is dbo. Can you specify how we can make this query work when the table is in some other schema.


The simple answer is this:



declare @ColumnName varchar(50)
declare @sql nvarchar(max)

set @ColumnName = 'SalesData_' + convert(varchar(2),datepart(dd,getdate()))
set @sql = 'select ' + @ColumnName + ' from yourschema.SalesTable'
print @sql




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
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