I want to create one INSERT statement with multiple values dynamically like to following example:
INSERT INTO table_name (ID, PARENT, NAME, ENABLED)
VALUES (1, 't1', 'Test1', 1 ), (2, 't2', 'Test2', 1 ), (3, 't3', 'Test3', 1 );
Currently I'm working with the following statement, but now I get too many individual statements and this doesn't deliver good performance
SELECT 'INSERT INTO table_name(ID, PARENT, NAME, ENABLED) '
|| 'VALUES ('|| ID ||','''|| PARENT ||''','''|| NAME || ''', '|| ENABLED ||');'
FROM table_name WHERE ID IN (... [inner select] ...);
How is it possible to create a loop which attached new values ins a single insert statement like this?
SELECT 'INSERT INTO table_name (ID, PARENT, NAME, ENABLED)' VALUES
DECLARE
myValues varchar2(500);
CURSOR myCur IS SELECT ID, PARENT, NAME, ENABLED FROM table_name;
BEGIN
FOR values IN myCur LOOP
IF myCur%ROWCOUNT = 1 THEN
myValues := '('||values.ID ||', '''|| values.PARENT ||''', '''|| values.NAME ||''', '|| values.ENABLED ||'),';
ELSE
myValues := '('||values.ID ||', '''|| values.PARENT ||''', '''|| values.NAME ||''', '|| values.ENABLED ||')';
end if;
END LOOP;
END;
--...
I need this to export my Data and insert into an other Database by using sqlplus
thanks all