DB2 stored procedure razor sql - stored-procedures

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.

Related

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

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.

HSQL problem when using TIMESTAMP value as IN parameter in stored procedure

Overview HSQL IN parameter of type TIMESTAMP doesn't work as expected for HSQL stored procedure.
Given the following DDL :
CREATE TABLE TS_STORE (
ID_COL VARCHAR(20) NOT NULL PRIMARY KEY,
TS TIMESTAMP
);
A DML statement such as :
INSERT INTO TS_STORE (ID_COL, TS) VALUES ('key1', '2020-02-19 12:17:53');
will successfully insert a row.
Then when I attempt to create a stored procedure to do the same as:
CREATE PROCEDURE TEST_PROC(IN IN_KEY VARCHAR(20), IN IN_TS TIMESTAMP)
MODIFIES SQL DATA
BEGIN ATOMIC
INSERT INTO TS_STORE(ID_COL, TS)
VALUES (IN_KEY, IN_TS);
END;
and attempt to call it as:
CALL TEST_PROC('key2', '2020-02-19 12:17:53');
Then I get an error: "incompatible data type in conversion".
This is a problem for me, since I am not allowed to change the signature of the the stored procedure to bypass the problem, since in my case HSQL is used as a mock for a production database (DB2) where the equivalent procedure works as expected.
It works if you call the procedure with a TIMESTAMP value, as opposed to the character string.
CALL TEST_PROC('key2', TIMESTAMP'2020-02-19 12:17:53');

cannot execute store procedure in phpmyadmin

Error in processing request: No routine with name `selectCategory` found
in database `xxx`.
This is my stored procedure
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectCategory`()
BEGIN
SELECT category_id, name AS display_name
FROM category
ORDER BY name;
END$$
DELIMITER ;
I am trying to run the stored procedure on the phpmyadmin site to check if the other problem I am having is due to php I wrote or the procedure itself, because of that error I cannot check and I do not know how to fix this error.
Is it possible that you are connecting to an instance which does not have the SP? check the db instance contains your SP.

DB2 alter a stored procedure

I am new to DB2 and am stuck in altering a stored procedure.
I am on DB2 9.1 version, AIX.
I wrote following SQL in Toad (free version):
ALTER PROCEDURE THE_USER.BONUS_MAIN ( )
REPLACE VERSION V1 ( )
MODIFIES SQL DATA
CALLED ON NULL INPUT
INHERIT SPECIAL REGISTERS
OLD SAVEPOINT LEVEL
BEGIN
...
END
I got error message immediately:
ERROR [42601] [IBM][DB2/AIX64] SQL0104N An unexpected token "ALTER PROCEDURE THE_USER.BONUS_MAIN" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "".
Any tip?
There is no ALTER PROCEDURE statement, you'll need to drop and recreate the procedure. Additionally, you're on such an old version (DB2 9.1 went out of support in April, 2012), you don't even have an option to use CREATE OR REPLACE PROCEDURE....

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;

Resources