如何在PL / SQL中使用`USING`子句和`LIKE`运算符?

时间:2017-02-02 06:56:16

标签: sql database oracle plsql

我有一个PL / SQL,我想在其中执行搜索。 PL / SQL是

val thing = List(("a", "AAA", True),("b", "BBB", True),("c", "CCC", True),("d", "DDD", False),("e", "EEE", True))
thing foreach {
  case (x,y,true) => func1(x, y)
  case (x,y,false) => func2(x, y, something_else) 
}

此PL / SQL可以执行关键字 NONE 的查找。我想在上执行DECLARE match_count INTEGER; BEGIN FOR t IN (SELECT owner, table_name, column_name FROM all_tab_columns WHERE table_name='GETS_TS_EGU_LOOKUP' and data_type LIKE '%CHAR%') LOOP EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name || ' WHERE '||t.column_name||' = :1' INTO match_count USING 'NONE'; IF match_count > 0 THEN dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count ); END IF; END LOOP; END; / 搜索,例如%NONE%

我该怎么做?

甚至可能吗?

2 个答案:

答案 0 :(得分:4)

那应该是

EXECUTE IMMEDIATE
  'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
  ' WHERE ' || t.column_name || ' like ''%'' || :1 || ''%'''
  INTO match_count
  USING 'NONE';

但是'NONE'是一个常数,为什么不简单:

EXECUTE IMMEDIATE
  'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
  ' WHERE ' || t.column_name || ' like ''%NONE%'''
  INTO match_count;

答案 1 :(得分:1)

您可以声明一个新变量来分配您要搜索的字符串,然后您可以在查询中使用它,如下所示

    DECLARE
          match_count INTEGER;
         srch_str varchar2(20);
        BEGIN
          srch_str:= 'NONE'
          FOR t IN (SELECT owner, table_name, column_name
                      FROM all_tab_columns
                      WHERE table_name='GETS_TS_EGU_LOOKUP' and data_type LIKE '%CHAR%') LOOP

            EXECUTE IMMEDIATE
              'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
              ' WHERE '||t.column_name||' like ''%'||srch_str||'%'''
              INTO match_count;

            IF match_count > 0 THEN
              dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
            END IF;

          END LOOP;

        END;

它将完美地运作。