Если вы потерялись во всех этих одинарных кавычках, я бы предложил вам использовать вместо этого CHR (39) (что равно одиночной кавычки).
Вот пример.
SQL> create table test (table_nm varchar2(10),
2 col_nm varchar2(10),
3 flg char(1));
Table created.
SQL> create table xyz (fname varchar2(10),
2 lname varchar2(10),
3 pid number,
4 filler1 varchar2(10),
5 filler2 varchar2(10),
6 some_col number);
Table created.
CTE FILLER1_POS
возвращает позицию (т. Е. column_id
) столбца FILLER1
;он используется в CASE
для различения столбцов, у которых ID
ниже (или выше) столбцов FILLER1
.
SQL> with filler1_pos as
2 (select column_id fid_pos
3 from all_tab_columns
4 where table_name = 'XYZ'
5 and column_name = 'FILLER1'
6 )
7 select 'insert into test (table_nm, col_nm, flg) values (' ||
8 chr(39) || a.table_name || chr(39) ||', '||
9 chr(39) || a.column_name || chr(39) ||', '||
10 chr(39) || case when a.column_id < f.fid_pos then 'Y'
11 else 'N'
12 end || chr(39) ||');' result
13 from all_tab_columns a join filler1_pos f on 1 = 1
14 where a.table_name = 'XYZ'
15 order by a.column_id;
RESULT
--------------------------------------------------------------------------------
insert into test (table_nm, col_nm, flg) values ('XYZ', 'FNAME', 'Y');
insert into test (table_nm, col_nm, flg) values ('XYZ', 'LNAME', 'Y');
insert into test (table_nm, col_nm, flg) values ('XYZ', 'PID', 'Y');
insert into test (table_nm, col_nm, flg) values ('XYZ', 'FILLER1', 'N');
insert into test (table_nm, col_nm, flg) values ('XYZ', 'FILLER2', 'N');
insert into test (table_nm, col_nm, flg) values ('XYZ', 'SOME_COL', 'N');
6 rows selected.
SQL>
Давайте запустим эти INSERTS и проверим результат:
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'FNAME', 'Y');
1 row created.
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'LNAME', 'Y');
1 row created.
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'PID', 'Y');
1 row created.
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'FILLER1', 'N');
1 row created.
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'FILLER2', 'N');
1 row created.
SQL> insert into test (table_nm, col_nm, flg) values ('XYZ', 'SOME_COL', 'N');
1 row created.
SQL> select * from test;
TABLE_NM COL_NM F
---------- ---------- -
XYZ FNAME Y
XYZ LNAME Y
XYZ PID Y
XYZ FILLER1 N
XYZ FILLER2 N
XYZ SOME_COL N
6 rows selected.
SQL>