Insert array of JSON objects in db2 stored procedure - stored-procedures

I'm trying to pass an array of JSON objects to the Stored procedure, so that each JSON object will be stored in a single row of the table.
for an example:
to insert a single entry: INSERT INTO SCHEMA_NAME.TABLE_NAME (id,name) values (1,'my_name')
now i have an array of such data which I want to push into stored procedure to get all data inserted into table without hitting SP again and again.
array example: [{id:1,name:'name1'},{id:2,name:'name2'}]
now i can have number of object to be stored.
so either i would hit the SP again and again to push each object individually or i would pass whole array and run a loop inside a stored procedure to get it done.
I have DB2 10.5 LUW with fixpack 7 installed and using node.js

DB2 10.5 does not support JSON as a native data type, so you won't be able to do this without passing the JSON as a VARCHAR and having the stored procedure parse/deconstruct the JSON. The obvious way to do this would with a C or Java stored procedure.
That said, there are some JSON capabilities (see this series of articles for more details, but this is almost certainly not what you want.

Related

ATOMIC INSERT in STORED PROCEDURE

I'm fairly new to stored procedure. I have to design a stored procedure for ATOMIC insert (Mass insert). I'm using COBOL program to call the stored procedure in DB2. I will store values in array and have to insert all at one shot. Below is the query we are using in COBOL program and which I have to convert to stored procedure.
INSERT INTO TABLE_NAME
(COLUMN1
,COLUMN2
,COLUMN3
,COLUMN4
,COLUMN5)
VALUES
(VALUE1
,VALUE2
,VALUE3
,VALUE4
,VALUE5)
FOR WS-SUB ROWS
ATOMIC
VALUE1,VALUE2,VALUE3,VALUE4,VALUE5 are array elements and WS-SUB is number of occurrence.
I want to know, If I can handle array in stored procedure or want to know if its possible to do ATOMIC insert in DB2 stored procedure.
Thanks in advance.
Following the documentation for DB2 on z/OS 12.0.0:
A DB2 Stored Procedure may be configured to use arrays as parameter types, see Example of using arrays in an SQL procedure.
However, if your intention is calling this from COBOL you may run into issues as the documentation for Supported SQL data types in COBOL embedded SQL applications indicates:
Arrays are not supported by the COBOL precompiler
Another approach would be to pass your data as something like a CLOB or VARCHAR delimited by a character and then parse it within your stored procedure.
By default DB2 Stored Procedures does not commit on return, so another option would be to iterate your COBOL tables and call the stored procedure repeatedly.

Table valued Parameters equivalent in db2

I am trying to pass a collection of Employee objects from Java code to a DB2 Stored Procedure. In SQL server I can do that by making use of Table-Valued Parameters. In DB2 how can I achieve this?
Refer to Invoking stored procedures with ARRAY of ROW parameters in JDBC applications.
Or use Declared Global Temporary Table creating it and inserting rows into it beforehand and use this DGTT in your SP as ordinary table.

Can I have table variable in stored procedure in redshift?

We are planning to create a procedure for our logic what should be in PL SQL in redshift (using workbench).
Can we use a table variable to traverse through the rows of the table ? Like we have dataframe in Python.
Sort of. Redshift implements a RECORD data type for stored procedures. Variables with this type can hold an arbitrary sets of rows.
"Overview of stored procedures in Amazon Redshift" > "Record types"
However, note that you cannot currently SELECT from a RECORD typed variable, only loop over the content.
There are several examples of using a RECORD variable in our GitHub repository: "amazon-redshift-utils/src/StoredProcedures/"

How to Store an Aggregated Value in a Stored Procedure?

I am using SQL Server 2012 and trying to write a stored procedure that will have two components:
1. Select statement that will return several rows
2. Another component will have an aggregated value based on some conditions.
So, for the second component, I am thinking to declare a variable in the stored procedure and store the aggregated data there.
Now, my question is can I populate my output parameter with the data stored in previously declared variable and access the parameter from SSRS 2012?
I would appreciate any suggestion(s) to get me started.
Thank you

Interbase 6 and stored procedure array parameters

Does Interbase 6 database support passing array parameter to stored procedure? If yes what is the syntax of declaring such variable and using it in the body of procedure?
It looks like even the current version of IB does not support array parameters.
The documentation includes an example of processing array data within the stored procedure to flatten it out, then passing several flat records out of the stored procedure. Of course you could do all this in IB 6 as well.
This example is in the Data Definition manual for XE. See Using stored procedures.
The Interbase documentation on this topic may be useful.

Resources