问题
1.getting invalid identifier error while updating row_val variable char value 'Good' into the table 2.while inserting number it's working fine but strings its getting errors 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;
/
Error
ORA-00904: "Good": invalid identifier ORA-06512: at line 43 ORA-06512: at "SYS.DBMS_SQL", line 1721
I want to insert each word in this 'good,son,r,,happy' string into separate columns in a single row
回答1:
String must be enclosed in the single quotes.
So you need to change
values('||i||')
To
values('''||i||''')
Same need to be done at other places in your code, if any.
Please note that two single quotes in string is converted to single quote.
来源:https://stackoverflow.com/questions/66059736/ora-00904-good-invalid-identifier-ora-06512