Friday, February 17, 2012

Dynamic Column Alias Name...

Hello All,
Here is my question; I am trying to dynamically assign a Column Alias in a
Select Statement to a variable, in which case the variable was assigned the
value of another select statement?
This is a simplified version of what I am trying to do, but would compliment
the results I am trying to get too if one would know if this is possible.
Here is the example SQL:
---
Create Table Test(
COL NVArChar(20)
)
Insert Into Test
Values('MyDynamicColumnName')
Go
Declare @.MyCol As NVarChar(20)
Set @.MyCol = (Select COL From TEST)
--Print @.MyVal
Select Top 20 au_id As @.MyCol
From authors
---AFAIK, it can't be done without using dynamic SQL.
Thomas
"Diablo" <Diablo@.discussions.microsoft.com> wrote in message
news:DA69C85C-04D8-42C5-A9FC-8BBDD1C87523@.microsoft.com...
> Hello All,
> Here is my question; I am trying to dynamically assign a Column Alias in a
> Select Statement to a variable, in which case the variable was assigned th
e
> value of another select statement?
> This is a simplified version of what I am trying to do, but would complime
nt
> the results I am trying to get too if one would know if this is possible.
> Here is the example SQL:
> ---
> Create Table Test(
> COL NVArChar(20)
> )
> Insert Into Test
> Values('MyDynamicColumnName')
> Go
> Declare @.MyCol As NVarChar(20)
> Set @.MyCol = (Select COL From TEST)
> --Print @.MyVal
> Select Top 20 au_id As @.MyCol
> From authors
> ---|||Thomas,
Would you happen to have an example of this I could leverage?
Regards...
"Thomas Coleman" wrote:

> AFAIK, it can't be done without using dynamic SQL.
>
> Thomas
>
> "Diablo" <Diablo@.discussions.microsoft.com> wrote in message
> news:DA69C85C-04D8-42C5-A9FC-8BBDD1C87523@.microsoft.com...
>
>|||Try this...just based it off the pubs db so wouldn't need to create a table
and populate...
declare @.val varchar(50)
select @.val = au_lname from authors where au_id = '172-32-1176'
execute ('select au_lname as ' + @.val + ' from authors')
go
HTH
J
"Diablo" <Diablo@.discussions.microsoft.com> wrote in message
news:5409CAC2-76A2-4CF7-8C91-0AFCA01827A3@.microsoft.com...
> Thomas,
> Would you happen to have an example of this I could leverage?
> Regards...
> "Thomas Coleman" wrote:
>|||Declare @.SQL VarChar(8000)
Declare @.ColName VarChar(25)
Set @.SQL = 'Select TOP 20 au_id As ' + QuoteName(@.ColName) + ' From authors'
Exec(@.SQL)
You should obviously be wary of using dynamic SQL for various reasons not th
e
least of which is that (in SQL 2000) users will need to have direct access t
o
the tables using their user account.
Thomas
"Diablo" <Diablo@.discussions.microsoft.com> wrote in message
news:5409CAC2-76A2-4CF7-8C91-0AFCA01827A3@.microsoft.com...
> Thomas,
> Would you happen to have an example of this I could leverage?
> Regards...
> "Thomas Coleman" wrote:
>|||>> I am trying to dynamically assign a Column Alias in a Select Statement to
a variable, in which case the variable was assigned the value of another se
lect statement? <<
You are confusing display, which is a front end function in a tiered
architecture, with a data model. Data elements do not magically change
names (and therefore what they model) at run time in an RDBMS.
The kludge for newbie programmers that don't know anything about RDBMS
is to use Dynamic SQL, which is slow and dangerous.|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1117048471.627932.101610@.o13g2000cwo.googlegroups.com...
> You are confusing display, which is a front end function in a tiered
> architecture, with a data model.
For a drastically different perspective I suggest you check out
www.alphora.com
which is based on the work of C.Date & al.You know Date don't you?:)
Hopefully the D4 language doesn't threaten you:)|||From the site:
> New version of toolset takes developers beyond
> Rapid Application Development (RAD)
How exciting! This sounds like the perfect tool for the new F.A.D.
development methedology:
http://weblogs.asp.net/alex_papadim.../05/405747.aspx
Alex Papadimoulis
http://weblogs.asp.net/Alex_Papadimoulis
"Pike" wrote:

> "--CELKO--" <jcelko212@.earthlink.net> wrote in message
> news:1117048471.627932.101610@.o13g2000cwo.googlegroups.com...
> For a drastically different perspective I suggest you check out
> www.alphora.com
> which is based on the work of C.Date & al.You know Date don't you?:)
> Hopefully the D4 language doesn't threaten you:)
>
>

No comments:

Post a Comment