How to write correct stored procedure in netezza to fetch and display all records from table ? need select procedure - stored-procedures

I have written a stored procedure for select operation in netezza db, but its throwing error while I try to execute that stored procedure.
Below is my stored procedure for select operation to fetch and display all records from a table.
#delimiter %%%;
CREATE OR REPLACE PROCEDURE return_result10() RETURNS REFTABLE(tbl)
LANGUAGE NZPLSQL AS
BEGIN_PROC
BEGIN
call * from tbl;
RETURN REFTABLE;
END;
END_PROC;%%%
#delimiter;%%%
I am getting below error as soon as I try to execute this using command ( execute PROCEDURE return_result10(); )
[Code: 1100, SQL State: HY000] ERROR: query "SELECT * from tbl" didn't return a single value
I want a stored procedure to fetch and display all records present in a table in netezza db.

Related

Error while connecting to stored procedure in Tableau

I'm getting this error while trying to connect to a stored procedure as data source:
Unable to complete action
The stored procedure returned no results. The fields don't have column names, or the data in the fields is not a supported data type
But the same stored procedure returns data in database. Any idea on this?
Please try adding the SET NOCOUNT ON code in the beginning of the stored procedure.
Something like
CREATE PROCEDURE [dbo].[ZZZ_THIS_IS_MY_SP]
#p INT
AS
BEGIN
SET NOCOUNT ON
DECLARE #Var1 NVARCHAR(20)
DECLARE #Var2 DATETIME
-- All other instructions...
END

Unable to retrieve multiple rows using cursor from database table using stored procedure DB2

I am trying to retrieve multiple rows from table using stored procedure and it doesn't work for me. My stored procedure is as follows,
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) ) LANGUAGE SQL
BEGIN
DECLARE C1 CURSOR FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;
I am trying to call the stored procedure from Mule Anypoint Studio using the Database connector and have seen the result as null when I print the payload with a logger. If I try to modify the query as it returns a single row it works (without using cursor).
Please help to resolve this. Thanks.
What platform and version of DB2?
Try the adding
DYNAMIC RESULT SETS 1
WITH RETURN TO CLIENT
Like so:
CREATE OR REPLACE PROCEDURE E_Enquiry
(IN SourceQueue1 VARCHAR(30) )
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR
select CreationTime
from ms.Exception_Message
where SourceQueue = SourceQueue1;
open c1;
END;

DB2 stored procedure razor sql

I'm trying to create a stored procedure in a DB2 database using RazorSQL Client but I'm getting this error:
Blockquote
ERROR: A character, token, or clause is invalid or missing.
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=SELECT
The stored procedure code is:
CREATE PROCEDURE GENERAL.sp_checkemp
(IN emp_code VARCHAR(20))
LANGUAGE SQL
READS SQL DATA
BEGIN
SELECT "name_emp" FROM GENERAL."employee" WHERE "code_emp" = 'abc';
END
SELECT statements in the SQL PL context must have an INTO clause -- you have to put the query results somewhere. If your query returns more than one row, which I suspect it is, you must use a cursor instead.

DB2 - How do I create a Stored Procedure that ignores errors?

I am trying to create a simple stored procedure in DB2 that performs the following actions:
Drop table "DELETE_ME"
Create table "DELETE_ME"
Insert into table "DELETE_ME"
Basically, if the table DELETE_ME already exists, the stored procedure executes fine. If it does not exist, then the Stored Procedure fails with the following error:
Lookup Error - DB2 Database Error: ERROR [42704] [IBM][DB2/LINUXX8664] SQL0204N "SARTS02T.DELETE_ME" is an undefined name.
I completely understand this. However, I was hoping to set the procedure up to ignore this error (and all errors) that way the procedure can work as a deployment script as well. On the first deployment, it will ignore the drop table line and jump right to building the table.
I found a CREATE PROCEDURE option called CONTINUE AFTER FAILURE however I am receiving an error. Please see my code and error below. Thanks for your help!!!
CODE:
CREATE OR REPLACE PROCEDURE TEST_PROC LANGUAGE SQL CONTINUE AFTER FAILURE
BEGIN
DROP TABLE DELETE_ME;
CREATE TABLE DELETE_ME (
COLUMN_A DECIMAL(5)
);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (69);
COMMIT;
END;
ERROR:
Lookup Error - DB2 Database Error: ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "CONTINUE AFTER FAILURE" was found following "ST_PROC LANGUAGE SQL". Expected tokens may include: "<space>".
The BEGIN ... END section is called a compound statement which allows various declarations and other statements, including other compound statements, to be included inside it.
You'll want to declare a condition handler. You could do it something like this.
CREATE OR REPLACE PROCEDURE TEST_PROC
LANGUAGE SQL
COMMIT ON RETURN YES
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '42704' -- or SQLEXCEPTION
BEGIN
-- insert any code here when DROP fails
END;
DROP TABLE DELETE_ME;
CREATE TABLE DELETE_ME
( COLUMN_A DECIMAL(5)
);
INSERT INTO DELETE_ME (COLUMN_A)
VALUES (69);
END;

DB2 Can't REFRESH Materialized Query Table (MQT) in Stored Procedure

I'm having trouble refreshing an MQT inside of a stored procedure.
I'm receiving the following error when attempting to refresh the table :
ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "REFRESH" was found following "ANGUAGE SQL BEGIN " . Expected tokens may include: "TRUNCATE" .
/**********
* CREATE A NEW DUMMY TABLE AND INSERT DUMMY DATA
**********/
CREATE TABLE DELETE_ME (
COLUMN_A DECIMAL(5)
);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (1);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (2);
INSERT INTO DELETE_ME (COLUMN_A) VALUES (3);
/**********
* CREATE A MATERIALIZED QUERY USING THE DUMMY TABLE
**********/
CREATE TABLE MQT_TEST AS (
SELECT COLUMN_A
FROM DELETE_ME
)
DATA INITIALLY DEFERRED
REFRESH DEFERRED
ENABLE QUERY OPTIMIZATION
NOT LOGGED INITIALLY;
COMMIT;
/**********
* CREATE A SIMPLE PROCEDURE TO REFRESH THE MATERIALIZED QUERY
**********/
CREATE OR REPLACE PROCEDURE TEST_PROC LANGUAGE SQL
BEGIN
REFRESH TABLE MQT_TEST;
END;
--!!!! FAILS WITH THE FOLLOWING ERROR CODE: - DB2 Database Error :
ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "REFRESH" was found following "ANGUAGE SQL BEGIN ". Expected tokens may include: "TRUNCATE" .
You can't execute the REFRESH TABLE as a static statement in a stored procedure as it can only run as a dynamic statement.
Therefore, the proper method to do this is to use the EXECUTE IMMEDIATE statement:
CREATE OR REPLACE PROCEDURE TEST_PROC
LANGUAGE SQL
BEGIN
declare vSQL varchar(1024);
set vSQL = 'refresh table MQT_TEST';
execute immediate vSQL;
END#

Resources