Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Select * as ?

Select * as ?

Select * as ?

In the database I have a number of tables with the same column name.  for example Employer.FirstName, Staff.FirstName, Person.firstName etc.
I'm doing a full outer join and want to rename these without specifying the columns.

So it's


Select employer.*, staff.*, person.* from employer full outer join staff on something full outer join person on something

What I think I want to do for simplicity is rename everything to tableName_columnName without having to explicitly name each column.

I'm trying to avoid


Select employer.address as Employer_Address,employer.FirstName as Employer_FirstName ...

And before you say it, I know that generally it is a waste of resources to simply say select * since you are returning a lot more than you actually need.  The reason for using "select *" is there are a number of user defined columns where the user can name the column whatever they want.  Once the application is in place, I don't have anyway of knowing exactly what the users have added for column names.

Is there a way to effectively say "Select employers.* as employers_*"?

To build may have to be the slow laborious task of years.  To destroy can simply be the thoughtless act of a single day.

RE: Select * as ?

Ouch. You never want to do that! Never never let users add columns with names you don't know. This is trouble waiting to happen. Typically when user defined columns are allowed they are named usercolumn1, usercolumn2 etc and there are a limited number of them. If you don't know the column names how can you effectively use them?

How on earth will you ever write code against a database when you don't know the column names?

I suppose your only choice is to look in the system tables for the column names, but I would never allow this to be done by the user interface. Talk about making your data completely unsafe!

And to have to select * for every single query, this thing is going to be slower than dirt.

Frankly I think your only valid choice is a complete redesign.


"NOTHING is more important in a database than integrity." ESquared

RE: Select * as ?

Thanks for your editorial comment.  It makes for good reading when sitting on the toilet, but that's about it.

It would have been far more useful for you just to have said
"Sorry, but I don't know of a way to do what you are asking"

I'll correct your thought process since it was wrong.
I am not using Select * for every single query, just for a query under certain conditions.

To build may have to be the slow laborious task of years.  To destroy can simply be the thoughtless act of a single day.

RE: Select * as ?

jeep, don't get mad

the design dictates the solution, and in this case there ain't no way to get what you want

SQL Sister is right, you should be thinking about a redesign

and a three-way full outer join?  whoa!!!

r937.com | rudy.ca

RE: Select * as ?

Think "Report Generation Tool."
Ever done something with Crystal Reports or Report Smith?
Dog slow.  Can't get around it.  If you want an idiot proof interface, the trade off is performance because you have to assume that an idiot is using it.

The application is fine and does a bang up job for what it is intended to do.  The user base is looking for a cheap way to do some on demand reports.  Both Crystal Enterprise and Report Smith Enterprise are over kill for what is needed here.  At $20,000 and $14,000 respectively per server installation, it's just not cost effective.

So I'm building a scaled down simplified way for the users to pick and choose what they need when generating a report, hence the full outer joins and the select *.  Yes it is slow.  I know it's slow.  It would be slow if we used Crystal Enterprise as well.  The difference is with 2 days of my programming time I can rattle off a quick and dirty tool that lets users generate their own on demand reports without them having to have any knowledge of the database.

The solution that I was hoping to avoid is renaming the columns in all of the data tables to ensure they are unique, then adding to functions to the application to handle the conversion of the column names.  I don't really want to do that because it means testing every single page in the application to accommodate this one functionality upgrade.  What really should be just an add on means a whole new release of the product.

To build may have to be the slow laborious task of years.  To destroy can simply be the thoughtless act of a single day.

RE: Select * as ?

Why not dynamically generate the field list for the query?  You could grab a list of the column names from the database, apply whatever transformations you need to turn them into a pretty "table.column_name AS Table_Column_Name" format, and dump that into the SELECT query that you pass to the database.  Maybe not be the most elegant solution in the world, but it would give you the effect of auto-prefixing all the column names without having to manually do it in the SQL or alter the database.

RE: Select * as ?

also do a search for EAV entity-attribute-value

i don't much like it but it does seem to fit the circumstances

r937.com | rudy.ca

RE: Select * as ?

As AdaHacker said, it's very simply to do programatically:

Here is a Python exapmle


def generate_select(schema, table, field_list):
  myselect = "select \n"
  for field in field_list:
    myselect += "  %10s as %s_%s\n" % (field.ljust(10), table, field)
  # FROM  
  myselect +=  "from %s.%s" % (schema, table)
  return myselect

if __name__=="__main__":
  # obtain these from your database using driver (ADO, ODBC, ...)
  field_list=['LASTNAME', 'FIRSTNAME', 'Field001', 'FIELD002', 'FIELD003']
  schema = 'BIZDATA'
  table = 'CUSTOMERS'
  generated_select = generate_select(schema, table, field_list)
  print generated_select
Output is your desired select


  Field001   as CUSTOMERS_Field001

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close