Home » SQL & PL/SQL » SQL & PL/SQL » oracle procedure (oracle 10g)
oracle procedure [message #640333] |
Mon, 27 July 2015 06:56 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
Hi Guru's,
can anyone help me how to write a oracle procedure for the below requirement.
Step 1 :
SQL> select * from location_queue ;
PROCESS_CODE SCHEDULED SCHED PERFORM_WHEN
------------------------------ --------- ----- --------------------
locationCNT CHICAGO,1672o 29-SEP-14
locationCNT BOSTON,1673 29-SEP-14
locationCNT CHICAGO,1673 29-SEP-14
locationCNT NEW YORK,1673 30-SEP-14
locationRETUPD 19-DEC-14
locationZPUPD 09-JAN-15
Each record need to be passed & location and sales need to be taken as input only for %locationCNT% records.
Input : location =CHICAGO, sales=1672 need to be
Step 2:
SELECT COUNT(*) INTO value FROM areas WHERE location = &ocation AND sales = &ales;
Step 3 :
If count>0,delete the records from the table.
EXEC SQL DELETE FROM area WHERE location = &ocation AND sales = &sales;
Step 4 :
WHEN (location = 'CALIFORNIA'| location = 'BOSTON')
:VALUE:=PUTCOUNTS.LET_BOSTON
(:location,:sales);
WHEN (location ='MASS'|location = 'CHICAGO' |location = 'NEW YORK')
:VALUE:=PUTCOUNTS.LET__CHICAGO
(:location,:sales);
WHEN (location = 'MASCCEHUTES')
:VALUE:=PUTCOUNTS.LET_MASSCHEUTES
(:location,:sales);
Oracle functions are :
PUTCOUNTS.LET__CHICAGO
PUTCOUNTS.LET__CHICAGO
[Updated on: Mon, 27 July 2015 06:58] Report message to a moderator
|
|
|
|
Re: oracle procedure [message #640336 is a reply to message #640335] |
Mon, 27 July 2015 07:16 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
sorry for not providing in tags.
I am new to plsql. Any help would be greatly appreciated.
SQL> select * from location_queue ;
Location date userid
------------------------------ --------- ----- --------------------
locationCNT CHICAGO,1672o 29-SEP-14 ldfo
locationCNT BOSTON,1673 29-SEP-14 lfoi
locationCNT CHICAGO,1673 29-SEP-14 lfoi
locationCNT NEW YORK,1673 30-SEP-14 lplm
locationRETUPD 19-DEC-14 lfks
locationZPUPD 09-JAN-15 fsds
Each record need to be passed & location and sales need to be taken as input only for %locationCNT% records.
Input : location =CHICAGO, sales=1672 need to be
Step 2:
SELECT COUNT(*) INTO value FROM areas WHERE location = &location AND sales = &ales;
Step 3 :
If count>0,delete the records from the table.
EXEC SQL DELETE FROM area WHERE location = &location AND sales = &sales;
Step 4 :
if (location = 'CALIFORNIA'|| location = 'BOSTON')
:VALUE:=PUTCOUNTS.LET_BOSTON
(:location,:sales);
if (location ='MASS'|location = 'CHICAGO' || location = 'NEW YORK')
:VALUE:=PUTCOUNTS.LET__CHICAGO
(:location,:sales);
if (location = 'MASCCEHUTES')
:VALUE:=PUTCOUNTS.LET_MASSCHEUTES
(:location,:sales);
Oracle functions are :
PUTCOUNTS.LET_MASSCHEUTES
PUTCOUNTS.LET__CHICAGO
PUTCOUNTS.LET_BOSTON
[Updated on: Mon, 27 July 2015 07:23] Report message to a moderator
|
|
|
Re: oracle procedure [message #640338 is a reply to message #640336] |
Mon, 27 July 2015 07:23 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
Your requirement is a little unclear. As Michel said:
Michel Cadot wrote on Mon, 27 July 2015 13:11
Welcome to the forum.
...If you post a working Test case: create table and insert statements along with the result you want with these data then we will work with your table and data, and an example would be clearer than your short explanations (you are not limited in the number of words, so use plain English to to explain what you want from what exactly).
|
|
|
Re: oracle procedure [message #640339 is a reply to message #640338] |
Mon, 27 July 2015 07:39 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
In the location table, there will be a records starting with From this I need to pick before , as 1st argument ie Chennai and After , as 2nd argument ie sales Value. Should pass every record in the for loop and execute the below steps.
Examples :
DETAILS.LNM BANGLORE,6000 => Banglore 1st arg as location id and 6000 as 2nd arg as sales value.
DETAILS.LNM SALEM,5000 => Salem 1st arg as location id and 5000 as 2nd arg as sales value
DETAILS.LNM TIRUPUR,2000 => Tirupur 1st arg as location id and 2000 as 2nd arg as sales value.
Records in table location_all
SQL> select * from location_queue
LOCATION_CODE
------------------------------
DETAILS.LNM CHENNAI,2000
DETAILS.LNM BANGLORE,6000
DETAILS.LNM SALEM,5000
DETAILS.LNM TIRUPUR,2000
DETAILS.LNM TRICHY,4000
DETAILS.SKM
DETIALS.SKM
DETAILS.MKDM
SELECT COUNT(*) INTO value FROM areas WHERE location = CHENNAI AND sales= 2000;
If VALUE>0,delete the records from the table.
EXEC SQL DELETE FROM area WHERE location =chennai AND sales= 20000
if (location = 'CHENNAI'|| location = 'BANGLORE')
#function need to be triggerd for Chennai/banglore
VALUE=PUTCOUNTS.LET_chennai('chennai',20000);
if (location = 'SALEM'|| location = 'TRICHY')
#function need to be triggerd for salem/trichy
VALUE:=PUTCOUNTS.LET_salem(:location,:sales);
If value =0 then
print "job success"
else
print "job failed"
[/code]
Value is the variable.
Respective functions are triggeres for the different locations
[Updated on: Mon, 27 July 2015 07:42] Report message to a moderator
|
|
|
|
|
|
Re: oracle procedure [message #640344 is a reply to message #640342] |
Mon, 27 July 2015 08:10 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
Quote:SQL and PL/SQL issues or errors:
Include the CREATE table statement, provide sample data (INSERT statements) as well as the expected result. Also include the SQL code that's not working and the error you are getting. For PL/SQL errors, provide the code with the error's line number and the method that was used to call it.
[Updated on: Mon, 27 July 2015 08:11] Report message to a moderator
|
|
|
Re: oracle procedure [message #640346 is a reply to message #640339] |
Mon, 27 July 2015 08:18 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
Steps to be executed:
Step 1 :
SQL> select * from location_queue ;
PROCESS_CODE date
------------------------------ ---------
locationCNT TRICHY,16720 29-SEP-14
locationCNT BANGLORE,16730 29-SEP-14
locationCNT TRICHY,16730 29-SEP-14
locationCNT SALEM,16730 30-SEP-14
locationRETUPD 19-DEC-14
locationZPUPD 09-JAN-15
Each record need to be passed & location and sales need to be taken as input only for %locationCNT% records.
Input : location =TRICHY, sales=1672 need to be
Step 2:
select COUNT(*) into value from areas where location = &ocation and sales= &ales;
Step 3 :
If count>0,delete the records from the table.
delete from areas where location = &ocation and sales = &sales;
Step 4 :
if (location = 'CHENNAI'| location = 'BANGLORE')
VALUE:=PUTCOUNTS.LET_BANGLORE(&location,&sales);
if(location ='KOVAI'|location = 'TRICHY' |location = 'SALEM')
VALUE:=PUTCOUNTS.LET__TRICHY('&location',&sales);
WHEN (location = 'TIRUPUR')
VALUE:=PUTCOUNTS.LET_TIRUPUR(:location,:sales);
Step 5:
If value=0 , then calculate value1 & value2 by executing the below function.
value1:=PUTCOUNTS.PUTVALUE1(&LOCATION,&SALES;
value2:=PUTCOUNTS.PUTVALUE2(&LOCATION,&SALES);
step 6:
if [value=0 && value1=0 && value2=0] then
do
print "Job got sucesss"
else
print "job got failed"
Function :
PUTVALUE1(&LOCATION,&SALES;
PUTVALUE2(&LOCATION,&SALES);
LET_TIRUPUR(:location,:sales);
LET_BANGLORE(&location,&sales);
Variable :
Value
value1
Value2
Expected result:
|
|
|
|
|
|
|
Re: oracle procedure [message #640355 is a reply to message #640342] |
Mon, 27 July 2015 08:36 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
insert INTO location_queue(location, date_time,id)VALUES('locationCNT CHICAGO,1672o', '29-SEP-14', 'ldfo');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT BOSTON,1673', '29-SEP-14', 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT CHICAGO,1673', '29-SEP-14', 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT NEW YORK,1673', '29-SEP-14', 'lplm');
insert INTO location_queue(location, date_time,id)VALUES('locationRETUPD', '29-SEP-14', 'lfks');
insert INTO location_queue(location, date_time,id)VALUES('locationZPUPD', '29-SEP-14', 'fsd');
[Updated on: Mon, 27 July 2015 08:37] Report message to a moderator
|
|
|
|
Re: oracle procedure [message #640357 is a reply to message #640356] |
Mon, 27 July 2015 08:45 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
thanks for your correction.
insert INTO location_queue(location, date_time,id)VALUES('locationCNT CHENNAI,1672o', 'to_date', 'ldfo');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT BANGALORE,1673', 'to_date', 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT SALEM,1673', 'to_date', 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT TRICHY,1673', 'to_date', 'lplm');
insert INTO location_queue(location, date_time,id)VALUES('locationRETUPD', '29-SEP-14', 'to_date');
insert INTO location_queue(location, date_time,id)VALUES('locationZPUPD', '29-SEP-14', 'to_date');
[Updated on: Mon, 27 July 2015 08:51] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: oracle procedure [message #640367 is a reply to message #640366] |
Mon, 27 July 2015 09:33 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
sure I will check it before I post.
insert INTO location_queue(location, date_time,id)VALUES('locationCNT CHENNAI,1672o', CURRENT_TIMESTAMP, 'ldfo');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT BANGALORE,1673', CURRENT_TIMESTAMP, 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT SALEM,1673', CURRENT_TIMESTAMP, 'lfoi');
insert INTO location_queue(location, date_time,id)VALUES('locationCNT TRICHY,1673', CURRENT_TIMESTAMP, 'lplm');
insert INTO location_queue(location, date_time,id)VALUES('locationRETUPD', CURRENT_TIMESTAMP, 'lkml');
insert INTO location_queue(location, date_time,id)VALUES('locationZPUPD', CURRENT_TIMESTAMP, 'kfml');
|
|
|
|
|
Re: oracle procedure [message #640370 is a reply to message #640368] |
Mon, 27 July 2015 09:42 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
I have written a procedure below to my understanding. But the location id and week is in the column name location from table location_queue. can you help me how to trim the letter before and after , as field separator and pass the argument $location, $ sales.
SQL> select * from location_queue ;
PROCESS_CODE date
------------------------------ ---------
locationCNT TRICHY,16720 29-SEP-14
locationCNT BANGLORE,16730 29-SEP-14
locationCNT TRICHY,16730 29-SEP-14
locationCNT SALEM,16730 30-SEP-14
locationRETUPD 19-DEC-14
locationZPUPD 09-JAN-15
Create or replace procedure Main_Proc
AS
cursor rec
IS
SELECT * FROM location;
V_value NUMBER;
v_proc_name VARCHAR2(50);
V_value NUMBER;
V_value2 NUMBER;
V_value3 NUMBER;
BEGIN
v_proc_name := 'PROCESS';
DBMS_OUTPUT.PUT_LINE ('Insaide the'||v_proc_name);
DBMS_OUTPUT.PUT_LINE ('Now Getting the count from the table areas');
SELECT COUNT(*)
INTO V_value
FROM areas
WHERE location = rec.location
AND sales = rec.sales;
IF V_value > 0 THEN
DELETE FROM areas
WHERE location = rec.location
AND sales = rec.sales;
ELSE
DBMS_OUTPUT.PUT_LINE ('No Data found from the table areas');
END IF;
IF rec.location IN ('CHENNAI','BANGLORE') THEN
V_value := PUTCOUNTS.LET_BANGLORE(rec.location,rec.sales);
ELSIF rec.location IN ('SALEM','TRICHY','KOVAI') THEN
V_value := PUTCOUNTS.LET__TRICHY(rec.location,rec.sales);
ELSE rec.location IN ('TIRUPUR') THEN
V_value := PUTCOUNTS.LET_TIRUPUR(rec.location,rec.sales);
END IF;
IF V_value = 0 THEN
V_value2 := PUTCOUNTS.PUTVALUE1(rec.location,rec.sales);
V_value3 := PUTCOUNTS.PUTVALUE2(rec.location,rec.sales);
END IF;
BEGIN
IF V_value = 0 and V_value2 = 0 and V_value3 = 0 THEN
EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server = ''127.0.0.1''';
UTL_MAIL.send(sender => 'me@address.com',
recipients => 'you@address.com',
subject => 'Test Mail',
message => 'Job Success',
mime_type => 'text; charset=us-ascii');
ELSE
EXECUTE IMMEDIATE 'ALTER SESSION SET smtp_out_server = ''127.0.0.1''';
UTL_MAIL.send(sender => 'me@address.com',
recipients => 'you@address.com',
subject => 'Test Mail',
message => 'Job FAIL',
mime_type => 'text; charset=us-ascii');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Program errors while sending email due to:'||SQLERRM);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Program errors due to:'||SQLERRM);
END;
|
|
|
Re: oracle procedure [message #640372 is a reply to message #640370] |
Mon, 27 July 2015 09:52 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
arun888 wrote on Mon, 27 July 2015 20:12
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Program errors while sending email due to:'||SQLERRM);
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Program errors due to:'||SQLERRM);
Please read when others.
It's better not to have an exception block than adding noise to the code
[Updated on: Mon, 27 July 2015 09:54] Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: oracle procedure [message #640408 is a reply to message #640403] |
Tue, 28 July 2015 02:46 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
v_locattion_id :=substr('rec.location',12,4);
v_sales := substr('rec.location',17,4);
Your substrings will resolve to 'n' and NULL respectively for every iteration of the loop.
|
|
|
|
|
|
|
Re: oracle procedure [message #640420 is a reply to message #640355] |
Tue, 28 July 2015 06:29 |
|
EdStevens
Messages: 1376 Registered: September 2013
|
Senior Member |
|
|
arun888 wrote on Mon, 27 July 2015 08:36
insert INTO location_queue(location, date_time,id)VALUES('locationCNT CHICAGO,1672o', '29-SEP-14', 'ldfo');
Your first column, named LOCATION_QUEUE, looks like it is getting three separate data elements all jammed together -- "locationCNGT", "CHICAGO", and "1672O". This is really, REALLY bad design. You need to read up on the fundamentals of "Data Normalization" and "Third Normal Form." The concept of data normalization and designing your tables to Third Normal Form is not some esoteric subject. It is the very heart of relational databases.
|
|
|
Goto Forum:
Current Time: Fri Apr 19 12:01:41 CDT 2024
|