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 > Selecting multiple columns

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-06, 06:20
Jugular Bean Jugular Bean is offline
Registered User
 
Join Date: Jul 2006
Posts: 11
Selecting multiple columns

Say I have 20 columns in my table. I want to select all of them.
That would be SELECT * FROM

However out of these I want to rename say 3
SELECT a AS b etc.

Is there anyway to do this without having to name all 20 columns in the select statement?
Reply With Quote
  #2 (permalink)  
Old 08-07-06, 06:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
no, there is not
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-07-06, 11:04
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool


NO, It's all or nothing.

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #4 (permalink)  
Old 08-07-06, 13:41
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Just an observation, but using the SELECT * syntax is usually considered "bad form" anyway. If something causes a change in the table schema, any code that depends on specific columns in a specific order will break. If you explicitly name the columns that you expect and want, you can be much more certain of getting what you expected.

If you follow the "best practice" of explicitly stating what columns you want, then aliasing one or all of them is trivial.

-PatP
Reply With Quote
  #5 (permalink)  
Old 08-08-06, 00:35
Jugular Bean Jugular Bean is offline
Registered User
 
Join Date: Jul 2006
Posts: 11
Darn! Well anyhoo!
Reply With Quote
  #6 (permalink)  
Old 08-08-06, 02:43
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Some RDMBS's support
Code:
SELECT a AS b, c AS d, *
FROM mytable
but as Pat mentioned: it's not a good idea to use "*" in "production" code.
With some cut-and-paste work (from a query on the catalog) you don't need to type in all 20 column names...
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #7 (permalink)  
Old 08-08-06, 09:58
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Isn't it a lot to ask to individually code 20 column names into an SQL statement?
Reply With Quote
  #8 (permalink)  
Old 08-08-06, 10:35
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by urquel
Isn't it a lot to ask to individually code 20 column names into an SQL statement?
no, it isn't

it isn't a lot to ask to code all desired columns into an SQL statement
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 08-08-06, 10:50
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by urquel
Isn't it a lot to ask to individually code 20 column names into an SQL statement?
I have several SQL queries (embedded in applications) on joins of (sometimes) more than 5 tables, which return more than 20 columns.
I never use "SELECT *" in those cases but mention the columns explicitly (typically one per line, for readability).
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #10 (permalink)  
Old 08-09-06, 00:04
Jugular Bean Jugular Bean is offline
Registered User
 
Join Date: Jul 2006
Posts: 11
Thanks for the tips guys.

Patience is the key to good SQL statements then.
Reply With Quote
  #11 (permalink)  
Old 08-09-06, 02:17
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
When you write the code, spending a minute or two to put all columns into the SELECT column list might look too long, but you'll be grateful after a few months when you (or, even worse, someone else) will have to debug this code.

Although you *know* which column belongs to which table at that moment, believe me, you'll forget that and - without using table aliases along with column names - you'll be in a deep trouble. How will you know which tables you joined in every WHERE clause?

Spend some extra time now to save much more time and effort in the future.
Reply With Quote
  #12 (permalink)  
Old 03-18-08, 17:42
quikenheimer quikenheimer is offline
Registered User
 
Join Date: Mar 2008
Posts: 2
Smile Selecting columns but modifying data

I am developing an application that will select a table with approximately 50 columns, but if a column contains NULLs, I want high-values returned from the selection. Currently, i'm doing this process manually. For example:

SELECT COL1, COL1, COL3, COL4
INTO WORK1:IND-WORK1,
WORK2:IND-WORK2,
WORK3:IND-WORK3,
WORK4:IND-WORK4

If IND-WORK1 = 0
MOVE WORK1 TO VALUE1
END-IF
IF IND-WORK2 = 0
MOVE WORK2 TO VALUE2
END-IF

Is there a way of having DB2 internally change nulls to high-values before returning the value? When i'm updating the table, I also have to do the reverse, where I check for high-values, if present, I move -1 to the IND-WORK1 to indicate NULL values should be loaded into the table.

Is there any easier way of doing this, has anybody been challenged like this?
Reply With Quote
  #13 (permalink)  
Old 03-18-08, 17:52
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by quikenheimer
Is there a way of having DB2 internally change nulls to high-values before returning the value?
Sure:
Code:
SELECT COALESCE(col1, x'FF'), COALESCE(col2, x'FF'), ...
INTO :work1, :work2, ...
FROM   mytable ...
Quote:
Originally Posted by quikenheimer
I also have to do the reverse
Also this is easily possible:
Code:
INSERT INTO mytable(col1, col2, ...)
VALUES (NULLIF(:work1, x'FF'), NULLIF(:work2, x'FF'), ...)
(Or likewise for UPDATE, for that matter.)
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #14 (permalink)  
Old 03-18-08, 17:54
quikenheimer quikenheimer is offline
Registered User
 
Join Date: Mar 2008
Posts: 2
Thanks. I'll give this a try!
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