我试图在数据库中找到chr(1) - chr(47)的所有事件,因为它们弄乱了我的数据,但由于未知的原因,我在尝试时得到错误。
代码真的很简单:
declare
v_sql varchar(300);
match_count integer;
BEGIN
FOR l_counter IN 1..37
LOOP
dbms_output.put_line(l_counter);
v_sql := 'select count(*) from CD_WELL where WELL_ID LIKE '|| '''%' || chr(l_counter) || '%''' ;
dbms_output.put_line(v_sql);
execute immediate v_sql
into match_count;
dbms_output.put_line(match_count);
END LOOP;
end;
/
当我尝试chr47以后,它工作,但较低的值给我错误:
脚本输出:1第1行错误PL/SQL过程成功完成。
DBMS输出1 0 2选择计数()从CD_WELL WELL_ID像"% %"0 3选择计数()从CD_WELL WELL_ID像"% %"0 4选择计数()从CD_WELL WELL_ID像"% %"0 5选择计数()从CD_WELL WELL_ID像"% %"0 6选择计数()从CD_WELL WELL_ID像"% %"0 7 select count ()从CD_WELL WELL_ID像"% %"0 8选择计数()从CD_WELL WELL_ID像"% %"0 9选择计数()从CD_WELL WELL_ID像"% %"0 10select count ()从CD_WELL WELL_ID像"% %"0 11 select count ()从CD_WELL WELL_ID像"% %"0 12 select count ()从CD_WELL WELL_ID像"% %"0 13选择计数()从CD_WELL WELL_ID像"% %"0 14 select count ()从CD_WELL WELL_ID像‘% % 0 15 select count ()从CD_WELL WELL_ID像‘% % 0 16 select count ()从CD_WELL WELL_ID像‘% % 0 17 select count ()从CD_WELL WELL_ID像"% %"18 0选择计数()从CD_WELL WELL_ID像"% %"0 19 select count ()从CD_WELL WELL_ID ' % % 0 20 select count ()从CD_WELL WELL_ID像"% %"0 21 select count ()从CD_WELL WELL_ID像"% %"0 22 select count ()从CD_WELL WELL_ID像"% %"0 23选择计数()从CD_WELL WELL_ID像"% %"0 24 select count ()从CD_WELL WELL_ID像"% %"0 25 select count ()从CD_WELL WELL_ID像"% %"0 26选择count() from CD_WELL where WELL_ID LIKE '%%' 27 select count() from CD_WELL where WELL_ID LIKE '%%' 28 select count() from CD_WELL where WELL_ID LIKE '%%' 29 select count() from CD_WELL where WELL_ID LIKE '%%' 30 select count() from CD_WELL where WELL_ID LIKE '%%' 32 select count() from CD_WELL where WELL_ID LIKE '%%' 33 select count() from CD_WELL where WELL_ID LIKE '%%' !select count() from CD_WELL where WELL_ID LIKE '%"%' 35 select count() from CD_WELL where WELL_ID LIKE '%#%' 36 select count() from CD_WELL where WELL_ID LIKE '%$%' 37 select count() from CD_WELL where WELL_ID LIKE '%%%' 9452
这里有人能帮我吗?由于对于LIKE
,您需要转义%
字符(和_
字符),当您试图将其作为文字而不是通配符进行匹配时。(你也不需要动态SQL)
DECLARE
v_sql VARCHAR2(300);
match_count INTEGER;
BEGIN
FOR l_counter IN 1..40
LOOP
select count(*)
into match_count
from CD_WELL
where WELL_ID LIKE '%' || CASE CHR(l_counter)
WHEN '%' THEN '%'
WHEN '_' THEN '_'
ELSE CHR(l_counter)
END || '%' ESCAPE '';
dbms_output.put_line(l_counter || ' = ' || match_count);
END LOOP;
end;
/
或使用INSTR
:
DECLARE
v_sql VARCHAR2(300);
match_count INTEGER;
BEGIN
FOR l_counter IN 1..40
LOOP
select count(*)
into match_count
from CD_WELL
where INSTR(WELL_ID, CHR(l_counter)) > 0;
dbms_output.put_line(l_counter || ' = ' || match_count);
END LOOP;
end;
/
db<此处小提琴