0

在 pl/sql 中,我可以将in关键字与一组字符串一起使用:

select * from languages where language_tag in ('en','fr','es')

我如何定义脚本部分的集合并再次使用它('en','fr','es')DECLARE

--edit:一个非常讨厌的方法(这是我目前的方法!)是在声明部分将项目定义为 csv 字符串并使用execute_immediate

DECLARE
  v_csv_tags VARCHAR2(123) :='''en'',''es''';

BEGIN
execute immediate 'delete from config_supports_language where language_code not in ('||v_csv_tags||')';
execute immediate 'delete from languages where language_code not in ('||v_csv_tags||')';

END;
/
EXIT;
4

1 回答 1

1

您可以创建嵌套表或 varray SQL 类型(作为模式对象),然后在 PL/SQL 存储过程或匿名 PL/SQL 块中使用它,如下所示:

  1. SQL 类型

    create type T_List as table of varchar2(123);
    /
    Type created
    
  2. PL/SQ 块:

    declare
       l_list T_List3 := T_List3('en','fr','es'); -- the l_list also can be initialized
    begin                                         -- in the BEGIN..END section
       select <<columns list>> 
         into <<variables>>
         from languages 
        where language_tag in (select column_values  -- you can query table(l_list)
                                 from table(l_list)) -- as many times as you like
    exception
      when no_data_found
      then dbms_output.put_line('No data is found');
    end;
    
于 2013-08-13T11:36:23.650 回答