Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:10+00:00 2026-06-15T07:35:10+00:00

First time working with DB2. I have a stored proc that performs a union,

  • 0

First time working with DB2. I have a stored proc that performs a union, well it trys to anyhow. The procedure times out. The select statements on either side of the union execute quickly with no issue when done individually. Why would a union do this?

Here is some sudo code for the proc, assume data types are either int or string, and they match when compared or unioned unless otherwise noted in the SQL:

DECLARE Foo CURSOR WITH RETURN FOR
  Select STRIP(A.Name) as my_Name, 
     Case A.Number
        when 2 then '(' || strip(char(A.Number)) || ')' strip(B.num)
        when 3 then '(' || strip(char(A.Number)) || ')' strip(C.num)
        when 4 then '(' || strip(char(A.Number)) || ')' strip(D.num)
        when 5 then '(' || strip(char(A.Number)) || ')' strip(E.num)
        when 6 then '(' || strip(char(A.Number)) || ')' strip(F.num)
        end as my_number
  FROM A
       left outer join B on A.Number= 2 and A.Name = B.Name
       left outer join C on A.Number= 3 and C.Name = B.Name
       left outer join D on A.Number= 4 and D.Name = B.Name
       left outer join E on A.Number= 5 and E.Name = B.Name
       left outer join F on A.Number= 6 and F.Name = B.Name 
      ,session.Temp_Result X
  WHERE X.ID = A.ID
  GROUP BY A.Number, A.Name, B.Name, C.Name, D.Name, E.Name, F.Name
for fetch only ur;

DROP TABLE Session.Temp_Result;

DECLARE GLOBAL TEMPORARY TABLE session.Temp_Result
(     ID DECIMAL(18,0)
);

INSERT INTO session.Temp_Result
        select X.ID
        from Z, Y, X, Q
        where Z.num = 6
          and Z.ID = Y.ID2
          and Y.GROUPA = 'ABC'
          and Y.GROUPB = 'DEF'
          and Y.ID = X.ID2
          and X.ID = Q.ID            
        union
       select W.ID
        from Z, Y, W
        where Z.num = 6
          and Z.ID = Y.ID2
          and Y.GROUPA = 'ABC'
          and Y.GROUPB = 'DEF'
          and Y.ID = W.ID2
        group by ID;


  OPEN C_HIERARCHIES;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:35:12+00:00Added an answer on June 15, 2026 at 7:35 am

    Try to query using “with” statement.

    DECLARE GLOBAL TEMPORARY TABLE session.Temp_Result (
    ID DECIMAL(18,0))
    WITH REPLACE;
    
    INSERT INTO session.Temp_Result
    (ID)
    WITH Q1(Y_ID)
    AS (
    SELECT Y.ID
    FROM Z
        INNER JOIN Y ON Z.ID = Y.ID2
    WHERE Z.NUM = 6 AND Y.GROUPA = 'ABC' AND Y.GROUPB = 'DEF')
    
    SELECT X.ID
    FROM X
        INNER JOIN Q1 ON X.ID2 = Q1.Y_ID
    WHERE EXISTS(SELECT 1 FROM Q WHERE Q.ID = X.ID)
    UNION
    SELECT DISTINCT W.ID
    FROM W
        INNER JOIN Q1 ON W.ID2 = Q1.Y_ID
    

    If it does not help, try to use “Explain SQL” for the select statement. Maybe you should create some indexes…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with oracle for the first time, and I have a table called
First time working with android. I'm look for a way that an android app
This is my first time working with the Google Maps API. I have a
I have just installed a custom WordPress theme (this is my first time working
First time working with DB2, and I'm getting an occasional failed sql state with
This is my first time working with classes, so excuse my ignorance. I have
Let me start off by saying that this is my first time working with
First time working with JSON in Rails and trying to figure out something which
This is my first time working with a WPF datagrid. From what I understand
This is my first time working with file i/o in java, and it's not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.