我正在尝试下面的动态查询,我从一个查询中获取列名、表名,我需要将这些值放在一个变量中,我将把它作为一个全局变量。
列名可以在 5 到 50 列之间变化。我写了下面的查询,但它不起作用。给出以下消息匿名块已完成消息:-905-ORA-00905:缺少关键字
问题在于 parse 语句,DBMS_SQL.PARSE 不确定该由谁继续
DECLARE
TYPE lc_pdh_cur IS REF CURSOR;
ont_pdh lc_pdh_cur;
lv_sql_stmt VARCHAR2(3000);
lv_value VARCHAR2(1000);
lv_view_name VARCHAR2(1000);
lv_columns VARCHAR2(1000);
lv_var_columns VARCHAR2(1000);
LV_VAR_STMT VARCHAR2(1000);
LV_COL_STMT VARCHAR2(1000);
v_cursor integer;
v_dname char(20);
v_rows integer;
TYPE l_column_pair IS TABLE OF VARCHAR2(2400) -- Associative array type
INDEX BY VARCHAR2(200); -- indexed by string
l_pairs l_column_pair; -- Associative array variable
CURSOR lc_agv_name
is
SELECT database_column,decode (value_set_name,null,substr(attr_name,0,24),substr(attr_name,0,24)||'_disp') attr_name FROM ego_attrs_v a WHERE attr_group_name = 'Claims';
BEGIN
SELECT agv_name INTO lv_view_name FROM ego_attr_groups_v WHERE attr_group_name = 'Claims';
BEGIN
FOR lc_agv_name_rec in lc_agv_name
LOOP
lv_col_stmt := lv_col_stmt || lc_agv_name_rec.attr_name ||',';
lv_var_stmt := lv_var_stmt || 'l_pairs('''|| lc_agv_name_rec.database_column ||''') ,';
END LOOP;
lv_columns := SUBSTR(lv_col_stmt,0,length(lv_col_stmt)-1);
lv_var_columns := SUBSTR(lv_var_stmt,0,length(lv_var_stmt)-1);
EXCEPTION
when others
then
dbms_output.put_LINE('mESSAGE :'||lv_columns||'--'||lv_var_columns||'-'||SQLCODE||'-'||SQLERRM);
END;
BEGIN
v_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(v_cursor, 'SELECT '||lv_columns||' INTO '||lv_var_columns|| ' FROM '||lv_view_name||' WHERE header_id = 8175', DBMS_SQL.NATIVE);
v_rows :=DBMS_SQL.EXECUTE(v_cursor);
loop
if DBMS_SQL.FETCH_ROWS(v_cursor) = 0 then
dbms_output.put_LINE('No Data :View '||lv_view_name||' Columns '||lv_columns);
exit;
end if;
end loop;
DBMS_SQL.CLOSE_CURSOR(v_cursor);
dbms_output.put_LINE('Values :'||l_pairs('Coulmn1') ||'some variable'||l_pairs('Coulmn2') );
EXCEPTION
when others
then
dbms_output.put_LINE('Message :'||lv_sql_stmt||SQLCODE||'-'||SQLERRM);
END;
END;
/