If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Simple SELECT * (easy one)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-03-07, 06:01
nooch nooch is offline
Registered User
 
Join Date: Jun 2003
Location: Wakefield, UK
Posts: 8
Question Simple SELECT * (easy one)

Hi,

Sorry I'm a bit new at this...

I'm creating a view. I want to select everything in from a table, but i want one of the columns to have an alias name. The thing is the table is likely to change, and i don't want to have to keep re-writing the SQL. Is there a say to (for example):

CREATE VIEW `test` AS
SELECT *, `test_field` as `test_type` FROM `test_table`;
Reply With Quote
  #2 (permalink)  
Old 08-03-07, 06:02
nooch nooch is offline
Registered User
 
Join Date: Jun 2003
Location: Wakefield, UK
Posts: 8
actually, i know that would kind of work, but i don't want the duplicated field name
Reply With Quote
  #3 (permalink)  
Old 08-03-07, 06:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
the field name wouldn't be duplicated, but the column would be

the best way around your problem is not to use the dreaded, evil "select star"
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 08-03-07, 08:12
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Nooch, it may be worth noting that if you make a change to your test_table your view will not reflect this - even if you use SELECT *
__________________
George
Twitter | Blog
Reply With Quote
  #5 (permalink)  
Old 08-03-07, 10:10
nooch nooch is offline
Registered User
 
Join Date: Jun 2003
Location: Wakefield, UK
Posts: 8
decided to declare each field name, hopefully there won't be that many changes with the original table.

georgev, if the table column name changes my view won't be affected then?
Reply With Quote
  #6 (permalink)  
Old 08-03-07, 11:02
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
If you add new columns to your table, they will not appear on your view.
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 08-05-07, 08:59
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The syntax is not completely right. You can use "*" in the SELECT list, but then it has to be qualified with a correlation name if there are also other columns listed. The other issue is that delimited names must use double-quotes and not single quotes. And finally, the ';' is not part of the SQL statement.
Code:
CREATE VIEW "test" AS
SELECT t.*, "test_field" as "test_type" FROM "test_table"
Another alternative is to give the names of columns like this in the CREATE VIEW statement:
Code:
CREATE VIEW "test"("col1", "col2", ..., "test_type") AS
SELECT t.*, "test_field" FROM "test_table"
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #8 (permalink)  
Old 08-09-07, 17:34
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by georgev
If you add new columns to your table, they will not appear on your view.
Is that part of the standard or a SQL Server implementation issue? (Just flying the ANSI flag ).
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #9 (permalink)  
Old 08-10-07, 02:38
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by pootle flump
Is that part of the standard or a SQL Server implementation issue? (Just flying the ANSI flag ).
That's in the standard.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 08-10-07, 05:53
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by stolze
That's in the standard.
Thank you
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #11 (permalink)  
Old 08-16-07, 10:49
Brett Kaiser Brett Kaiser is offline
Window Washer
 
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
ummmmmmmmmm

Don't use SELECT *
__________________
Brett
8-)

It's a Great Day for America everybody!

dbforums Yak CorralRadio 'Rita
dbForums Member List
I'm Good Once as I ever was

The physical order of data in a database has no meaning.
Reply With Quote
  #12 (permalink)  
Old 08-17-07, 04:33
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by Brett Kaiser
ummmmmmmmmm

Don't use SELECT *
That is common sense, one would think.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #13 (permalink)  
Old 08-17-07, 09:43
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Actually two or three would think, but we still have to repeat that little litany over, and over, and over again!

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On