db2 stored procedure to create a select query dynamically - stored-procedures

I am new to IBM db2 stored procedure, what I am trying to do is to get the values of a column from a table and build a select query based on these values, this is what I have tried, not sure how to proceed
CREATE TYPE currencySymbols AS VARCHAR(20) ARRAY[100]#
CREATE PROCEDURE ins_curr_ano(IN crsymbol VARCHAR(20), IN cost1 integer, IN cost2 integer, IN teirId integer)
BEGIN
DECLARE currencies currencySymbols;
DECLARE maxCount INTEGER DEAFULT 0;
set currencies = ARRAY[SELECT distinct(CURR_SYMBOL) as currencySymbols FROM CURRENCY_MAPPING];
set maxCount = CARDINALITY(currencies);
for i in 1..maxCount loop
dbms_output.put_line(i);
end loop;
END#
Below is the error I am getting:
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "loop" was found following "for i in
1..maxCount". Expected tokens may include: "(". LINE NUMBER=13.
SQLSTATE=42601

That for ... loop statement in your code has PL/SQL syntax, while everything else has DB2 SQL PL syntax. You cannot mix the two in the same routine.

Related

How do I list all procedures that can be used in SQL queries in Informix

In Informix, I can create procedures like these:
CREATE TABLE t (i INT);
CREATE PROCEDURE p1()
RETURNING INT
RETURN 1;
END PROCEDURE;
CREATE PROCEDURE p2()
RETURNING INT
INSERT INTO t VALUES (1);
RETURN 1;
END PROCEDURE;
Now, I can call both procedures using the EXECUTE syntax:
EXECUTE PROCEDURE p1();
EXECUTE PROCEDURE p2();
But I can only execute the first one in a SELECT statement:
-- Works
SELECT p1();
-- Fails with SQL Error [IX000]: Illegal SQL statement in SPL routine.
SELECT p2();
This is documented here in the manual.
I cannot seem to distinguish the two types of procedure from the dictionary, e.g.
SELECT procname, isproc
FROM sysprocedures
WHERE procname IN ('p1', 'p2');
Yields:
|procname|isproc|
|--------|------|
|p1 |f |
|p2 |f |
Other fields from sysprocname are also not helpful. How can I recognise a procedure that contains such "illegal SQL statements"? (I'd like to avoid parsing the sysprocbody contents)

Redshift Stored Procedure - [Amazon](500310) Invalid operation: syntax error at or near "$1";

My Agenda is to store the counts of the 2 tables ( being passed in the parameter ) and then do some more operations upon comparing the both.
PROBLEM -
Stored Procedure throwing Error :
CREATE OR REPLACE PROCEDURE dev.gp_count_matching_20191204(actual_tablename character varying(256), bkp_tablename character varying(256))
LANGUAGE plpgsql
AS $$
DECLARE
actual_table_name varchar(256);
backup_table_name varchar(256);
actual_count_query varchar(1024);
actual_count int;
backup_count_query varchar(1024);
backup_count int;
BEGIN
call dev.gp_test_error_handling_tablename_format(actual_tablename);
call dev.gp_test_error_handling_tablename_format(bkp_tablename);
actual_count:=(select count(*) as counts from actual_tablename);
--raise info 'Actual Table Name - %, Actual Table Count - %',actual_tablename,actual_count;
end;
$$
This throws the following Error while creating the stored procedure-
An error occurred when executing the SQL command:
CREATE OR REPLACE PROCEDURE dev.gp_count_matching_20191204(actual_tablename character varying(256), bkp_tablename character varying(256))
LANGUAGE pl...
[Amazon](500310) Invalid operation: syntax error at or near "$1";
1 statement failed.
Execution time: 0.99s
If I comment out the actual_count:=(select count(*) as counts from actual_tablename);
then the Stored Procedure gets created Successfully.
I guess it has something to do with me using the parameter ( since $1 points the first parameter ) in the query.
Since I am pretty new with Stored procedure, I unable to figure out the exact problem.
Thanks in Advance.
You need to use EXECUTE when running dynamic SQL. In your example the query is in parentheses but nothing is making it execute. To execute the query into a variable you using the INTO syntax
sql := 'SELECT …'
EXECUTE sql_var INTO result_var;
Please see the example Stored Procedures in our GitHub repo "Amazon Redshift Utils". https://github.com/awslabs/amazon-redshift-utils/tree/master/src/StoredProcedures
There are several examples that use dynamic SQL, such as https://github.com/awslabs/amazon-redshift-utils/blob/master/src/StoredProcedures/sp_split_table_by_range.sql
You can also use the below-given syntax:
SELECT INTO Count count(*) from table_name;
By doing this you are inserting the value of count(*) into variable Count. It works.

Is it possible to pass in a variable amount of parameters to a stored procedure in redshift?

I am trying to write a stored procedure in AWS Redshift SQL and one of my parameters needs the possibility to have an integer list (will be using 'IN(0,100,200,...)' inside there WHERE clause). How would I write the input parameter in the header of the procedure so that this is possible (if at all?)
I've tried passing them in as a VARCHAR "integer list" type thing but wasn't sure then how to parse that back into ints.
Update: I found a way to parse the string and loop through it using the SPLIT_PART function and store all of those into a table. Then just use a SELECT * FROM table with the IN() call
What I ended up doing was as follows. I took in the integers that I was expecting as a comma-separated string. I then ran the following on it.
CREATE OR REPLACE PROCEDURE test_string_to_int(VARCHAR)
AS $$
DECLARE
split_me ALIAS FOR $1;
loop_var INT;
BEGIN
DROP TABLE IF EXISTS int_list;
CREATE TEMPORARY TABLE int_list (
integer_to_store INT
);
FOR loop_var IN 1..(REGEXP_COUNT(split_me,',') + 1) LOOP
INSERT INTO int_list VALUES (CAST(SPLIT_PART(split_me,',',loop_var) AS INT));
END LOOP;
END;
$$ LANGUAGE plpgsql;
So I would call the procedure with something like:
CALL test_string_to_int('1,2,3');
and could do a select statement on it to see all the values stored into the table. Then in my queries the need this parameter I ran:
.........................
WHERE num_items IN(SELECT integer_to_store FROM int_list);

Getting invalid identifier and SQL ignored errors in stored procedure in Oracle

I am trying to create a stored procedure in order to insert some data into some table in Oracle 11g.
For this purpose, I need to read the last amount from the latest insert for that user, and then add it to my new value and save the changes as a new row and if any kind of exception is occurred just rollback. This is what I have come up with so far:
CREATE OR REPLACE PROCEDURE MYTESTDB.INSERT_INTO_TESTBANK
(
ID IN TBLTESTBANK.ID%TYPE,
USERID IN TBLTESTBANK.USERID%TYPE,
TYPE IN TBLTESTBANK.TYPE%TYPE,
AMOUNT IN TBLTESTBANK.AMOUNT%TYPE,
DATETIMESTAMP IN TBLTESTBANK.DATETIMESTAMP%TYPE,
TRANSACTION_ID IN TBLTESTBANK.IDTRANS%TYPE,
TOTAL_MONEY IN TBLTESTBANK.TOTALMONEY%TYPE,
COMPUTED_HASH IN TBLTESTBANK.HASH%TYPE
)
IS
BEGIN
DECLARE
LastAmount TBLTESTBANK.TOTALMONEY%TYPE;
BEGIN
SELECT TBLTESTBANK.TOTALMONEY INTO LASTAMOUNT
FROM
(
SELECT TBLTESTBANK.ID
FROM TBLTESTBANK tblbnk
WHERE tblbnk.USERID = USERID
order by max(tblbnk.DATETIMESTAMP)
)
where ROWNUM<2;
EXCEPTION WHEN NO_DATA_FOUND
THEN LastAmount := 0;
END;
LastAmount := LastAmount+ AMOUNT;
INSERT INTO TBLTESTBANK (ID, USERID, TYPE, LastAmount,
DATETIMESTAMP, IDTRANS, TOTALMONEY, HASH
)
VALUES (ID, USERID, TYPE, AMOUNT, DATETIMESTAMP, TRANSACTION_ID, TOTAL_MONEY, COMPUTED_HASH);
COMMIT;
EXCEPTION WHEN OTHERS
THEN
ROLLBACK;
END;
/
Whenever I try to test it, I get a red wiggly line under the first SELECT , and then he TESTBANK.ID in the inner select statements. For the select command it says, sql statement is ignored, and for the TESTBANK.ID it says invalid identifier!
It also doesn't let me add two variables, and keeps saying LastAmount must be declared.
And these are the errors I get :
Error(19,7): PL/SQL: SQL Statement ignored
Error(22,16): PL/SQL: ORA-00904: "TBLTESTBANK"."ID": invalid identifier
Error(32,3): PLS-00201: identifier 'LASTAMOUNT' must be declared
Error(32,3): PL/SQL: Statement ignored
Error(34,3): PL/SQL: SQL Statement ignored
Error(34,47): PL/SQL: ORA-00904: "LASTAMOUNT": invalid identifier
You have two problems:
In your select statement:
SELECT TBLTESTBANK.ID
FROM TBLTESTBANK tblbnk
WHERE tblbnk.USERID = USERID
order by max(tblbnk.DATETIMESTAMP)
You have given the table TBLTESTBANK the alias tblbnk therefor you must use the alias throughout the statement:
SELECT tblbnk.ID -- use the correct alias here
FROM TBLTESTBANK tblbnk
WHERE tblbnk.USERID = USERID
order by max(tblbnk.DATETIMESTAMP)
Second:
in the INSERT statement you have to list columns not values.
INSERT INTO TBLTESTBANK (ID, USERID, TYPE, LastAmount,
DATETIMESTAMP, IDTRANS, TOTALMONEY, HASH
)
should be:
INSERT INTO TBLTESTBANK (ID, USERID, TYPE, AMOUNT, --- column name instead of variable name
DATETIMESTAMP, IDTRANS, TOTALMONEY, HASH
)
And then of course you need to use LastAmount in the values part, not AMOUNT
In general it's not a good idea to have variables with the same name as columns of tables that you use in the procedure. It's easy to shadow a variable with a column. You should rename the variables to avoid any problems there.
Edit (I didn't notice this at first).
You also have an error in the structure of the code. A stored procedure (or function) does not have a DECLARE section:
Your declaration needs to look like this:
CREATE OR REPLACE PROCEDURE MYTESTDB.INSERT_INTO_TESTBANK
(
....
)
IS
-- no DECLARE
-- variables right after the IS keyword
LastAmount TBLTESTBANK.TOTALMONEY%TYPE;
BEGIN
...
END;
You can try below select query to get latest record.
SELECT TB.TOTALMONEY INTO LASTAMOUNT
FROM
TESTBANK TB
WHERE
TB.USERID=USERID
AND
DATETIMESTAMP= (SELECT MAX (TB2.DATETIMESTAMP) FROM TESTBANK TB2
WHERE TB2.USERID=USERID);

Informix procedure — how to return an empty table?

I need to create an Informix procedure to return a table with rows if I found some value and an empty table if no value found.
I know how to return a table with rows, but I don't know how to return the empty table; can anyone help?
CREATE row type AType (
id VARCHAR(255),
name VARCHAR(255)
);
CREATE PROCEDURE getmeasurement (p_date DATETIME YEAR TO SECOND)
RETURNING MULTISET(AType NOT NULL);
DEFINE AType_TABLE MULTISET (AType NOT NULL);
DEFINE v_id VARCHAR(255);
DEFINE v_name VARCHAR(255);
....
IF( FoundValue ) THEN
-- INSERT INTO TABLE
INSERT INTO TABLE (AType_TABLE) VALUES (ROW(v_id,v_name)::AType);
ELSE
// how to initial a AType_TABLE instance with empty records.
END IF
....
RETURN AType_TABLE;
END PROCEDURE;
Despite what it says in the SQL Syntax Manual, SPL (Stored Procedure Language) procedures can return collection types (COLLECT, SET, MULTISET or LIST). I've reported a documentation bug against that — which misled me earlier this week.
I'm not able to get this procedure to compile under Informix 11.70.FC6 on Mac OS X 10.7.5, but the error it comes up with is:
SQL -9905: [Internal] No extended type information for domain.
I've run into various issues trying various variants of the above code. You can't have a WHERE clause on a DELETE from a multiset, it seems (different error message). You can also run into problems if you rollback the creation of the type and then try again.
However, I was able to test it with a pre-release of 11.70.FC8 on Linux (RHEL 5, x86/64) and got the desired output:
CREATE ROW TYPE IF NOT EXISTS AType
(
id VARCHAR(255),
name VARCHAR(255)
);
CREATE PROCEDURE getmeasurement (p_date DATETIME YEAR TO SECOND)
RETURNING MULTISET(AType NOT NULL);
DEFINE AType_TABLE MULTISET(AType NOT NULL);
DEFINE v_id VARCHAR(255);
DEFINE v_name VARCHAR(255);
LET v_id = "Polynomial - " || p_date;
LET v_name = "Euclid's Geometry of the Plane";
INSERT INTO TABLE (AType_TABLE) VALUES(ROW(v_id, v_name)::AType);
IF 1 = 1 THEN
-- how TO initial a AType_TABLE instance WITH empty records.
DELETE FROM TABLE(AType_TABLE);
END IF
RETURN AType_TABLE;
END PROCEDURE;
EXECUTE PROCEDURE getmeasurement(CURRENT);
ROLLBACK;
The output was:
MULTISET{}
which is an empty multiset. If you don't insert something into the multiset first, the delete fails. This is a trifle odd.
Summary
You may be OK with Informix 11.70.xC7; it may work on some other versions too. But there are likely to be a lot of versions of Informix where it does not work.

Resources