簡體   English   中英

如何在 Oracle SQL 中查找 chr(1) - chr(47) 的出現?

[英]How to look for occurences of chr(1) - chr(47) in Oracle SQL?

我試圖在數據庫中找到所有出現的 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 過程成功完成。

數據庫管理系統輸出

1 0 2 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 3 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 4 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 5 select count ( ) from CD_WELL where WELL_ID LIKE '%%' 0 6 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 7 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 8 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 9 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 10 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 11 select count( ) from CD_WELL where WELL_ID LIKE '% % ' 0 12 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 13 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 14 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 15 select count ( ) from CD_WELL where WELL_ID LIKE '% %' 0 16 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 17 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 18 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 19 選擇count( ) from CD_WELL where WELL_ID LIKE '%%' 0 20 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 21 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 22 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 23 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 24 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 25 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 26 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 27 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 28 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 29 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 30 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 31 select count( ) from CD_WELL where WELL_ID LIKE '%%' 0 32 select count( ) from CD_WELL where WELL_ID LIKE '% %' 0 33 select count( ) from CD_WELL where WELL_ID LIKE '%!%' 0 34 select count( ) from CD_WELL where WELL_ID LIKE '%"%' 0 35 select count( ) from CD_WELL where WELL_ID LIKE '%#%' 0 36 選擇計數( 從 CD_WELL where WELL_ID LIKE '%$%' 0 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<> 在這里擺弄

謝謝MTO! 這對我很有幫助,但是...

現在我嘗試將該搜索應用於數據庫中的每個表,所以我寫道:

DECLARE

    match_count INTEGER;
    v_owner varchar2(255) := 'EDM';
    -- A string that is part of the data type(s) of the columns to search through (case-insensitive)
    v_data_type varchar2(255) := 'CHAR';
    current_column varchar(255) :=''; 
    current_table varchar(255) :=''; 
    
BEGIN

for cur_tables in (select owner, table_name from all_tables where owner = v_owner and table_name in 
                       (select table_name from all_tab_columns where owner = all_tables.owner and data_type like '%' ||  upper(v_data_type) || '%')
                       order by table_name) loop
        
        current_table:=cur_tables.table_name;
        dbms_output.put_line('current table is: ' || current_table);

        for cur_columns in (select column_name from all_tab_columns where 
                            owner = v_owner and table_name = cur_tables.table_name and data_type like '%' || upper(v_data_type) || '%' and regexp_instr(column_name, '[ '|| CHR(10) || CHR(13) || CHR(9) || Chr(32)||']') = 0)
                            loop

        current_column :=  cur_columns.column_name;
        dbms_output.put_line('current column is: ' || current_column);
                          
                            FOR l_counter IN 1..40
                            LOOP
                            select count(*)
                            into match_count
                            from current_table
                            where  INSTR(current_column, CHR(l_counter)) > 0;
  
                            if match_count >0 then
                            dbms_output.put_line(CHR(l_counter)||' character found in '|| current_table||' column: ' || current_column || ' number of occurences = ' || match_count);
                            end if;   
                        
                            END LOOP;  

                   end loop;
    end loop;
    
end;

但它給了我更多的錯誤......你能告訴我我做錯了什么嗎?

這兩段代碼分別工作,但它們一起給了我:

第 1 行錯誤 ORA-06550:第 30 行,第 34 列:PL/SQL:ORA-00942:表或視圖不存在 ORA-06550:第 28 行,第 29 列:PL/SQL:忽略 SQL 語句

先感謝您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM