1.在将row_val变量char值’Good’更新到表中时,得到无效的标识符错误2.在插入数字时,它工作正常,但字符串却出现错误3。
declare count_temp number; csv_data varchar2(1000); val varchar2(100); row_val varchar2(100); sqlcmd varchar2(3000); col_name varchar2(100); col_data varchar2(100); begin csv_data:='good,son,r,,happy'; col_data:='varchar2(100)'; select regexp_count(csv_data, ',') + 1 into count_temp from dual; dbms_output.put_line(count_temp); sqlcmd := 'create table test(id number GENERATED ALWAYS as IDENTITY(START with 1 INCREMENT by 1))'; DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; for i in 1..count_temp loop col_name:='column_'||''||i; sqlcmd := 'alter table test add '||col_name||' '||col_data; DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; sqlcmd:='insert into test ('||col_name||') values('||i||')'; DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; sqlcmd:='update test set '||col_name||'='||i||' where id=1'; DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; sqlcmd:='delete from test where id > 1'; DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; end loop; for i in 1..count_temp loop col_name:='column_'||''||i; select regexp_substr(csv_data, '(.*?)(,|$)',1,i, NULL, 1) into val from dual; row_val:=val; --in this update statement getting errors sqlcmd:='update test set '||col_name||'='||to_char(row_val)||' where id=1'; -- DBMS_OUTPUT.PUT_LINE(sqlcmd); execute immediate sqlcmd; DBMS_OUTPUT.put(val||' '); end loop; dbms_output.new_line; end; /
错误 ORA-00904:“良好”:标识符无效ORA-06512:在第43行ORA-06512:在“ SYS.DBMS_SQL”在第1721行
错误
ORA-00904:“良好”:标识符无效ORA-06512:在第43行ORA-06512:在“ SYS.DBMS_SQL”在第1721行
我想将此’good,son,r ,, happy’字符串中的每个单词插入一行中的单独列中
字符串必须用单引号引起来。
所以你需要改变
values('||i||')
到
values('''||i||''')
如果有的话,还需要在代码的其他位置执行相同的操作。
请注意,字符串中的两个单引号将转换为单引号。