I am learning and new to db2.
Here is what I am trying to achieve in steps:
Step 1 is complete and able to insert data into the table. Now I need help with executing the select statement and print the result.. I am trying to put this select statement in the same stored procedure after the insert...
Query:
select
columna, columnb, columnc
from
table A
when
columnd <> null,
if columnd is null then
select the columna, columnb, columnc
from table A
where max(columne)
Here both columnd
and columne
are timestamp columns and need the latest row from the table.
Like fetch first 1 row only in both the cases.
Once the results are selected columna, columnb, columnc
should be printed as output once the stored procedure is completed.
TIA..
I have tried something like this below:
putting the whole second step in second stored procedure and calling the stored procedure inside the main insert stored procedure. But I am failing in the logic. As I said - I am still learning...
CREATE PROCEDURE test()
LANGUAGE SQL
BEGIN
DECLARE V_COLUMNA VARCHAR(255);
DECLARE V_COLUMNB VARCHAR(255);
DECLARE V_COLUMNC VARCHAR(255);
DECLARE V_COLUMND VARCHAR(255);
DECLARE V_COLUMNE VARCHAR(255);
IF (
SELECT
COUNT(*)
FROM
tableA pl
WHERE
columnd IS NULL) > 0 THEN
SELECT
columna,
columnb,
columnc,
columne
INTO
V_COLUMNA,
V_COLUMNB,
V_COLUMN,
V_COLUMNE
FROM
tableA
ORDER BY
TIMESTAMP DESC
FETCH FIRST 1 ROW ONLY;
ELSE
SELECT
columna,
columnb,
columnc,
columnd
INTO
V_COLUMNA,
V_COLUMNB,
V_COLUMN,
V_COLUMND
FROM
tableA
ORDER BY
TIMESTAMP DESC
FETCH FIRST 1 ROW ONLY;
END IF;
IF V_COLUMND IS NOT NULL THEN
SELECT
V_COLUMNA,
V_COLUMNB,
V_COLUMN,
V_COLUMND;
END IF;
IF V_COLUMND IS NULL THEN
SELECT
V_COLUMNA,
V_COLUMNB,
V_COLUMN,
V_COLUMNE;
END IF;
END;
You can alter Your DB2 procedure for output in this way:
CREATE OR REPLACE PROCEDURE
...
SPECIFIC ...
DYNAMIC RESULT SETS 1
...
BEGIN
...
IF ... THEN
BEGIN
DECLARE cur1 CURSOR WITH RETURN TO CALLER FOR
SELECT ...
...
;
OPEN cur1;
END;
END IF;
END @