Home » SQL & PL/SQL » SQL & PL/SQL » What we should use for better function or procedure
What we should use for better function or procedure [message #621760] Mon, 18 August 2014 01:40 Go to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Hi Guys,

I want to print prime number 1 to 100 in pl/sql block then what we should use here means function or procedure and why.
Re: What we should use for better function or procedure [message #621762 is a reply to message #621760] Mon, 18 August 2014 01:52 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

How to Check if a Number Is Prime

Note that MOD function directly gives you the remainder of a division.

Re: What we should use for better function or procedure [message #621764 is a reply to message #621760] Mon, 18 August 2014 02:15 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
College homework question? Is the problem with the algorithm or with the PL/SQL?
Re: What we should use for better function or procedure [message #621766 is a reply to message #621760] Mon, 18 August 2014 02:31 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
http://www.orafaq.com/forum/m/612145/?srch=prime+number#msg_612145
Re: What we should use for better function or procedure [message #621767 is a reply to message #621762] Mon, 18 August 2014 02:33 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Michel Cadot wrote on Mon, 18 August 2014 01:52

How to Check if a Number Is Prime

Note that MOD function directly gives you the remainder of a division.



Thank you Michel for sharing supporting link
But I want to know what is better to use here
as
Create or replace function is_prime(prime number)

or

Create or replace procedure is_prime(prime number)
Re: What we should use for better function or procedure [message #621768 is a reply to message #621767] Mon, 18 August 2014 02:34 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
A function.

[EDIT] Why? Because you can use that function in SQL; for a procedure (with an OUT parameter), you have to use PL/SQL.

[Updated on: Mon, 18 August 2014 02:35]

Report message to a moderator

Re: What we should use for better function or procedure [message #621769 is a reply to message #621767] Mon, 18 August 2014 02:35 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
A procedure.

[EDIT] Why? Because it has printed output, which is a side effect and a Bad Thing.

[Updated on: Mon, 18 August 2014 02:37]

Report message to a moderator

Re: What we should use for better function or procedure [message #621770 is a reply to message #621767] Mon, 18 August 2014 02:51 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
A SQL? Unless there is a real need of pl/sql.

Re: What we should use for better function or procedure [message #621771 is a reply to message #621769] Mon, 18 August 2014 02:51 Go to previous messageGo to next message
aaditya321
Messages: 225
Registered: January 2014
Location: Delhi
Senior Member
Thanks to all every guys.
Re: What we should use for better function or procedure [message #638352 is a reply to message #621771] Wed, 10 June 2015 00:13 Go to previous messageGo to next message
abrarkiit
Messages: 5
Registered: May 2015
Location: India
Junior Member

DECLARE
N NUMBER;
CNT NUMBER:=0;
BEGIN
FOR I IN 1..100 LOOP
FOR J IN 1..I LOOP
IF MOD(I,J)=0 THEN
CNT:=CNT+1;
END IF;
END LOOP;
IF CNT <= 2 THEN
IF MOD(I,2)!=0 THEN
DBMS_OUTPUT.PUT_LINE(I);
END IF;
END IF;
CNT:=0;
END LOOP;
END;
/

hope this help u.
Re: What we should use for better function or procedure [message #638355 is a reply to message #638352] Wed, 10 June 2015 00:54 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I doubt it will as solutions have already been given about one year ago.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Previous Topic: SQL query help
Next Topic: Difference
Goto Forum:
  


Current Time: Fri Apr 26 11:52:28 CDT 2024