DB2 LUW 9.X: Multirow insert in stored procedures - stored-procedures

what is the best way performancewise to insert multiple rows to a table from a stored procedure?
EXEC SQL
INSERT INTO DSN8A10.ACT
(ACTNO, ACTKWD, ACTDESC)
VALUES (:HVA1, :HVA2, :HVA3)
FOR :NUM-ROWS ROWS
END-EXEC.
The above is apparently only available in z/os but not in luw. Is there any equivalent?

"DB2 9.x" is a bit too broad of a specification. Beginning with DB2 9.7 you might use the UNNEST() table function, something like this:
insert into ACT (ACTNO, ACTKWD, ACTDESC)
select ACTNO, ACTKWD, ACTDESC from unnest (V_ARR) as (ACTNO, ACTKWD, ACTDESC);
assuming V_ARR is an array of row type with "columns" ACTNO, ACTKWD, and ACTDESC.

Related

How to insert CDC Data from a stream to another table with dynamic column names

I have a Snowflake stored procedure and I want to use "insert into" without hard coding column names.
INSERT INTO MEMBERS_TARGET (ID, NAME)
SELECT ID, NAME
FROM MEMBERS_STREAM;
This is what I have and column names are hardcoded. The query should copy data from MEMBERS_STREAM to MEMBERS_TARGET. The stream has more columns such as
METADATA$ACTION | METADATA$ISUPDATE | METADATA$ROW_ID
which I am not intending to copy.
I don't know of a way to not copy the METADATA columns if not hardcoding. However if you don't want the data maybe the easiest thing to do is to add them to your target, INSERT using a SELECT * and later in the sp set them to NULL.
Alternatively, earlier in your sp, run an ALTER TABLE ADD COLUMN to add the columns, INSERT using SELECT * and then after that run an ALTER TABLE DROP COLUMN to remove the columns? That way your table structure stays the same, albeit briefly it will have some extra columns.
A SELECT * is not usually recommended but it's the easiest alternative I can think of

DB2 Stored Procedure Not able to assign data to a variable

I have a simple stored procedure to calculate the sum of salaries of employees, sum of their squares and number of rows.
This is the stored procedure I have written:
I get an error in fetching the number of rows from the database and assigning it to a variable. What do I do? Using DB2 11.5
It helps to specify the exact error code when asking questions (don't write get an error, do write instead 'get error SQL0104N ...`.
Your mistake is that you have not followed the documented order for SQL statements in compound SQL blocks.
The SELECT statement can only appear after any cursor definitions, local procedures , and handlers if you have any.
So move the statement SELECT COUNT(*) INTO TOTAL_ROWS FROM EMPLOYEE; so that it appears after the DECLARE CURSOR1 ... line, the try to recompile.

Hive join query to list columns from only one table

I am writing a hive query to join two tables; table1 and table2. In the result I just need all columns from table1 and no columns from table2.
I know the solution where I can select all the columns manually by specifying table1.column1, table1.column2.. and so on in the select statement. But I have about 22 columns in table 1. Also, I have to do the same for multiple other tables ans its painful process.
I tried using "SELECT table1.*", but I get a parse exception.
Is there a better way to do it?
Hive 0.13 onwards the following query syntax works:
SELECT a.* FROM a JOIN b ON (a.id = b.id)
This query will select all columns from a. So instead of typing all the column names (making the query cumbersome), it is a better idea to use tablealias.*

INSERT INTO with unknown number of column in scriptella

I have to back up a table, which can change number of column. When my etl script starts it doesn't know the number of column. How can I create INSERT INTO table VALUES (?1, ?2, ...) script on the fly?
Regards,
Jacek
Depending on the database, one can use CREATE TABLE FROM SELECT (or similar) to back up the table. Example:
CREATE TABLE new_table AS (SELECT * FROM old_table);

Is it possible to obtain result set of a sp as a table that I can query?

Is it possitble to get a stored-procedure's result set as a table so that I can query that?
something like:
SELECT PK_Item, Count(PK_Item)
FROM (pMyStoredProcedure) --This sp returns a table that has PK_Item column
GROUP BY PK_ITEM
ORDER BY PK_ITEM DESC
I am not an T-SQL expert but my friend says it is kind of impossible to do this with sprocs.
Is not there any way? But without modifying the stored procedure.
thanks!
If you know the table structure that the sp will return using sql server 2005
you can use
declare #table table(
columns here...
)
INSERT INTO #table exec your_sp params
select * from #table

Resources