| |
|
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.
|
 |

08-03-07, 06:01
|
|
Registered User
|
|
Join Date: Jun 2003
Location: Wakefield, UK
Posts: 8
|
|
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`;
|
|

08-03-07, 06:02
|
|
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
|
|

08-03-07, 06:49
|
|
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"
|
|

08-03-07, 08:12
|
|
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 *
|
|

08-03-07, 10:10
|
|
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? 
|
|

08-03-07, 11:02
|
|
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.
|
|

08-05-07, 08:59
|
|
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
|
|

08-09-07, 17:34
|
|
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.
|
|
|

08-10-07, 02:38
|
|
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
|
|

08-10-07, 05:53
|
|
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.
|
|
|

08-16-07, 10:49
|
|
Window Washer
|
|
Join Date: Nov 2002
Location: Jersey
Posts: 10,303
|
|
ummmmmmmmmm
Don't use SELECT *
|
|

08-17-07, 04:33
|
|
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
|
|

08-17-07, 09:43
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|