Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

AS400 DB2 how to run nested subqueries

Posted on 2011-02-17
6
Medium Priority
?
3,048 Views
Last Modified: 2012-05-11
I mostly work with SQL Server. I'm not a AS400 DB2 expert. How do I write the following subquery? When I run it display the following error.

error during prepare
37000(-104)[ibm[psystem i access odbc driver)[db2 for i5/os)sql0104 - token <end-of-statement> was not valid. valid tokens: as in out <identifier>.

Example1:

SELECT *
FROM
(
SELECT *
FROM TABLE1
)

Example2 - both table 1 and table 2 have the same number of columns

SELECT *
FROM
(
SELECT *
FROM TABLE1
UNION ALL
SELECT *
FROM TABLE2
)
0
Comment
Question by:glenn_r
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:glenn_r
ID: 34918549
in addition i've tried specifying the column names with alias but it still throws the same error. I'm simulating 2 tables with the same columns and column types.

example:

SELECT column1, column2
FROM
(
SELECT po1 AS column1, line1 AS column2
FROM TABLE1
UNION ALL
SELECT po2 AS column1, line2 AS column2
FROM TABLE2
)
0
 
LVL 18

Assisted Solution

by:Dave Ford
Dave Ford earned 248 total points
ID: 34918568
The following queries work beautifully for me on AS/400 v6r1

HTH,
DaveSlash

select * from (
  select *
  from   mytable  where  deleteRequest = 'N'
) as tempTable
where userid = 2102

----------------------------

select * from (
  select *
  from   mytable
  where  deleteRequest = 'N'
  union
  select *
  from   mytable
  where  deleteRequest = 'Y'
) as tempTable
where userid = 2102

Open in new window

0
 

Author Comment

by:glenn_r
ID: 34918603
Any ideas why id be getting that error?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:glenn_r
ID: 34918632
daveslash: I see you specify a tablename after the subquery (as tempTable). Can you strip this off and see if you get the same error. Perhaps thats my problem.
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 34918659

That's exactly your problem.

Personally, I prefer to use the WITH clause instead of embedding a SELECT in the FROM clause.

e.g.

with deleteRequestNo as (
  select *
  from   myTable
  where  deleteRequest = 'N'
),
deleteRequestYes as (
  select *
  from   myTable
  where  deleteRequest = 'Y'
)
select *
from   deleteRequestNo
union
select *
from   deleteRequestYes

Open in new window

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 252 total points
ID: 34927341
your problem is that you haven't named the subquery... that is the missing token...

select * from
  (select ....
       from ...
     union ...
       select
         .... from

 ) as ANAME

required in both db2 and MSSQL etc...
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question