Assign variable to subquery result inside a PL/SQL block - oracle

Here is a bit of more simplified pseudocode describing what I am trying to do:
DECLARE
CURSOR CURSOR_A IS
SELECT FIELD_A1, FIELD_A2
FROM TABLE_A;
vNAME NVARCHAR2(100) := NULL;
BEGIN
FOR RECORD_A IN CURSOR_A LOOP
IF (RECORD_A.FIELD_A1 IS NOT NULL) THEN
vNAME := RECORD_A.FIELD_A1;
ELSE
vNAME := (SELECT FIELD_B
FROM TABLE_B
WHERE TABLE_B.B2 = RECORD_A.A2)
END LOOP;
END;
/
Am I not allowed to have a SELECT statement inside of a PL/SQL block?

All you need - one select. Try it:
declare
name nvarchar2(100) := null;
begin
for row_ in (
select field_a1, field_b from table_a left outer join table_b on b2 = a2
) loop
name := coalesce(row_.field_a1, row_.field_b);
-- do something
end loop;
end;
/
You could still add ... and field_a1 is null to the on-clause if you have too many rows in both tables and concerned about performance.

You are allowed to do select but you need to specify variable that will store selected data
DECLARE
CURSOR CURSOR_A IS
SELECT FIELD_A1, FIELD_A2
FROM TABLE_A;
vNAME NVARCHAR2(100) := NULL;
BEGIN
FOR RECORD_A IN CURSOR_A LOOP
IF (RECORD_A.FIELD_A1 IS NOT NULL) THEN
vNAME := RECORD_A.FIELD_A1;
ELSE
SELECT FIELD_B into vNAME
FROM TABLE_B
WHERE TABLE_B.B2 = RECORD_A.A2;
END LOOP;
END;
/

We required into clause to store a value of select statement inside Begin and end block.
Please look into below example highlighted line:
DECLARE
CURSOR CURSOR_A IS
SELECT FIELD_A1, FIELD_A2
FROM TABLE_A;
vNAME NVARCHAR2(100) := NULL;
BEGIN
FOR RECORD_A IN CURSOR_A LOOP
IF (RECORD_A.FIELD_A1 IS NOT NULL) THEN
vNAME := RECORD_A.FIELD_A1;
ELSE
SELECT FIELD_B into vNAME --**highlighted line**
FROM TABLE_B
WHERE TABLE_B.B2 = RECORD_A.A2
END LOOP;
END;

Related

How assigne value from execute immediate into variable

--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.

Select Variable in Oracle

Here is what I will do in TSQL
declare #Init_Cnt int =1,#Tot_cnt int
set #Tot_cnt = (select count(distinct column_name) from mytable)
while (#init_cnt <= #tot_cnt)
begin
//Insert statement
end
set #init_cnt = #init_cnt+1;
This is what I tried in Oracle
declare
v_Init_cnt INT;
v_Tot_cnt int;
set v_Init_cnt := 1;
begin
select count(distinct column_name) v_Tot_cnt into from mytable
end
begin
while v_Init_cnt
loop
dbms_output.put_line ('COUNT': || v_Init_cnt );
v_Init_cnt
exit when v_Init_cnt <= v_Tot_cnt ;
end loop;
end;
How will I achieve my Tsql version in Oracle? Am I doing it right? If I wanna select only my variable say
select v_Tot_cnt from dual; is not working how can I do that?
It would look something like this in Oracle.
DECLARE
v_Init_Cnt number(10) :=1;
v_Tot_cnt number(10);
BEGIN
select count(distinct column_name) INTO v_Tot_cnt from mytable;
WHILE (v_Init_Cnt <= v_Tot_cnt)
LOOP
--Insert statement
v_Init_Cnt := v_Init_Cnt+1;
END LOOP;
END;

Oracle - NO_DATA_FOUND error, but there is data

I have a PL/SQL block that I put into a stored procedure so that I can debug it. Here is a bit of pseudocode to describe the PL/SQL block:
CREATE OR REPLACE PROCEDURE SPROC_A AS
CURSOR CURSOR_A IS
SELECT ID, YEAR
FROM TABLE_A;
vNAME VARCHAR2(200) := NULL;
BEGIN
FOR REC IN CURSOR_A LOOP
BEGIN
SELECT NVL(B.FIELD_1, B.FIELD_2) INTO vNAME --the sproc breaks here--
FROM TABLE_B B
WHERE B.ID = REC.ID
AND B.YEAR = REC.YEAR
EXCEPTION
WHEN NO_DATA_FOUND THEN
vNAME := NULL;
END;
UPDATE TABLE_C C
SET C.NAME = vNAME
WHERE C.ID = REC.ID
AND C.YEAR = REC.YEAR;
END LOOP;
END;
If I run this portion independently and supply my own values for REC.ID and REC.YEAR, data is returned:
SELECT NVL(B.FIELD_1, B.FIELD_2) --INTO vNAME
FROM TABLE_B B
WHERE B.ID = REC.ID
AND B.YEAR = REC.YEAR
Furthermore, the variable into which I am trying to select data is of datatype VARCHAR2(200) - TABLE_B.FIELD_1 and TABLE_B.FIELD_2 are both of datatype VARCHAR2(40), so there shouldn't be a mismatch of datatypes.
What else could I be missing here?

Need inner and outer join in one function

I have one table that holds a record for each customer (main table). I then have a table with additional detail for some customers. The additional detail table sometimes has no records for a record in the main table. Sometimes the detail table has multiple records for a record in the main table & if this is the case I need the most recent record (hence the max subselect).
The trouble is my function only returns values for the few records in the detail table. If I comment out the portion of the function that looks at the detail table and just return the STAT3 value it seems to work. How do I make the second select statment below only apply if there is a result for that query?
create or replace FUNCTION "F_RETURN_STAT" (
N_UNIQUE IN NUMBER)
RETURN VARCHAR2
IS
V_STAT3 varchar2(20);
V_STAT varchar2(20);
V_STAT2 varchar2(20);
D_ACTDATE date;
D_STARTDATE date;
BEGIN
select expire into D_ACTDATE
from main_table a
where a.uniquefield = N_UNIQUE;
IF
D_ACTDATE > SYSDATE
or
D_ACTDATE is null
then
V_STAT :='TRUE';
else
v_STAT :='FALSE';
end if;
select b.startdate into D_STARTDATE
from main_table a, detail_table b
where a.uniquefield= b.main_table_id(+) and
b.main_table_id = N_UNIQUE and
b.uniquefield in
(select max(c.uniquefield) from detail_table c group by main_table_id);
if
D_STARTDATE is not null
then
V_STAT2 :='FALSE';
end if;
if
V_STAT2 ='FALSE'
then
V_STAT3 :='FALSE';
ELSE
V_STAT3 := V_STAT;
end if ;
RETURN(V_STAT3);
end;
I think this version of your function will solve your problem:
CREATE OR REPLACE FUNCTION f_return_stat(n_unique IN NUMBER)
RETURN VARCHAR2 IS
v_stat3 VARCHAR2(20);
v_stat VARCHAR2(20);
v_stat2 VARCHAR2(20);
d_actdate DATE;
d_startdate DATE;
BEGIN
--First Query
SELECT expire
INTO d_actdate
FROM main_table a
WHERE a.uniquefield = n_unique;
IF d_actdate > SYSDATE OR d_actdate IS NULL THEN
v_stat := 'TRUE';
ELSE
v_stat := 'FALSE';
END IF;
BEGIN
--Second Query
SELECT b.startdate
INTO d_startdate
FROM detail_table b
WHERE b.main_table_id = n_unique
AND b.uniquefield IN (SELECT MAX(c.uniquefield)
FROM detail_table c
GROUP BY main_table_id);
EXCEPTION
WHEN NO_DATA_FOUND THEN
d_startdate := NULL;
END;
IF d_startdate IS NOT NULL THEN
v_stat2 := 'FALSE';
END IF;
IF v_stat2 = 'FALSE' THEN
v_stat3 := 'FALSE';
ELSE
v_stat3 := v_stat;
END IF;
RETURN (v_stat3);
END;
In your version of the second query, your join (a.uniquefield= b.main_table_id) and your filter (b.main_table_id = N_UNIQUE) are equivalent, so main_table a can be removed altogether. The only reason to leave it in is to make sure that your query always returns a row. If you use exception handling to catch the NO_DATA_FOUND exception, that need goes away and you can simplify your query to just select from detail_table b.
I believe there could be a more efficient way however this might do the job:
SELECT b.startdate
INTO d_startdate
FROM detail_table b
WHERE b.main_table_id = n_unique
and
(b.uniquefield in
(select max(c.uniquefield) from detail_table c group by main_table_id)
or b.uniquefield is null);

Dynamically add where clauses to a cursor in oracle

I have plsql procedure which accepts certain parameters e.g. v_name, v_country, v_type.
I wish to have a cursor with a select statement like this:
select column from table1 t1, table2 t2
where t1.name = v_name
and t1.country = v_country
and t1.id = t2.id
and t2.type = v_type
If certain parameters are empty can I only add the relevant where clauses to the cursor? Or is there a better way to accomplish this?
The best way to use this is with DBMS_SQL.
You create a string that represents your SQL statement. You still use bind variables. It's painful.
It goes something like this (I haven't compiled this, but it should be close) :-
CREATE OR REPLACE FUNCTION find_country( v_name t1.country%TYPE,
v_type t2.type%TYPE) /* Hmm, column called type? */
DECLARE
v_SQL varchar2(2000);
v_select INTEGER; /* "Pointer" to a DBMS_SQL select statement */
v_execute INTEGER;
BEGIN
v_SQL := 'select column from table1 t1, table2 t2 ||
'where t1.id = t2.id';
IF v_name IS NOT NULL THEN
v_SQL := v_SQL || ' AND t1.country = :v_name'
END IF;
IF v_type IS NOT NULL THEN
v_SQL := v_SQL || ' AND t2.type = :v_type';
END IF;
/* Setup Cursor */
v_select := dbms_sql.open_cursor;
dbms_sql.parse( v_select, v_SQL, DBMS_SQL.native);
IF v_name IS NOT NULL THEN
dbms_sql.bind_variable( v_select, ':v_name', v_name );
END IF;
IF v_type IS NOT NULL THEN
dbms_sql.bind_variable( v_select, ':v_type', v_type );
END IF;
DBMS_SQL.DEFINE_COLUMN(v_select, 1, v_column); /* This is what we have selected */
/* Return value from EXECUTE is undefined for a SELECT */
v_execute := DBMS_SQL.EXECUTE( v_select );
IF DBMS_SQL.FETCH_ROWS( v_select ) > 0 THEN
/* A row was found
DBMS_SQL.COLUMN_VALUE( v_select, 1, v_column);
/* Tidy Up */
DBMS_SQL.CLOSE_CURSOR(v_select);
RETURN v_ID_address;
ELSE
DBMS_SQL.CLOSE_CURSOR(v_select);
/* No row */
RETURN NULL;
END IF;
EXCEPTION
WHEN OTHERS THEN
IF DBMS_SQL.IS_open(v_select) THEN
DBMS_SQL.CLOSE_CURSOR(v_select);
END IF;
RAISE;
END;
This approach is so painful compared to just writing the SQL inline that unless you have heaps of columns sometimes it's just easier writing a couple of different versions using this syntax:
FOR r IN (SELECT blah FROM blah WHERE t1 = v_t1) LOOP
func( r.blah );
END LOOP;
It's not directly what you're asking, but it may be an acceptable solution:
select column from table1 t1, table2 t2
where
(v_name is null or t1.name = v_name)
and (v_country is null or t1.country = v_country)
and t1.id = t2.id
and (v_type is null or t2.type = v_type)
One way would be to build up your query as a string then use execute immediate
The best way to do this would be to use Oracle's Application Context feature, best defined as best performance and security.
The faster way would be what hamishmcn suggested, using EXECUTE IMMEDIATE. I'd choose that over WW's suggestion of DBMS_SQL every time.
Another way that's quickest to write but won't perform as well would be something like this:
select column from table1 t1, table2 t2
where t1.name = nvl(v_name, t1.name)
and t1.country = nvl(v_country, t1.country)
and t1.id = t2.id
and t2.type = nvl(v_type, t2.type)
You do not have to use dbms_sql to solve this problem
and you can still use normal cursor by using a ref cursor.
Sample:
DECLARE
TYPE cursor_ref IS REF CURSOR;
c1 cursor_ref;
r1 table1.column%type;
BEGIN
l_sql := 'select t1.column from table1 t1, table2 t2 where t1.id = t2.id ';
if v_name is not null then
l_sql := l_sql||' and t1.name = '||v_name ;
end if;
if v_country is not null then
l_sql := l_sql||' and t1.country = '||v_country';
end if;
if v_type is not null then
l_sql := l_sql||' and t2.type = '||v_type';
end if;
open c1 for l_sql;
loop
fetch c1 into r1;
exit when c1%notfound;
-- do something
end loop;
close c1;
end;
/
You can make this better by binding the variables with the command 'using' like this:
open c1 for l_sql using v_name, v_country;

Resources