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 > Database Server Software > DB2 > db2 values - how to specify alias

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-06-09, 07:00
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
db2 values - how to specify alias

Hi,
on db2 v9.5 linux I would like to write SQL with constant values:

db2 "values (100,100), (200,200), (300,300)"

The output is:
Code:
1           2
----------- -----------
        100         100
        200         200
        300         300
Is there anyway I can specify alias of column names instead of "1" and "2"?
Regards
Reply With Quote
  #2 (permalink)  
Old 07-06-09, 10:12
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
SELECT *
FROM TABLE ( VALUES (100, 100), (200, 200), (300, 300) ) AS t(c1, c2)
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #3 (permalink)  
Old 07-06-09, 12:59
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
stolze,
thank a lot, it works fine...

I was just very annoyed to write SQLs like:
Code:
select 100 as c1, 100 as c2 from sysibm.sysdummy1
union all
select 200 as c1, 200 as c2 from sysibm.sysdummy1
union all
select 300 as c1, 300 as c2 from sysibm.sysdummy1
order by 1, 2
the above SQL can get very long and confusing and it will only work on DB2. So I like Stolze's suggestion very much. Thanks.

One more question: Is Stolze's solution compatible with SQL standard? Will this solution work on other databases beside DB2? I would like to get as standard SQL as possible, to reduce complexity with different databases (like Oracle).
Regards
Reply With Quote
  #4 (permalink)  
Old 07-06-09, 16:40
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
What I posted is standard SQL. Whether it works on other DBMS depends on how closely the DBMSs adhere to the SQL standard.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 07-07-09, 06:25
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Stolze, thanks for help.

One more simple question: What is difference between bellow first and second SQL, second is without "t"? Both SQLs are working fine - why is "t" needed, what is its purpose?
Code:
SELECT *
FROM TABLE ( VALUES (100, 100), (200, 200), (300, 300) ) AS t(c1, c2)
and
Code:
SELECT *
FROM TABLE ( VALUES (100, 100), (200, 200), (300, 300) ) AS (c1, c2)
Regards
Reply With Quote
  #6 (permalink)  
Old 07-07-09, 07:26
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
"t" in first query is correlation-name. It is used to qualify column names.
"AS" is optional.
So, "AS" in second query must be interpreted as correlation-name.
Reply With Quote
  #7 (permalink)  
Old 07-07-09, 07:57
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
tonkuma, thanks a lot now I understand. I can write SQLs (joins) like this:

Code:
SELECT
   t.c1,
   t.c2,
   a.c1
FROM
   TABLE ( VALUES (100, 100), (200, 200), (300, 300) ) t (c1, c2)
INNER JOIN
   admin.tab a
   on t.c1=a.c1
admin table:
Code:
create table admin.tab (c1 int)
insert into admin.tab values (100)
insert into admin.tab values (400)
Thanks a lot for clearing this.
Regards
Reply With Quote
  #8 (permalink)  
Old 08-05-09, 05:02
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Quote:
Originally Posted by stolze
What I posted is standard SQL. Whether it works on other DBMS depends on how closely the DBMSs adhere to the SQL standard.
Hi,
I have tried the following SQL, that is working fine on DB2:
Code:
SELECT TEMP.C1, TEMP.C2 FROM TABLE
 ( VALUES (100, 100), (200, 200), (300, 300) ) AS TEMP (C1, C2)
but getting errors on Oracle and MySQL that "values" is not valid.

Interesting, is this really a standard if nobody follows it.
I have found web page that was updated yesterday with SQL-2008 standard and see there is no real support for such a command:
http://troels.arvin.dk/db/rdbms/#other-dummy_table DB2 sample would be: SELECT TEMP.C1 FROM TABLE ( VALUES (1 + 1)) AS TEMP (C1)

Regards

Last edited by grofaty; 08-05-09 at 07:00.
Reply With Quote
  #9 (permalink)  
Old 08-05-09, 09:48
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
In SQL-2003, this is in Subclause 7.3, "table value constructor"
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 08-05-09, 10:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by grofaty
Interesting, is this really a standard if nobody follows it.
that's the nice thing about standards -- there are so many to choose from
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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