如何在pl / sql脚本的declare部分定义一组字符串

时间:2013-08-13 11:15:37

标签: oracle plsql

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

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

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

- 编辑: 一个非常讨厌的方法(这是我目前的方法!)是在声明部分中将项目定义为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;

1 个答案:

答案 0 :(得分: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;