How can we call stored procedures in Netezza database? - stored-procedures

I am not able to call stored procedures in Netezza database.
Below is my procedure.
CREATE OR REPLACE PROCEDURE "SUJAY"."ADMIN"."EMPSELECT"()
RETURNS INTEGER
LANGUAGE NZPLSQL AS
BEGIN_PROC
BEGIN
select * from Employee;
END;
END_PROC;
When I run this in procedure editor in DB visualizer it throws below error.
[Code: 1100, SQL State: HY000] ERROR: Can't have a SELECT without INTO. Use CALL instead.

To resolve this error, you will need to modify your SELECT statement to include an INTO clause, which assigns the result of the SELECT statement to a variable. Here is an example of a SELECT statement that includes an INTO clause: something like this...
SELECT COUNT(*) INTO my_variable FROM mytable WHERE somecondition = false;
If you want to execute the SELECT statement without capturing the result and without an INTO clause, you could use the CALL statement instead of SELECT, this way it will not raise an exception.
CALL EMPSELECT();

Related

Multiple Session executing Snowflake Procedure Insert into same Table

We have a snowflake procedure that insert record into a table, something similar to below procedure. When this procedure is called by multiple application in multiple session, it is behaving erratic and throwing error. I understand when multiple session is trying to write to same table it is throwing error. Is there any work around to handle this scenario, since we do want to duplicate this proc for every application. Thanks in advance.
CREATE OR REPLACE PROCEDURE "SP_CRT_METADATA"(SOURCE_TYPE VARCHAR, BATCH_ID FLOAT)
RETURNS VARCHAR(16777216)
LANGUAGE JAVASCRIPT
EXECUTE AS OWNER
AS '
hist_col_load =` INSERT INTO SHRD_COL_MSTR_HIST SELECT * FROM SHRD_COL_MSTR WHERE SRC_TYPE =''`+SOURCE_TYPE+`'' AND ACTIVE_IND =''Y''`;
snowflake.execute({sqlText: hist_col_load});
'

DB2 calling stored Procedure in conditional statement

I need to query a stored procedure and on the basis of result set of that procedure need to make decisions in conditional statement
For instance I have a stored Procedure "Main_SP"
Now if result of "Main_SP" is 'null' then the result should be 'Tweety', but if th result set is not null then result set should be retrieved,
how to do it?
I tried following and some other but none worked.
SELECT
case Main_SP('MyVariable')
when 'null'
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
SELECT
case Main_SP('MyVariable')
when null
then 'Tweety' end
FROM SYSIBM.SYSDUMMY1 WITH UR
It is failing the condition, In first command when even it is 'null' it is not printing 'Tweety'.
and while using second, Getting error that 'Null' is not valid in the context.
I don't believe you can use a stored procedure that way.
A stored procedure can return data in two ways
As a result set
in an output or input/output parm
You're not using option 2 and I don't think a result set is ever NULL. There might be no records, but the RS itself isn't NULL. In addition, from what I can see in the manuals, processing a RS from a stored proc inside another stored proc in DB2 requires you to declare an allocate result set locators. But I've never done this.
If your procedure returns a single value or null, you'd be better served by defining it as a function with a return value. Then your code would be simply:
SELECT
COALESCE(Main_Fnc('MyVariable'),'Tweety')
FROM
SYSIBM.SYSDUMMY1 WITH UR
You can't call a stored procedure as part of an SQL fullselect; you would need to have the client that calls the stored procedure handle this logic. If you must do it on the server, implement a new stored procedure that calls Main_SP itself and contains your logic to interpret the result.

Send a List Parameter to DB2 Stored Procedure

I have a db2 stored procedure which has an sql statement using the 'recseq In(1,2,3)' syntax.
I have added a parameter(Character(5) to hold the list but in my call statement I cannot figure out the correct syntax.
I have tried call myprocedure(1, '1,2,3') but this generates a Conversion error
Note: call myprocedure(1, '1') will work but just not the list style.

Informix 4GL and triggers

I want a simple SQL (trigger) for duplicate checking.
My table name is test1 with 2 columns, code and sname. Before inserting a new record, check if the record already exists: if it does, generate an error and do not insert; if it does not, let the insert proceed.
How do I do that?
The simplest, most reliable way to ensure that there is no duplicate data in the table is not using triggers at all, but using UNIQUE or PRIMARY KEY constraints:
CREATE TABLE test1
(
code INTEGER NOT NULL PRIMARY KEY,
sname VARCHAR(32) NOT NULL UNIQUE
);
The four constraints (two NOT NULL, one PRIMARY KEY, one UNIQUE) automatically ensure that no duplicate records are inserted into the table.
If you choose to add a trigger, it will be duplicating the work that is done by these constraints.
As to how to do it, you will need to create a stored procedure which is invoked from the trigger statement. It will be given the new code and new name, and will do a SELECT to see whether any matching record occurs, and will raise an exception if it does and will not raise an exception if not.
CREATE PROCEDURE trig_insert_test1(ncode INTEGER, nname VARCHAR(32))
DEFINE ocode INTEGER;
FOREACH SELECT code INTO ocode
FROM test1
WHERE code = ncode OR sname = nname
RAISE EXCEPTION -271, -100, "Value to be inserted already exists in table test1";
END FOREACH;
END PROCEDURE
Then you use:
CREATE TRIGGER ins_test1 INSERT ON test1
REFERENCING NEW AS NEW
FOR EACH ROW (EXECUTE PROCEDURE ins_trig_test1(new.code, new.sname))
In Informix 4GL, you can either create strings containing these statements, and then PREPARE and EXECUTE (and FREE) them, or you can use SQL blocks:
SQL
CREATE TRIGGER ins_test1 INSERT ON test1
REFERENCING NEW AS NEW
FOR EACH ROW (EXECUTE PROCEDURE ins_trig_test1(new.code, new.sname))
END SQL
But, as I said at the outset, using triggers for this is not the best way to go; it is redundant given the table definition.
I've not run any of the SQL or SPL past the server; you'll need to check that the semi-colons are in the right places in the SPL, as SPL is fussy about that.
You can find the syntax for the SQL and SPL statements in the Informix 11.70 Information Centre.

Replace stored procedure with MyBatis mapping

I can not find an example of mapping in MyBatis that I can replace the below code with.
"if sqlcode <> 0" If no update takes place then do an insert
Any sugestions? :)
as
begin
execute SetDefaultIsolationLevel
update COMPANYLEVEL
set
companylevelid = #companylevelid,
companynameid = #companynameid,
level = #level,
memo = #memo,
operator = #operator,
changed = getdate(*)
where
companynameid = #companynameid
if sqlcode <> 0
BEGIN
insert into COMPANYLEVEL
(companylevelid,companynameid,level,memo,operator,changed)
values
(#companylevelid,#companynameid,#level,#memo,#operator,getdate(*))
END
commit transaction
end
I don't think MyBatis has any mapping to say "try an update, if that fails do an insert". If you want that done in one round trip to the database, then a stored procedure is appropriate. You can call this stored procedure from MyBatis, but the if/else logic would be in the stored proc.
If you are trying to get rid of the stored proc, then you'll need a two step check in your code. An update in MyBatis returns the number of rows updated (via the JDBC driver), so if that is zero, then you can call a MyBatis insert mapping. In cases where an insert occurs, it would require two round trips to the database.
You could also do an "upsert" using a MERGE statement in a stored proc, but that of course isn't related to MyBatis other than MyBatis can call your stored proc. It looks like you are using Sybase? If so, I'm not sure if Sybase has upserts - link to research: Upsert (update or insert) in Sybase ASE?

Resources