Home » SQL & PL/SQL » SQL & PL/SQL » Help me on Syntax of PL/SQL (PL/SQL using UNIX scripts)
Help me on Syntax of PL/SQL [message #646314] Thu, 24 December 2015 09:21 Go to next message
royal4111
Messages: 2
Registered: December 2015
Location: Bangalore
Junior Member
Hi,

I am a beginner to PL/SQL.Designing a UNIX CRON job to fetch the row count from four SELECT queries and compare the four outputs to be greater than zero.Below is the PL/SQL code and when i execute this, syntax error is popping up,could someone help me out whether the syntax is correct or not..Thanking you in advance


WHENEVER SQLERROR EXIT 1
ROLLBACK;
SET serveroutput ON
DECLARE

count_1 NUMBER:=0
count_2 NUMBER:=0;
count_3 NUMBER:=0;
count_4 NUMBER:=0;

PROCEDURE write_log(p_logmessage IN VARCHAR2) AS
BEGIN
BEGIN
DBMS_OUTPUT.PUT_LINE(p_logmessage);

EXCEPTION
WHEN others THEN

IF SQLCODE = -20000 THEN
DBMS_OUTPUT.disable();
DBMS_OUTPUT.enable(1000000);
DBMS_OUTPUT.PUT_LINE(p_logmessage);
END IF;

END;
END write_log;

BEGIN

write_log('Calclulation of missing Forecasted Site/Articles process started');

DBMS_OUTPUT.enable(1000000);

BEGIN


select sum(count) into count_1 from
(
select count (*) from table_a
);


select sum(count) into count_2 from
(
select count (*) from table_a
);


select sum(count) into count_3 from
(
select count (*) from table_a
);

select sum(count) into count_4 from
(
select count (*) from table_a
);


DBMS_OUTPUT.PUT_LINE('count is:'||count_1);

DBMS_OUTPUT.PUT_LINE('count is:'|| count_2);

DBMS_OUTPUT.PUT_LINE('count is:'|| count_3);

DBMS_OUTPUT.PUT_LINE('count is:'|| count_4);

write_log('oprocess completed');


---Validating the record Count -----

IF (count_1>0 OR count_2>0 OR count_3>0 OR count_4>0) THEN

raise_application_error(-20001,'records are present'||SQLCODE||' -ERROR- '||SQLERRM);

Else

write_log('No records present');

END IF;

EXIT;
/
Re: Help me on Syntax of PL/SQL [message #646315 is a reply to message #646314] Thu, 24 December 2015 09:41 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

This
select sum(count) into count_1 from 
(
select count (*) from table_a
);
should probably be this
select count (*) into count_1 from table_a;
but as you haven't shared the error, I can't be sure.
Re: Help me on Syntax of PL/SQL [message #646316 is a reply to message #646315] Thu, 24 December 2015 09:54 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
I suggest not using cron, but instead use DBMS_SCHEDULER in order to keep everything within the database.
Realize & understand that NOBODY will ever actually see any results even after all errors have been corrected, because there is no terminal or display attached to any background job. The output from PUT_LINE simply disappear into the virtual bit bucket.
Re: Help me on Syntax of PL/SQL [message #646362 is a reply to message #646316] Mon, 28 December 2015 12:29 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Try the following

DECLARE
FLAG VARCHAR2(1);
BEGIN
select nvl(max('N'),'Y')
INTO FLAG
FROM DUAL
WHERE (select count(*) from table_a) > 0
OR (select count(*) from table_b) > 0
OR (select count(*) from table_c) > 0
OR (select count(*) from table_d) > 0;

IF FLAG = 'N' THEN

raise_application_error(-20001,'records are present');

Else 
DBMS_OUTPUT.PUT_LINE('No records present');
END IF;
END;
/

[Updated on: Mon, 28 December 2015 12:30]

Report message to a moderator

Re: Help me on Syntax of PL/SQL [message #646366 is a reply to message #646362] Mon, 28 December 2015 14:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No need to count all rows to find if (at least) one is present.

Re: Help me on Syntax of PL/SQL [message #646367 is a reply to message #646366] Mon, 28 December 2015 16:04 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Your totally correct, it should be

DECLARE
FLAG VARCHAR2(1);
BEGIN
select nvl(max('N'),'Y')
INTO FLAG
FROM DUAL
WHERE exists (select null from table_a)
OR  exists (select null from table_b) 
OR  exists (select null from table_c)
OR exists (select null from table_d);

IF FLAG = 'N' THEN

raise_application_error(-20001,'records are present');

Else 
DBMS_OUTPUT.PUT_LINE('No records present');
END IF;
END;
/


Nice catch Michel.
Re: Help me on Syntax of PL/SQL [message #646481 is a reply to message #646367] Thu, 31 December 2015 21:59 Go to previous message
royal4111
Messages: 2
Registered: December 2015
Location: Bangalore
Junior Member
Thank you guys for all your inputs.I will execute and let you know the outcome..
Previous Topic: Can't create a Materialised view
Next Topic: Getting ORA-01839: date not valid for month specified
Goto Forum:
  


Current Time: Fri Apr 26 18:48:50 CDT 2024