Focal Point
[CLOSED] SQL PASS THRU - insert multiple rows to a table

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/5917041196

February 26, 2019, 02:18 PM
Riya
[CLOSED] SQL PASS THRU - insert multiple rows to a table
Hi,
I have a hold file with multiple rows and I need to insert the rows to a table in database using SQL pass thru via scheduled report every week.

Earlier I tried using just one row with all values in the INSERT statement and it works but how to insert multiple rows from a hold file to a table(similar toe ETL)? Also the table has WF_ID as the primary key.

Please let me know. Thanks in advance.

RK

This message has been edited. Last edited by: FP Mod Chuck,


WebFOCUS 8.1.05
Excel, PDF, Pivot, HTML
Windows
February 26, 2019, 02:58 PM
BabakNYC
Have you considered HOLD FORMAT SQLORA or whatever RDBMS you want to populate? It seems a lot less effort than what you describe.


WebFOCUS 8206, Unix, Windows
February 26, 2019, 03:22 PM
Riya
HOLD FORMAT to RDBMS creates a new table.
I need to load it to the existing table with new rows based on the report date. Also increment the auto generate the primary key.

Thank you
RK


WebFOCUS 8.1.05
Excel, PDF, Pivot, HTML
Windows
February 26, 2019, 03:28 PM
Waz
Do you need to use SQL pass thru ?

You could use MODIFY (Depending on the content like clobs and blobs)


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

February 27, 2019, 06:00 AM
Wep5622
If your database supports creating multiple VALUES rows, you can generate the SQL for that.

For example:
INSERT INTO CAR (COUNTRY, CAR)
VALUES
  ('JAPAN', 'MAZDA')
, ('ITALY', 'FIAT')
, ('FRANCE', 'PEUGEOT')
, ('FRANCE', 'CITROEN');


The lines under VALUES is what you would generate the code for.


WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
February 27, 2019, 07:07 AM
Addy
To add to Wep's suggestion
 -DEFAULTH &LOOP_CNTR = 0;
-DEFAULTH &CAR = '';
-DEFAULTH &COUNTRY = '';
-DEFAULTH &MODEL = '';

TABLE FILE CAR
PRINT
CAR
COUNTRY
MODEL 
ON TABLE SAVE AS COL_VALUES
END
-RUN
-SET &LOOP_CNTR = &LINES;
-REPEAT :Fetch_Data &LOOP_CNTR TIMES

-READ COL_VALUES  &CAR.A16 &COUNTRY.A10 &MODEL.A20

-TYPE VALUES ARE &CAR &COUNTRY &MODEL
-*Have your sql statement here
 INSERT INTO CAR (COUNTRY, CAR, MODEL)
 VALUES
('&COUNTRY', '&CAR','&MODEL');

-:Fetch_Data 



WF 8.2.04
Windows/Unix
All Formats
In Focus since 2006
February 27, 2019, 07:50 AM
Frans
How many rows do you want to insert? Looping with insert statements is limited, MODIFY is the best way to properly insert data in existing tables.


Test: WF 8.2
Prod: WF 8.2
DB: Progress, REST, IBM UniVerse/UniData, SQLServer, MySQL, PostgreSQL, Oracle, Greenplum, Athena.
February 27, 2019, 08:01 AM
Wep5622
Actually, that looks like a great task for my new favourite WebFOCUS function: PUTDDREC

-SET &LIST = 'INSLIST';
FILEDEF &LIST DISK INSLIST.ftm

TABLE FILE CAR
PRINT
  COMPUTE COMMA/A1 = IF LAST COUNTRY EQ '' THEN ' ' ELSE ','; NOPRINT
  COMPUTE INSTUPLE/A256 = COMMA | '(''' || COUNTRY || ''', ''' || CAR || ''', ''' || MODEL || ''')'; NOPRINT
  COMPUTE RESULT/I1 = PUTDDREC('&LIST', &LIST.LENGTH, INSTUPLE, ARGLEN(256, INSTUPLE, 'I3'), RESULT); NOPRINT

-* Other useful stuff you may want to hold unrelated to the insert list

BY COUNTRY NOPRINT
BY CAR NOPRINT
BY MODEL NOPRINT
ON TABLE HOLD
END
-RUN

SQL SQLMSS PREPARE SQLOUT FOR
INSERT INTO CAR (COUNTRY, CAR, MODEL)
VALUES
-INCLUDE INSLIST.ftm
;
END



WebFOCUS 8.1.03, Windows 7-64/2008-64, IBM DB2/400, Oracle 11g & RDB, MS SQL-Server 2005, SAP, PostgreSQL 11, Output: HTML, PDF, Excel 2010
: Member of User Group Benelux :
February 27, 2019, 08:51 AM
Addy
quote:
PUTDDREC

Good One


WF 8.2.04
Windows/Unix
All Formats
In Focus since 2006
February 27, 2019, 10:07 AM
Riya
quote:
Originally posted by Frans:
How many rows do you want to insert? Looping with insert statements is limited, MODIFY is the best way to properly insert data in existing tables.


Every week 200 to 300 rows.


WebFOCUS 8.1.05
Excel, PDF, Pivot, HTML
Windows
February 27, 2019, 10:09 AM
Riya
This helps, I will try and update you.:FETCH_DATA is the webfocus command? What does it do?

Thank you

quote:
Originally posted by Addy:
To add to Wep's suggestion
 -DEFAULTH &LOOP_CNTR = 0;
-DEFAULTH &CAR = '';
-DEFAULTH &COUNTRY = '';
-DEFAULTH &MODEL = '';

TABLE FILE CAR
PRINT
CAR
COUNTRY
MODEL 
ON TABLE SAVE AS COL_VALUES
END
-RUN
-SET &LOOP_CNTR = &LINES;
-REPEAT :Fetch_Data &LOOP_CNTR TIMES

-READ COL_VALUES  &CAR.A16 &COUNTRY.A10 &MODEL.A20

-TYPE VALUES ARE &CAR &COUNTRY &MODEL
-*Have your sql statement here
 INSERT INTO CAR (COUNTRY, CAR, MODEL)
 VALUES
('&COUNTRY', '&CAR','&MODEL');

-:Fetch_Data 



WebFOCUS 8.1.05
Excel, PDF, Pivot, HTML
Windows
February 27, 2019, 10:47 AM
Addy
Hi Riya,
:Fetch_data is a label that I created so that I can loop throught a segment.

You can call it whatever you want. I generally create my labels starting with : but it is not a compulsion.

have a look at following documentation

Looping in WF
Branching


WF 8.2.04
Windows/Unix
All Formats
In Focus since 2006
February 27, 2019, 01:42 PM
jgelona
Depending on the volume I use SQL Insert, the SQL Loader, and MODIFY. For just a few hundred records, I'd use MODIFY and if all you are doing is inserting then make sure to use "SET SQL LOADONLY" right after the MODIFY statement. This tell MODIFY that all you are doing is loading and it cuts the I/O in half. Without it, MODIFY doesn't know if you are loading, updating or both. As a result, MODIFY will do an unnecessary SQL SELECT first to see if the row exist then do the SQL INSERT when it doesn't. You can see this if you turn traces on.


In FOCUS since 1985. Prod WF 8.0.08 (z90/Suse Linux) DB (Oracle 11g), Self Serv, Report Caster, WebServer Intel/Linux.
February 28, 2019, 03:46 PM
Fernando
Riya, Using BabakNYC's idea.

Do your
 
HOLD FORMAT SQLORA
 


into a temp table (say TEMP_DATA)

Then run an insert into your table using the temp table.

Insert into MY_TABLE (col1,col2)
Select a.col1 AS col1, 
a.col2 AS col2
FROM TEMP_DATA


Fernando


Prod WF 8.1.04, QA WF 8.2.03, Dev WF 8.2.03
February 28, 2019, 04:00 PM
Waz
Fernando, thats a great idea.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!