Stored Procedures for IBM DB2 - sql

I am new to stored procedures. Need to pick up some stored procedures for DB2(which I am also new to). The code I wrote is not working:
CREATE PROCEDURE sp_test
(
id NUMBER
)
BEGIN
IF NUMBER < 0 THEN
RAISE VALUE ERROR;
END IF;
SELECT * FROM student_tb
WHERE taskid = 'NUMBER';
END;
/
and this is the script (another file) which calls the stored procedure:
BEGIN
sp_test('15');
END;
I am not sure what is wrong. Hope someone can advise. Thank you.

I'm not an expert in DB2, but I'm pretty sure you have to CALL stored procs in DB2.
call sp_test('15');
or
execute (call sp_test('15'));
The syntax might be a little different for stored procedures that return a result set. Docs for DB2 on your target platform should explain the difference.

Related

Call a stored procedure with OUT parameter within another stored procedure in SAP HANA

I am currently trying to send an Email from a stored procedure I have created using the native sys.staticserver_sendemail_dev function.
But both the procedures have OUT parameters declared when I try to invoke the email procedure in my I get an error stating the parameters are not declared.
I wanted to know how to call a stored procedure without parameters inside another stored procedure.
I understood from your question, that you have two stored procedures, where one calls the other and both have out parameters. This is a valid constellation. Please find a minimum example, which works on SAP HANA Cloud:
CREATE OR REPLACE PROCEDURE TEST_INNER (OUT i INTEGER)
AS
BEGIN
SELECT COUNT(*) INTO i FROM DUMMY;
END;
CREATE OR REPLACE PROCEDURE TEST_OUTER (OUT another_i INTEGER)
AS
BEGIN
DECLARE outparam INTEGER;
CALL TEST_INNER(:outparam);
another_i = :outparam;
END;
CALL TEST_OUTER(?);
If this does not solve your issue, you need to provide more details as well as minimal code snippet similar to the one above to reproduce the issue.

What have i gotten wrong for this stored procedure not to execute?

Here is the stored procedure and how I created it
CREATE OR REPLACE STORED PROCEDURE SimpleSelect()
AS
BEGIN
SELECT * FROM Permissions
END;
/
CALL SimpleSelect();
When creating the procedure I get the error
"Success with some compilation errors"
and when running the CALL command I get that the SQL command not properly ended.
It is important to know which database do you use. Some of this advice's will come in handy in many databases but some will not...
Do you have a table called Permissions already created ? If not, create it.
Please put a ; after the SELECT statement. Like this:
SELECT * FROM Permissions;
In MySQL this will not return error:
CREATE PROCEDURE SimpleSelect()
BEGIN
SELECT * FROM Permissions;
END;
/
When you fix your procedure the call command will work just fine...
Cheers!
Here is the DEMO

Can you call a Netezza stored procedure more than once in Select?

I created a simple stored procedure for testing purposes in Netezza that returns a string. When I call it from a select statement, it works fine, unless I call it more than once on multiple columns. I get the error:
ERROR [HY000] ERROR: Can't use a stored procedure in this context.
Is this not allowed?
Stored procedure:
CREATE OR REPLACE PROCEDURE SP_TEST_PROC(VARCHAR(ANY))
RETURNS VARCHAR(32)
EXECUTE AS OWNER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
TEST_PAR ALIAS FOR $1;
BEGIN
RETURN 'A' || TEST_PAR;
END;
END_PROC;
How I call it:
SELECT SP_TEST_PROC('abc') as test1, sp_test_proc('def') as test2
You cannot call more than one stored procedure in a SELECT. There are a couple of ways that you can call a Netezza stored procedure:
CALL SP_TEST_PROC('abc');
EXEC SP_TEST_PROC('abc');
SELECT SP_TEST_PROC('abc');
However, the you cannot have a FROM clause when using SELECT. The SELECT form is just a synonym for one of the other forms, and is not really a SELECT as we would normally think of one.
You can find the documentation on calling a stored procedure here.
If you are looking for a scalar function, then you are probably better served by writing a UDF. However, UDFs in Netezza do not support NZPLSQL as a language. You would have to write it in one of the supported UDF languages (e.g C++ or Lua).
To execute a stored procedure you must run this command:
EXECUTE SP_TEST_PROC('abc')
EXECUTE SP_TEST_PROC('def')
and not as you've written:
SELECT SP_TEST_PROC('abc') as test1, sp_test_proc('def') as test2
You can go here for an example

how to excecute a simple oracle procedure with out parameter

I am new to oracle procedures so I'm having a bit of trouble with this. I have an oracle procedure defined like
PROCEDURE USERPROGRESS (
queryType IN varchar2,
o_Cursor OUT cur_Cursor) IS ...
Notice how it has the out cur_Cursor. At the end of the procedure the procedure does
OPEN o_Cursor FOR sql_string;
How exactly do I can I call this procedure? I'm having trouble finding any helpful examples. Thanks.
Check this link..
http://www.oracle-base.com/articles/misc/UsingRefCursorsToReturnRecordsets.php
Depending on how you declared cur_Cursor, you can define a variable in your procedure and then fetch and display the results as needed.

Informix: procedure with output parameters?

I searched a lot, but couldn't find anything.. I just want to ask if there's any way to create and call a procedure (Informix) with out parameters. I know how to return one or more values (for procedures and for functions), but this is not what I want. It would be really strange, if Informix does not allow output parameters..
Thanks in advance!
EDIT: Yes, I saw it's possible, but I still can't execute such procedure. For example:
CREATE PROCEDURE mytest(batch INT,OUT p_out INT)
DEFINE inc INTEGER;
LET inc = 1;
LET p_out = 5;
END PROCEDURE;
and what I receive is:
The routine mytest can not be resolved
and this happens only on executing functions with output parameters..
Why do you need 'out' parameters? Informix procedures can return multiple values from a single call (or, in this case, a single value):
CREATE PROCEDURE mytest(batch INT) RETURNING INT AS p_out;
DEFINE inc INTEGER;
DEFINE p_out INTEGER;
LET inc = 1;
LET p_out = batch + inc;
RETURN p_out;
END PROCEDURE;
There are only a limited number of places where you can use an OUT parameter. One is in a query - there is a name SLV (statement local variable) that turns up in some error messages. I believe there's a way to get to OUT parameters via Java (JDBC) too. AFAIK, other APIs do not allow it.
Code written for Informix assumes that it won't need output parameters. Code migrated to Informix from other (impoverished?) systems that do not provide multiple output values from a single procedure need to be rethought to work sensibly with Informix.