Hi,
DBeaver can do that even for the great DB2. Right click on the table and "Generate SQL". It will export all insert statements for each row.
On Wednesday, October 14, 1998 at 10:00:00 AM UTC+3, Styrk Finne wrote:
IS it possible to generate INSERT statements out of data in tables ??
If you have table employee with the following fields and data :
empno name address ------------------------------------------------
0001 Erik Bergen
0002 Lise Oslo
and what I want is the result :
Insert into employee values ('0001','Erik','Bergen');
Insert into employee values ('0002','Lise','Oslo');
This is an adapted version for Db2 LUW:
db2 -td@
CREATE OR REPLACE FUNCTION generate_insert(
schemaname VARCHAR(50),
tablename VARCHAR(50),
rownu INTEGER
)
RETURNS VARCHAR(2000)
BEGIN
DECLARE stmt1 VARCHAR(2000);
DECLARE stmt2 VARCHAR(2000);
DECLARE mycursor CURSOR
FOR dynsql;
SELECT
'SELECT ''INSERT INTO ' || tablename || ' VALUES (''||'
|| listagg(''''''''' CONCAT RTRIM(REPLACE(' || COLNAME || ','''''''',''''''''''''))||''''''''',' ||'','' || ')
|| '||'')'''
|| ' FROM ' || schemaname || '.' || tablename
|| ' WHERE RID() = ' || rownu
INTO stmt1
FROM SYSCAT.COLUMNS
WHERE TABSCHEMA = schemaname
AND TABNAME = tablename;
PREPARE dynsql FROM stmt1;
OPEN mycursor;
FETCH FROM mycursor INTO stmt2;
CLOSE mycursor;
RETURN stmt2;
END
@
Thanks to Ryane for the initial idea.
I will post this code in my GitHub's gists.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 399 |
Nodes: | 16 (2 / 14) |
Uptime: | 39:30:16 |
Calls: | 8,336 |
Calls today: | 13 |
Files: | 13,155 |
Messages: | 5,891,234 |
Posted today: | 1 |