INSERT INTO ERT_TY
(z_DB_NAME,z_TABLE_NAME,z_COLUMN_NAME,S_COLUMN_TYP E,z_DATA)
SELECT 'TRAIN12','ALBUM',
'DEPTNAME','CHAR',DEPTNAME
FROM TRAIN12.DEPT FETCH FIRST 5 ROWS ONLY;
Error:
ILLEGAL USE OF KEYWORD FETCH. TOKEN WITH UNION EXCEPT QUERYNO WAS EXPECTED. SQLCODE=-199, SQLSTATE=42601
My db2 : version8 Z/os
os: windows
From the docs I got that FETCH will not work inside a subquery.
Can some one kindly let me know , how i have to insert the first 5 rows of a table into some other table.
How do you identify the 'first 5 rows' of a table?
Do you have any specific records to be inserted into the table OR you just want some random 5 rows from another table?
If you want specific rows then you can use a 'where' clause in your select query. Otherwise, you may have to write something like:
Code:
INSERT INTO ERT_TY
(z_DB_NAME,z_TABLE_NAME,z_COLUMN_NAME,S_COLUMN_TYP E,z_DATA)
SELECT 'TRAIN12','ALBUM',
'DEPTNAME','CHAR',DEPTNAME
FROM TRAIN12.DEPT where condition UNION ALL
SELECT 'TRAIN12','ALBUM',
'DEPTNAME','CHAR',DEPTNAME
FROM TRAIN12.DEPT where condition;
Repeat the Select query 5 times with Union All. So, there will be 5 select queries and each query should return one row to be added. I don't see if you can actually insert a fixed number of rows through a Select query in a single shot.