好的,我有以下函数可以在一个字符串中创建多个值(对于这个例子,它只有一个)并且它工作得很好。
然后我构建一个物化视图,其中包含字符串。
当我尝试创建它时,我得到无效的关系运算符
当我用 Dual 调用该函数时,它看起来像 GROUP_ALT_ID IN ('76410032')
它也不会在正常查询中工作,我得到同样的错误。
如何在Where子句中使用自定义字符串构建物化视图?
改用动态 SQL。
Create or replace type tab_str is table of varchar2(250);
Procedure mat_view_create(p_key_in tab_str) is
v_sql clob;
v_n number;
begin
select count(*) into v_n
from user_objects t
where t.object_name='GRP_KEY_TEST';
if v_n>0 then
--- dop mat view if it already exists
--- no such thing would be needed were it a view (not materialized), where create or replace view would do alll that is needed
execute immediate 'drop materialized view GRP_KEY_TEST';
end if;
v_sql:='select * from sample_table';
v_n:=0;
for i in p_key_in.first..p_key_in.last loop
if v_n=0 then
v_sql:=v_sql||'where group_alt_id in ('
end if;
v_sql:=v_sql||''''||p_key_in(i)||'''';
id i!=p_key_in.count then
v_sql:=v_sql||',';
else
v_sql:=v_sql||')';
end if;
end loop;
execute immediate v_sql;
end;
尚未实际测试代码。我可能错过了一些东西,但这就是我要做的。 尽管如此,请认为删除并创建物化视图会导致直接引用它的任何存储程序单元(意味着不通过动态 SQL)或引用它的任何视图的反编译。而非物化视图,使用创建或替换视图不会导致任何内容的反编译。真的一定要物化视图吗?