Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using field data to generate SQL Aliases

Status
Not open for further replies.

SlytlyMad

IS-IT--Management
Jun 9, 2003
3
0
0
GB
I'm trying to do the following:

select field1 as anothertable.field2 from mytable....

Does anyone know if this is possible/how to do it?
 
you can specify column aliases like so

select field1 as mynewfieldname from mytable

though I'm not quite sure why you are qualifying the alias with a table name, does this example help?


Matt

Brighton, UK
 
Thanks for a speedy response.

Umm, not really what I'm after though.

I have a table used to store form label values (e.g. Name, Address, D.O.B, etc.) and these are customisable. They are referenced by using the table's ID field.

For view-creation purposes, I need to be able to write a large select statement and use the label names as aliases.

Is that any clearer? (If not, I'll try to put together some data).
 
Nope, still not quite sure what you're after! Can you give some example data and the result you are looking for?

--James
 
You must use a dynamic query
declare ...

select @my_alias = <retrieve alias here>

select @sql_query = 'select field1 as ' + @my_alias + ' from mytable'
execute (@sql)
 
i think this is what you ment

declare @var varchar(50)

select @var = (select alias from Wherever)

exec('Select column_name as [' + @var + '] from table')


I think that is what you are tyying to do
 
looks like I was composing a reply same time as James, sorry for the duplication
 
wilk and pascalsql are exactly right...is there any way of doing this with 'standard' T-SQL?

Or will I have to use a stored procedure?

MTIA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top