How assigne value from execute immediate into variable - oracle

--create table locations_localtab as select * from HR.locations;
SET SERVEROUTPUT ON;
declare
type tLOC_type is table of locations_localtab%rowtype index by binary_integer;
tLOC tLOC_type := tLOC_type();
vPostal_code locations_localtab.postal_code%type;
vCity locations_localtab.city%type;
vLocal varchar2(50);
vSource varchar2(50);
comm long;
begin
select location_id,null,postal_code,city,state_province,country_id
bulk collect into tLOC
from HR.locations;
for ii in (select column_name from (select 'POSTAL_CODE' c1,'CITY' c2, 'COUNTRY_ID' c3 from dual) UNPIVOT (column_name for (name_of_col) in (c1,c2,c3))) loop
for i in 1..tLOC.count loop
comm := 'vSource := tLOC('||i|| ').'||ii.column_name ;
dbms_output.put_line(comm);
execute immediate comm;
-- select city into vLocal from locations_localtab where location_id = tLOC(i).location_id;
dbms_output.put_line('vSOURCE -->'||'.'||vSOURCE);
dbms_output.put_line('vLOCAL -->'||'.'||vLOCAL);
end loop;
end loop;
-- output -->
-- vSource := tLOC(1).POSTAL_CODE;
-- vSource := tLOC(2).POSTAL_CODE;
I would like to make loop by column_name to use this iterator for another loop with table type..
I would like to assign value of tLOC(i).ii.column_name into variable vSource, how can I do this?
I have no idea how I can deal with it.
Thank you in advance for your help.

Related

How to insert multiple row result of dynamic sql to another Table?

I write one dynamic SQL which the result of it is a table with 2 columns and multiple rows, I want to insert it to another table with 4 columns that 2 of them will be filled by the result of dynamic SQL, I try to use collection but don't know how to insert result to another table
CREATE OR REPLACE PROCEDURE P_C_SM_Failure_error_Code_P2P AS
v_month VARCHAR2(16); -- to get Month for each table
v_day VARCHAR2(16); -- to get day for each table
v_ERRCODE t_c_rpt_resultmsg.code%TYPE;
v_ERRMSG t_c_rpt_resultmsg.MESSAGE%TYPE;
v_param VARCHAR2(16);
v_sql VARCHAR2(3000);
v_result number;
type t_c_result is record (Err_code varchar2(2000), Err_count number);
type v_t_result is table of t_c_result index by PLS_INTEGER;
v_t1_result v_t_result;
BEGIN
v_sql :='0';
v_param := 'Gateway_G';
v_result := '0';
select to_char(sysdate - 1,'MM') into v_month from dual;
select to_char(sysdate - 1,'DD') into v_day from dual;
-- Get count of P2P
v_sql := '(select count(*), error_code from (
select error_code from sm_histable'||v_month||''||v_day||'#ORASMSC01 where
orgaccount = '''||v_param||''' and destaccount = '''||v_param||''' and
sm_status <> 1 union all
select error_code from sm_histable'||v_month||''||v_day||'#ORASMSC02 where
orgaccount = '''||v_param||''' and destaccount = '''||v_param||''' and
sm_status <> 1 )
group by error_code)';
EXECUTE IMMEDIATE v_sql bulk collect into v_t1_result;
--insert into t_c_rpt_result2 values (trunc(sysdate, 'DD'), v_errcount,
v_err_code,'Failure_error_Code_P2P');
--for indx in 1 .. v_t1_result.COUNT
--loop
--dbms_output.put_line (v_t1_result (indx).Err_code);
--end loop;
You may append the constant values of date and the error message to the subquery and run a dynamic insert. It should also work if you remove the outer parentheses of your dynamic sql since constants can be included in group by. Always remember to pass values as bind variables rather than concatenating them (v_param). Also, specify the column names explicitly in an INSERT statement.
v_sql := '(select count(*) as cnt, error_code
from (
select error_code from sm_histable'||v_month||''||v_day||'#ORASMSC01
where orgaccount = :x and destaccount = :x and sm_status <> 1
union all
select error_code from sm_histable'||v_month||''||v_day||'#ORASMSC02
where orgaccount = :x and destaccount = :x and sm_status <> 1 )
group by error_code)';
EXECUTE IMMEDIATE v_sql bulk collect into v_t1_result using v_param;
EXECUTE IMMEDIATE 'insert into t_c_rpt_result2(err_dt,err_msg,errcount,error_code)
select :dt,:msg,cnt,error_code from '|| v_sql
USING trunc(sysdate, 'DD'),'Failure_error_Code_P2P',v_param;
I think you are looking at an excellent use case for FORALL. The collection you are populating needs to be done with execute immediate since you are dynamically constructing the table name. But the insert into t_c_rpt_result2 looks static to me.
BEGIN
v_sql :=
'(select count(*) as cnt, error_code
from (
select error_code from sm_histable'
|| v_month
|| ''
|| v_day
|| '#ORASMSC01
where orgaccount = :x and destaccount = :x and sm_status <> 1
union all
select error_code from sm_histable'
|| v_month
|| ''
|| v_day
|| '#ORASMSC02
where orgaccount = :x and destaccount = :x and sm_status <> 1 )
group by error_code)';
EXECUTE IMMEDIATE v_sql BULK COLLECT INTO v_t1_result USING v_param;
FORALL indx IN 1 .. v_t1_result.COUNT
INSERT INTO t_c_rpt_result2 (err_dt,
err_msg,
errcount,
ERROR_CODE)
VALUES (TRUNC (SYSDATE, 'DD'),
'Failure_error_Code_P2P',
v_t1_result (indx).cnt,
v_t1_result (indx).ERROR_CODE);
END;
Find more examples of FORALL on LiveSQL here. Of course, even if your insert was dynamic, you can use FORALL - put the execute immediate directly "inside" the FORALL statement. But I don't think that complexity is justified here.
Hope that helps!

query to find all columns in a table with no value in it

I have written this query for finding the total number of column which does not have a value in a table but its not working.
SET serveroutput ON;
DECLARE
v_count NUMBER;
v_table_name VARCHAR2(200);
CURSOR c2
IS
SELECT Column_name FROM all_tab_columns WHERE table_name= A;
BEGIN
FOR r1 IN c2
LOOP
dbms_output.put_line(r1.column_name);
EXECUTE immediate('SELECT COUNT(r1.column_name) INTO v_count FROM HR'||'.'||A);
IF v_count =0 THEN
dbms_output.put_line(v_count);
END IF;
END LOOP;
END;
I would like you to try this code. It works for me :)
SET serveroutput ON;
DECLARE
myOwner VARCHAR2(20):='HR';
myTable VARCHAR2(25):='A';
CURSOR c2
IS
SELECT column_name,
avg_col_len
FROM all_tab_columns
WHERE table_name = myTable
AND owner = myOwner;
BEGIN
FOR r1 IN c2
LOOP
IF r1.AVG_COL_LEN =0 THEN
dbms_output.put_line('column_name = '||r1.column_name);
END IF;
END LOOP;
END;
Hope this can help you.

How to store a column of result of select query in an array?

If we have a column in a table of type number, how can we store the result of select query on that column in an array ?
This sample uses a list (table of numbers) to achieve this, because i find
those lists much more handy:
CREATE OR REPLACE TYPE numberlist AS TABLE OF NUMBER;
DECLARE
v_numberlist numberlist;
BEGIN
SELECT intval numbercolumn
BULK COLLECT INTO v_numberlist
FROM lookup;
FOR i IN 1..v_numberlist.count
LOOP
dbms_output.put_line( v_numberlist(i) );
END LOOP;
END;
Create a type which store number:-
CREATE OR REPLACE TYPE varray is table of number;
--write your select query inside for loop () where i am extracting through level
declare
p varray := varray();
BEGIN
for i in (select level from dual connect by level <= 10) loop
p.extend;
p(p.count) := i.level;
end loop;
for xarr in (select column_value from table(cast(p as varray))) loop
dbms_output.put_line(xarr.column_value);
end loop;
END;
output:-
1
2
3
4
5
6
7
8
9
10
Just an option to use some native SQL datatype. Hope it helps.
SET SERVEROUTPUT ON;
DECLARE
lv_num_tab DBMS_SQL.NUMBER_TABLE;
BEGIN
SELECT LEVEL BULK COLLECT INTO lv_num_tab FROM DUAL CONNECT BY LEVEL < 10;
FOR I IN lv_num_tab.FIRST..lv_num_tab.LAST
LOOP
dbms_output.put_line(lv_num_tab(i));
END LOOP;
END;
You may also want to put the whole select in a table. You can use a BULK COLLECT to an array:
CREATE OR REPLACE TYPE t_my_list AS TABLE OF VARCHAR2(100);
CREATE OR REPLACE
PROCEDURE get_tables(p_owner in varchar2)
as
v_res t_my_list;
v_qry varchar2(4000) := '';
begin
v_qry := ' SELECT table_name from all_tables where owner='''||p_owner||'''';
dbms_output.put_line(v_qry);
-- all at once in the table
execute immediate v_qry bulk collect into v_res;
FOR I in 1..v_res.count
loop
dbms_output.put_line(v_res(i));
end loop;
exception
when others then
raise;
end get_tables;
/
begin
get_tables('E') ;
end;
/

compare xtable count with table in oracle procedure

(I have several tables and for each table I have corresponding xtable.
what I am trying to do is create oracle procedure that will compare counts between xtable and table,
if (xtable >= table)
do something...
type namesarray IS VARRAY(5) OF VARCHAR2(10);
tbl_names namesarray;
xtbl_names namesarray;
total integer;
BEGIN
tbl_names := namesarray('tbl1', 'tbl2', 'tbl3', 'tbl4');
xtbl_names := namesarray('xtbl1', 'xtbl2', 'xtbl3', 'xtbl4');
total := tbl_names.count;
FOR i in (
SELECT COUNT(*) as TBL_COUNTS FROM tbl_names(i);
SELECT COUNT(*) as XTBL_COUNTS FROM xtbl_names(i)
)
LOOP
IF(i.XTBL_COUNTS >= i.TBL_COUNTS )
THEN
-- print to console
END IF;
END LOOP;
END;
This is all I got so far.
Can someone help?
How about this?
if (xtable >= table)
do something...
type namesarray IS VARRAY(5) OF VARCHAR2(10);
tbl_names namesarray;
xtbl_names namesarray;
total integer;
v_count_1 integer;
v_count_2 integer;
BEGIN
tbl_names := namesarray('tbl1', 'tbl2', 'tbl3', 'tbl4');
xtbl_names := namesarray('xtbl1', 'xtbl2', 'xtbl3', 'xtbl4');
total := tbl_names.count;
FOR i in (
select
t1.table_name as tab_1_name, t2.table_name as tab_2_name
from ( select table_name from user_tables where table_name in (...) ) t1
,( select 'x'||table_name as table_name from user_tables where table_name in (...) ) t2
where 'x'||t1.table_name = t2.table_name
)
LOOP
execute immediate 'select count(*) from '||i.tab_1_name into v_count_1;
execute immediate 'select count(*) from '||i.tab_2_name into v_count_2;
IF( v_count_1 >= v_count_2 )
THEN
-- print to console
END IF;
END LOOP;
END;

Oracle - How do I get the actual size of a specific ROW?

Is this possible? Or at least I'm looking to have a list of the size of all rows in a table.
select vsize(col1) + vsize(col2) + vsize(col3) +
long_raw_length_function(long_col) + DBMS_LOB.GETLENGTH(blob_col)
from table
where id_col = id_val;
for the long_raw_length_function, see this Get the LENGTH of a LONG RAW
if you're interested in the average row length, you could analyze the table (with the DBMS_STATS package), then query ALL_TABLES.avg_row_len.
Here below is the query I have modified to get the table row length when you don't have any data. This can help you with Capacity planning for Environment setup:
SET serveroutput ON linesize 300
DECLARE
v_max_size NUMBER := 0;
v_owner VARCHAR2(30);
v_table_name VARCHAR2(30);
v_data_type VARCHAR2(30);
v_data_length NUMBER := 0;
v_data_precision NUMBER := 0;
CURSOR CUR_TABLE
IS
SELECT DISTINCT table_name
FROM all_tab_columns
WHERE owner='TMS_OWNER'
AND table_name NOT LIKE 'VIEW%'
ORDER BY table_name;
BEGIN
FOR Tab IN CUR_TABLE
LOOP
v_table_name := Tab.table_name;
v_max_size := 0;
FOR i IN
(SELECT owner,
table_name,
data_type,
data_length,
data_precision
FROM all_tab_columns
WHERE owner ='TMS_OWNER'
AND table_name = v_table_name
)
LOOP
IF i.data_type = 'NUMBER' THEN
v_max_size := (v_max_size + i.data_precision);
ELSE
v_max_size := (v_max_size + i.data_length);
END IF;
END LOOP;
dbms_output.put_line(chr(10));
dbms_output.put_line('Table ='||v_table_name||', Max Record Size = '||v_max_size||' bytes');
END LOOP;
END;
/

Resources