Dynamic where clause in ORACLE - oracle

The requirement is to fetch results from STUDENTS table.
If zero is passed in as student id,no more filtering on the studentid is required.
PROCEDURE getResults (
inStudentId IN NUMBER,
inSectionId IN NUMBER,
inRowLimit IN NUMBER,
outResultsData OUT gphResultsData
) AS
stStudentId VARCHAR2(50) := '';
BEGIN
outResultsData := gphResultsData();
IF inStudentId = '0' THEN
stStudentId := '%';
ELSE
stStudentId := TO_CHAR(inStudentId);
END IF;
FOR rResults IN (
SELECT
RESULTS.STUDENT_ID,
RESULTS.STUDENT_NAME
FROM
RESULTS
WHERE
RESULTS.STUDENT_ID LIKE stStudentId AND -- not a good idea
RESULTS.SECTION_ID = inSectionId AND
ROWNUM <= inRowLimit
) LOOP
outResultsData.extend;
outResultsData(outResultsData.last).studentId := rResults.STUDENT_ID;
outResultsData(outResultsData.last).studentName := rResults.STUDENT_NAME;
END LOOP;
EXCEPTION
WHEN others THEN
...
I have come up with the above solution - which is definitely not ideal as
inStudentId is converted to STRING using TO_CHAR and then subjected to LIKE.
I guess the better way would be to dynamically generate and execute the where clause. That is -
if inStudentId = 0 ,
SELECT
RESULTS.STUDENT_ID,
RESULTS.STUDENT_NAME
FROM
RESULTS
WHERE
RESULTS.SECTION_ID = inSectionId AND
ROWNUM <= inRowLimit
if inStudentId is not zero,
SELECT
RESULTS.STUDENT_ID,
RESULTS.STUDENT_NAME
FROM
RESULTS
WHERE
RESULTS.STUDENT_ID = inStudentId AND
RESULTS.SECTION_ID = inSectionId AND
ROWNUM <= inRowLimit
Any pointer on how to best solve this problem will be a great help.

Assuming you have the types:
CREATE TYPE gphResult IS OBJECT(
student_id INT,
student_name VARCHAR2(50)
);
/
CREATE TYPE gphResultsData IS TABLE OF gphResult;
/
Then you can avoid the loop using BULK COLLECT INTO like this:
PROCEDURE getResults (
inStudentId IN NUMBER,
inSectionId IN NUMBER,
inRowLimit IN NUMBER,
outResultsData OUT gphResultsData
) AS
stStudentId VARCHAR2(50) := '';
BEGIN
SELECT gphResult( STUDENT_ID, STUDENT_NAME )
BULK COLLECT INTO outResultsData
FROM RESULTS
WHERE ( inStudentId = 0 OR student_id = inStudentId )
AND SECTION_ID = inSectionId
AND ROWNUM <= inRowLimit;
EXCEPTION
WHEN others THEN
...

I think the easiest way to do it is the following:
PROCEDURE getResults (
inStudentId IN NUMBER,
inSectionId IN NUMBER,
inRowLimit IN NUMBER,
outResultsData OUT gphResultsData
) AS
BEGIN
outResultsData := gphResultsData();
FOR rResults IN (
SELECT
RESULTS.STUDENT_ID,
RESULTS.STUDENT_NAME
FROM
RESULTS
WHERE
(RESULTS.STUDENT_ID = inStudentId OR inStudentId = 0) AND
RESULTS.SECTION_ID = inSectionId AND
ROWNUM <= inRowLimit
) LOOP
outResultsData.extend;
outResultsData(outResultsData.last).studentId := rResults.STUDENT_ID;
outResultsData(outResultsData.last).studentName := rResults.STUDENT_NAME;
END LOOP;
EXCEPTION
WHEN others THEN
...

Related

PLSQL no data found

I have a new problem and new question :) I want to save data in table but Oracle can't do this. He give error ora-01403 no data found. In code below I can't find then is this error. Thanks for helping.
DECLARE
CURSOR c_data IS
SELECT
ROWID, td.*
FROM cols td
WHERE td.col1 IS NOT NULL AND td.col2 IS NOT NULL AND td.col3 IS NOT NULL AND td.col4 IS NOT NULL AND td.col5 IS NOT NULL AND td.col6 IS NOT NULL
AND td.col7 IS NOT NULL OR td.col8 IS NOT NULL OR td.col9 IS NOT NULL OR td.col10 IS NOT NULL;
c_id NUMBER;
r_number NUMBER;
editor NUMBER;
intendant NUMBER;
supersu NUMBER;
BEGIN
FOR i IN c_data LOOP
c_id := 0;
r_number := 0;
r_number := random_number();
c_id := fn_name(i.col5);
in next section I want to insert data in table
INSERT INTO obj.tb#link_name (
first,
ip,
cuname,
data1,
tp,
lastest,
middle,
date3,
date2,
date
) VALUES (
i.col1,
i.col8 || i.col9,
c_id,
to_date(sysdate, 'MM/DD/YYYY'),
CASE
WHEN i.col8 IS NOT NULL THEN
(
SELECT tp
FROM vtp
WHERE tp = 10
)
ELSE
(
SELECT tp
FROM vtp
WHERE tp = 20
)
END,
i.col3,
i.col2,
to_date(i.col4, 'MM/DD/YYYY'),
to_date(i.col6, 'MM/DD/YYYY'),
to_date(i.col7, 'MM/DD/YYYY')
);
COMMIT;
in next section I find some value
SELECT DISTINCT ( ed ) NTO editor
FROM dt_table_2
WHERE upper(first) LIKE upper(i.col1) AND upper(last) LIKE upper(i.col3) AND upper(middle) LIKE upper(i.col2) AND ROWNUM = 1;
SELECT DISTINCT ( numberVar ) INTO intendant
FROM dt_table_1
WHERE numberVar = :p13_apex_item AND ROWNUM = 1;
in next section I want to insert data in table
INSERT INTO obj.tb2#link_name (
nb_val,
nb_val2
) VALUES (
1,
editor
);
COMMIT;
SELECT DISTINCT ( st ) INTO supersu
FROM obj.tbd12#link_name
WHERE ed = editor AND ROWNUM = 1;
INSERT INTO obj.tdb3#link_name (
data1,
solus,
snumber,
ter,
sd,
ad,
dd,
ed,
ssd,
data2,
id
) VALUES (
to_date(sysdate, 'MM/DD/YYYY'),
intendant,
r_number,
to_date(sysdate + 5, 'MM/DD/YYYY'),
1,
1,
1,
editor,
supersu,
to_date(sysdate, 'MM/DD/YYYY'),
intendant
);
COMMIT;
END LOOP;
END;
There are 3 SELECT ... INTO statements in code you posted:
SELECT DISTINCT (ed)
INTO editor
FROM dt_table_2
WHERE UPPER (FIRST) LIKE UPPER (i.col1)
AND UPPER (LAST) LIKE UPPER (i.col3)
AND UPPER (middle) LIKE UPPER (i.col2)
AND ROWNUM = 1;
SELECT DISTINCT (numbervar)
INTO intendant
FROM dt_table_1
WHERE numbervar = :p13_apex_item
AND ROWNUM = 1;
SELECT DISTINCT (st)
INTO supersu
FROM obj.tbd12#link_name
WHERE ed = editor
AND ROWNUM = 1;
At least one of them didn't find any results and has returned the NO_DATA_FOUND error. Which one? No idea, you'll have to debug it yourself and see what you'll going to do about it:
handle the exception, or
fix code you wrote

inserting parameters from select into procedure

I've got this select :
SELECT gp.name, gda.value
FROM game_definition_aff gda,
GAME_PARAMETAR gp,
game_aff ga,
game_name gn
WHERE 4355 = ga.aff_id
AND ga.id = gda.game_aff_id
AND gp.id = gda.game_parametar_id
AND 15 = ga.game_name_id
AND gn.game_name_id = ga.game_name_id
and gp.name in ( 'MIN_BET','MAX_BET','MAX_WIN')
which returns :
NAME - VALUE
MAX_WIN - 100
MAX_BET - 50
MIN_BET - 10
And i've got procedure :
get_percentage (i_player_id, o_session_id, royal_tri_win, o_percentage,
o_min_bet,
o_max_bet,
o_max_win,
o_pot
);
Now I need to insert values from select max_win,max_bet and min_bet into procedures parameters o_max_win,o_max_bet and o_bet_bet..
How can I do that ?
PS. this is procedure where is this code from above. and where are defined all parameters that is used in it..
PROCEDURE open_session_3w (
i_player_id NUMBER,
old_session_id NUMBER,
i_ip_address VARCHAR2,
i_machine_number VARCHAR2,
o_last_bet OUT NUMBER,
o_min_bet OUT NUMBER,
o_max_bet OUT NUMBER,
o_max_win OUT NUMBER,
o_credits OUT NUMBER,
o_session_id OUT NUMBER,
o_state OUT VARCHAR2
)
IS
o_percentage NUMBER;
o_pot NUMBER;
pom weak_cur;
p_active_session NUMBER;
p_parent number;
v_max_win number;
v_min_bet number;
v_max_bet number;
BEGIN
select parent_id into p_parent from casino_users where party_id = i_player_id;
check_pl_sess_3w (i_player_id, old_session_id);
o_session_id :=
player.open_new_session (i_player_id, i_ip_address,i_machine_number,'GAME SESSION');
select MAX( CASE WHEN gp.name = 'MAX_WIN' THEN VALUE END ),
MAX( CASE WHEN gp.name = 'MAX_BET' THEN VALUE END ),
MAX( CASE WHEN gp.name = 'MIN_BET' THEN VALUE END )
INTO v_max_win,v_max_bet,v_min_bet
FROM game_definition_aff gda,
GAME_PARAMETAR gp,
game_aff ga,
game_name gn
WHERE i_player_id = ga.aff_id
AND ga.id = gda.game_aff_id
AND gp.id = gda.game_parametar_id
AND 15 = ga.game_name_id
AND gn.game_name_id = ga.game_name_id
and gp.name in ( 'MIN_BET','MAX_BET','MAX_WIN') ;
player.get_percentage (i_player_id,
o_session_id,
royal_tri_win,
o_percentage,
COALESCE(v_min_bet,o_min_bet),
COALESCE(v_max_bet,o_max_bet),
COALESCE(v_max_win,o_max_win),
o_pot
);
Use MAX(CASE WHEN) and store each value in 3 variables.
DECLARE
v_max_win NUMBER;
v_max_bet NUMBER;
v_min_bet NUMBER;
BEGIN
select MAX( CASE WHEN gp.name = 'MAX_WIN' THEN VALUE END ),
MAX( CASE WHEN gp.name = 'MAX_BET' THEN VALUE END ),
MAX( CASE WHEN gp.name = 'MIN_BET' THEN VALUE END )
INTO v_max_win,v_max_bet,v_min_bet
FROM
game_definition_aff gda,
GAME_PARAMETAR gp,
game_aff ga,
game_name gn
..
..
get_percentage (i_player_id, o_session_id, royal_tri_win, o_percentage,
COALESCE( v_min_bet,o_min_bet),
COALESCE( v_max_bet,o_min_bet),
COALESCE( v_min_win,o_max_win),
o_pot
);
END;
/
Alternatively if you want to avoid local variables, you may use implicit cursor loop.
for rec in ( --select query above )
LOOP
get_percentage (i_player_id, o_session_id, royal_tri_win, o_percentage,
COALESCE( rec.min_bet,o_min_bet),
COALESCE( rec.max_bet,o_min_bet),
COALESCE( rec.min_win,o_max_win),
pot
);
END LOOP;

Calcute percentage of every game in

I need to calculate percentage and accumulate it from every game in. I have to parameters p_percentage which is gonna be variable and parameter p_top is also variable and his task is when p_top is satisfied then do something (update or insert) . I get lost here in loop . Is it better to do loop or fetch into v_percentage. And Am I doing it right ?
Thanks
CREATE OR REPLACE FUNCTION "jackpot"
(p_percentage number
,p_top number,
p_player number,
p_party_id number
)
RETURN NUMBER
declare
Cursor c1 is
select p_percentage/game_in *100 into v_percentage from game
where player_id = p_player ;
rw c1%rowtype;
v_top number;
v_percentage number;
BEGIN
open c1;
FOR i IN c1
while v_percentage <= p_top
LOOP
v_percentage:=v_percentage+v_percentage;
end loop;
--update something
close c1;
return v_percentage;
END;
end;
EDIT
create or replace PROCEDURE super_six_jackpot (
i_party_id IN NUMBER,
i_jackpot_limit IN NUMBER,
i_jackpot_perc IN NUMBER,
o_pot_size OUT NUMBER
)
IS
p_username VARCHAR2(100);
p_party_id number;
pot_perc NUMBER;
pot_size NUMBER;
parent_aff NUMBER;
ret_pot_size NUMBER;
pom weak_cur;
BEGIN
SELECT c.party_id, p.aff_id
into p_party_id, parent_aff
FROM casino_users c,pot_by_aff p
WHERE c.parent_id = p.aff_id
AND c.parent_id = :i_party_id;
SELECT total
INTO pot_perc
FROM (
SELECT SUM( i_jackpot_perc/game_in * 100 ) OVER ( ORDER BY ROWNUM ) AS total
FROM game_record
WHERE party_id = p_party_id
ORDER BY total DESC
)
WHERE total <= i_jackpot_limit
AND ROWNUM = 1;
EXECUTE IMMEDIATE 'UPDATE pot_by_aff
SET bingo_jackpot_size = NVL (total, 0)
WHERE aff_id = :parent_aff
RETURNING pot_size
INTO :ret_pot_size'
USING parent_aff,
OUT ret_pot_size;
BEGIN
OPEN pom FOR 'SELECT bingo_jackpot_size
FROM pot_by_aff
WHERE aff_id = :parent_aff'
USING parent_aff;
FETCH pom
INTO ret_pot_size;
CLOSE pom;
END;
o_pot_size := ret_pot_size;
END super_six_jackpot;
Do you simply need to know whether the total percentage for a player reaches or exceeds the top? For that you could use the SUM function in a single SQL statement, no loop. I don't know where game_in comes from, and why you would use p_percentage inside the SELECT, but perhaps this helps:
CREATE OR REPLACE PROCEDURE give_jackpot (p_percentage NUMBER,
p_top NUMBER,
p_player NUMBER,
p_party_id NUMBER)
IS
l_total NUMBER;
BEGIN
SELECT SUM (p_percentage / game_in * 100)
INTO l_total
FROM game
WHERE player_id = p_player;
IF l_total >= p_top
THEN
/* insert or update here */
NULL;
END IF;
END;
Use an analytic function to get the cumulative total in the SQL query.
You do not use DECLARE with specifying a procedure.
Do not use double quoted identifiers if you do not have to.
You cannot use SELECT ... INTO within a cursor.
Like this:
CREATE OR REPLACE FUNCTION jackpot (
p_percentage number,
p_top number,
p_player number,
p_party_id number
) RETURN NUMBER
IS
v_percentage number;
BEGIN
SELECT total
INTO v_percentage
FROM (
SELECT SUM( p_percentage/game_in *100 ) OVER ( ORDER BY ROWNUM ) AS total
FROM game
WHERE player_id = p_player
ORDER BY total DESC
)
WHERE total <= p_top
AND ROWNUM = 1;
--update something
RETURN v_percentage;
END;
/

Updating column is not working in Oracle PLSQL

I have a Stored procedure in PLSQL which Inserts and Updates records on the basis of some condition.
Now here the issue is. While Inserting the record for the first time, it inserts records properly as required but
while updating it doesn't updates the record of the table.
Below is SP
PROCEDURE INSERT_INTO_VSAT_MST_DATA
(
P_SAPID IN NVARCHAR2,
P_CIRCLE IN NVARCHAR2,
P_CANDIDATEID IN NVARCHAR2,
P_SITEID IN NVARCHAR2,
P_PRIORITYID IN NVARCHAR2,
P_SITENAME IN NVARCHAR2,
P_LATITUDE IN NVARCHAR2,
P_LONGITUDE IN NVARCHAR2,
P_CONTACT_DETAILS IN CLOB,
P_SITETYPE IN NVARCHAR2,
P_SITE_PLOT_DIMENSION IN NUMBER,
P_TECHNOLOGY IN NVARCHAR2
)
AS
V_COUNT NUMBER:=0;
V_PANAROMICIMG_COUNT NUMBER:=0;
V_SATELLITEIMG_COUNT NUMBER:=0;
V_SITEPLOTIMG_COUNT NUMBER:=0;
V_VSAT_DETAIL_ID NUMBER:=0;
BEGIN
SELECT COUNT(VSAT_DETAIL_ID) INTO V_COUNT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
IF V_COUNT > 0 THEN
SELECT VSAT_DETAIL_ID INTO TBL_INSERT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
ELSE
INSERT INTO TBL_VSAT_MST_DETAIL
(
SAP_ID,
CIRCLE,
CANDIDATE_ID,
SITE_ID,
PRIORITY,
SITE_NAME,
LATITUDE,
LONGITUDE,
CONTACT_DETAILS,
SITE_TYPE,
SITE_DETAILS_DIMENSION,
SITE_DETAILS_TECHNOLOGY
VALUES
(
P_SAPID,
P_CIRCLE,
P_CANDIDATEID,
P_SITEID,
P_PRIORITYID,
P_SITENAME,
P_LATITUDE,
P_LONGITUDE,
P_CONTACT_DETAILS,
P_SITETYPE,
P_SITE_PLOT_DIMENSION,
P_TECHNOLOGY
) RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
END IF;
IF TBL_INSERT > 0 THEN
BEGIN
SELECT COUNT(*) INTO V_PANAROMICIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Panaromic' AND IMG_ID = TBL_INSERT;
SELECT COUNT(*) INTO V_SATELLITEIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Satellite' AND IMG_ID = TBL_INSERT;
SELECT COUNT(*) INTO V_SITEPLOTIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'SitePlot' AND IMG_ID = TBL_INSERT;
IF V_PANAROMICIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Panaromic' AND IMG_ID = TBL_INSERT;
END;
END IF;
IF V_SATELLITEIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Satellite' AND IMG_ID = TBL_INSERT;
END;
END IF;
IF V_SITEPLOTIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'SitePlot' AND IMG_ID = TBL_INSERT;
END;
END IF;
FOR PMULTIFIELDS IN (SELECT REGEXP_SUBSTR(P_PANORAMIC_IMAGES,'[^,]+', 1, LEVEL) AS IMAGES FROM DUAL
CONNECT BY REGEXP_SUBSTR(P_PANORAMIC_IMAGES, '[^,]+', 1, LEVEL) IS NOT NULL
)
LOOP
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
PMULTIFIELDS.IMAGES,
'Panaromic',
SYSDATE,
P_CREATEDBY
);
END LOOP;
FOR PSATELLITEIMG IN (SELECT REGEXP_SUBSTR(P_SATELLITE_IMAGES,'[^,]+', 1, LEVEL) AS IMAGES FROM DUAL
CONNECT BY REGEXP_SUBSTR(P_SATELLITE_IMAGES, '[^,]+', 1, LEVEL) IS NOT NULL
)
LOOP
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
PSATELLITEIMG.IMAGES,
'Satellite',
SYSDATE,
P_CREATEDBY
);
END LOOP;
IF P_SITEPLOT_IMAGES IS NOT NULL THEN
BEGIN
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
P_SITEPLOT_IMAGES,
'SitePlot',
SYSDATE,
P_CREATEDBY
);
END;
END IF;
END;
END IF;
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
NOTE
While updating the record my TBL_INSERT returns as NULL
Expanding on what #user7294900 pointed you towards... in the declare section you have:
V_VSAT_DETAIL_ID NUMBER:=0;
then if v_count > 0 you do:
SELECT VSAT_DETAIL_ID INTO TBL_INSERT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
The select is setting TBL_INSERT to the ID value from your table. But when you do the update your filter is using V_VSAT_DETAIL_ID, which is still set to its initial value of zero.
You probably meant to do:
SELECT VSAT_DETAIL_ID INTO V_VSAT_DETAIL_ID FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
although you could still with your current select, and use that in the update too (making the returning into a bit redundant.
Be aware though that if v_count is not exactly 1, i.e. you have more than one row matching the P_SAPID and P_CANDIDATEID values, the select will get a too-many-rows exception. You won't see that because you are silently squashing any errors you get at run time.
It's usually not a good idea to commit or rollback inside a procedure anyway; it should be up to the caller to decide what to do, as this could be one of a series of statements and calls that you really want to treat as an atomic transaction. (You may be interested in savepoints.)
If you really, really want to rollback on exception within the procedure, you should at least re-raise the exception so the caller knows there was a problem:
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END INSERT_INTO_VSAT_MST_DATA;
but I would avoid when others if you can.
You could also combine a few steps by getting the ID at the start (again kind of assuming there is at most one matching row), and dropping the separate count and v_count variable:
SELECT MAX(VSAT_DETAIL_ID) INTO V_VSAT_DETAIL_ID
FROM TBL_VSAT_MST_DETAIL
WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
IF V_VSAT_DETAIL_ID IS NOT NULL THEN
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
ELSE
...
And I'm not sure why you're doing counts before your deletes later on, and it looks like all your tbl_insert references could/should be v_vast_detail_id - there doesn't seem an obvious reason to have two variables for that. Passing in a comma-delimited string that you then have to tokenize is also a bit painful - you should consider passing in a collection of values instead, if whatever calls this can manage that.
As also pointed out, you could use merge instead of the separate insert/update logic.
You don't assign value to V_VSAT_DETAIL_ID which is used in your update as a key.
You should use merge for this kind of operations

How to optimize my stored procedure

I would like to know if there are ways I could optimize n improve performance of this stored procedure. I'm fairly new at it & would appreciate your help
To summary , the stored procedure takes an array . Each array an entry of the form a:b:c . After extracting the required data, an entry is made in a table.
create or replace
PROCEDURE insert_sku_prom_assigments (
sku_assigment_array tp_string_array_table
)
IS
first_colon_pos NUMBER;
second_colon_pos NUMBER;
sku VARCHAR2 (40);
assign VARCHAR2 (20);
promotion VARCHAR2 (40);
l_count INTEGER;
BEGIN
FOR i IN sku_assigment_array.FIRST .. sku_assigment_array.LAST
LOOP
first_colon_pos := INSTR (sku_assigment_array (i), ':', 1, 1);
second_colon_pos := INSTR (sku_assigment_array (i), ':', 1, 2);
sku := SUBSTR (sku_assigment_array (i), 1, first_colon_pos - 1);
assign :=
SUBSTR (sku_assigment_array (i),
first_colon_pos + 1,
second_colon_pos - first_colon_pos - 1
);
promotion := SUBSTR (sku_assigment_array (i), second_colon_pos + 1);
IF sku IS NOT NULL AND assign IS NOT NULL AND promotion IS NOT NULL
THEN
SELECT COUNT (*)
INTO l_count
FROM mtep_sku_promotion_rel_unver
WHERE sku_id = sku
AND assignment = assign
AND promotion_id = promotion
AND ROWNUM = 1;
IF l_count < 1
THEN
INSERT INTO mtep_sku_promotion_rel_unver
(sku_id, assignment, promotion_id
)
VALUES (sku, assign, promotion
);
END IF;
END IF;
l_count := 0;
END LOOP;
END insert_sku_prom_assigments;
#jorge is right there probably isn't much performance improvement to be found. But you could consider these alternatives to selecting a count before inserting:
This:
INSERT INTO mtep_sku_promotion_rel_unver
(sku_id, assignment, promotion_id
)
SELECT sku, assign, promotion FROM DUAL
WHERE NOT EXISTS
( SELECT NULL
FROM mtep_sku_promotion_rel_unver
WHERE sku_id = sku
AND assignment = assign
AND promotion_id = promotion
);
Or, assuming those 3 columns are defined as a key:
BEGIN
INSERT INTO mtep_sku_promotion_rel_unver
(sku_id, assignment, promotion_id
)
VALUES (sku, assign, promotion
);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN NULL;
END;
This is not performance-related, but I also like to use the Oracle function apex_util.string_to_table to break up delimited strings easily:
PROCEDURE ...
IS
v_tab apex_application_global.vc_arr2;
...
BEGIN
v_tab := apex_util.string_to_table (sku_assigment_array (i), ':');
if v_tab.count >= 3 then
sku := v_tab(1);
assign := v_tab(2);
promotion := v_tab(3);
end if;
Replace the row-by-row PL/SQL with a single SQL statement:
create or replace procedure insert_sku_prom_assigments
(
sku_assigment_array tp_string_array_table
) is
begin
--#4: Insert new SKU, ASSIGN, and PROMOTION.
insert into mtep_sku_promotion_rel_unver(sku_id, assignment, promotion_id)
select sku, assign, promotion
from
(
--#3: Get values.
select sku_string,
substr (sku_string, 1, first_colon_pos - 1) sku,
substr (sku_string, first_colon_pos + 1, second_colon_pos-first_colon_pos - 1) assign,
substr (sku_string, second_colon_pos + 1) promotion
from
(
--#2: Get positions.
select
sku_string,
instr(sku_string, ':', 1, 1) first_colon_pos,
instr(sku_string, ':', 1, 2) second_colon_pos
from
(
--#1: Convert collection to table.
select column_value sku_string
--Use this for debugging:
--from table(tp_string_array_table('asdf:qwer:1234'))
from table(sku_assigment_array)
) converted_collection
) positions
) sku_values
--Only non-null values:
where sku is not null and assign is not null and promotion is not null
--Row does not already exist:
and not exists
(
select 1/0
from mtep_sku_promotion_rel_unver
where sku_id = sku
and assignment = assign
and promotion_id = promotion
);
end insert_sku_prom_assigments;
/
Here are sample objects for testing:
create or replace type tp_string_array_table is table of varchar2(4000);
create table mtep_sku_promotion_rel_unver(
sku_id varchar2(100),
assignment varchar2(100),
promotion_id varchar2(100));
begin
insert_sku_prom_assigments(tp_string_array_table('asdf:qwer:1234'));
end;
/
SQL almost always outperforms PL/SQL. It's also easier to test and debug once you get used to the way the code flows.

Resources