[英]How can i use column_name dynamically in pl/sql
---- Hey guys, kinldy help me in this code, i want to give column_name dynamically in dbms_output, but its giving me error ------ ---- 嘿伙计们,kinldy 在这段代码中帮助我,我想在 dbms_output 中动态地给出 column_name,但是它给我错误 ------
--set serveroutput on;
declare
type t_list_rec is record
(table_name all_tab_columns.table_name%type,
column_name all_tab_columns.COLUMN_NAME%type);
type t_list is table of t_list_rec index by PLS_INTEGER;
v_array t_list;
type e_list is table of CUSTOMER_ADDRESS%rowtype;
t_array e_list:=e_list();
begin
select /*+ parallel(14) */ table_name,column_name bulk collect into v_array from all_tab_columns where table_name='CUSTOMER_ADDRESS' and OWNER='MIG';
for k in 1..v_array.count() loop
t_array.extend;
EXECUTE IMMEDIATE
'select /*+ parallel (16) */ * from '||v_array(k).table_name||' where not regexp_like ('||v_array(k).column_name||',''[A-za-z0-9.]'')'
into t_array(k);
dbms_output.put_line(t_array(k).v_array(k).column_name);
end loop;
end;
---Error----
Error report -
ORA-06550: line 17, column 37:
PLS-00302: component 'V_ARRAY' must be declared
ORA-06550: line 17, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Not quite sure what you want, but you can get something like it using SYS_REFCURSOR.不太确定你想要什么,但你可以使用 SYS_REFCURSOR 得到类似的东西。
Example Procedure:示例程序:
create or replace Procedure Desc_Tab_Content(p_tab IN VarChar2, p_owner IN VarChar2) IS
BEGIN
DECLARE
type t_list_rec is record (table_name all_tab_columns.table_name%type,
column_name all_tab_columns.COLUMN_NAME%type);
type t_list is table of t_list_rec index by PLS_INTEGER;
v_array t_list;
--
mReport SYS_REFCURSOR;
mSql VarChar2(512);
mCount Number(6);
mCountDist Number(6);
mHead VarChar2(64);
mHeadLen Number(3);
mDataLen Number(3);
BEGIN
Select table_name, column_name
bulk collect into v_array
From all_tab_columns
Where table_name = p_tab and owner = p_owner;
--
mHead := 'Table ' || v_array(1).table_name || ' - Colummn''s (DistVals/TotalVals)';
mHeadLen := Length(mHead);
dbms_output.put_line(mHead);
dbms_output.put_line(RPAD('-', mHeadLen, '-'));
--
For k in 1..v_array.count() Loop
mSql := 'Select Count(DISTINCT ' || v_array(k).column_name || '), Count(*) From ' || v_array(k).table_name;
OPEN mReport FOR mSql;
FETCH mReport Into mCount, mCountDist;
mDataLen := Length(k || '. ' || v_array(k).column_name);
dbms_output.put_line(k || '. ' || v_array(k).column_name || LPAD(' (' || mCount || '/' || mCountDist || ')', mHeadLen - mDataLen, ' '));
End Loop;
END;
END Desc_Tab_Content;
In the procedure you define what you want to do with your data.在该过程中,您可以定义要对数据执行的操作。 Here is the list of columns with number of distinct values (per column) out of total records in the given table.这是给定表中总记录中不同值(每列)数量的列列表。
... call & result: ...电话和结果:
set serveroutput on
BEGIN
Desc_Tab_Content(your_tbl_name, your_owner_name);
END;
/* R e s u l t:
anonymous block completed
Table ATBL - Colummn's (DistVals/TotalVals)
-------------------------------------------
1. ADATE (1/3)
2. ANAME (3/3)
3. ID (3/3)
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.