Re: PRAGMA Question

From: Scott Urman <surman_at_dlsun338.us.oracle.com>
Date: 1996/11/27
Message-ID: <57g0o4$de2_at_inet-nntp-gw-1.us.oracle.com>#1/1


In article <E1HIvL.78E_at_sunyit.edu>, xsah1_at_sunyit.edu (Scott Humphrey) writes:
|> I need some help regarding pragmas. I keyed a program directly from the
|> Oracle PL/SQL Programming book for generating random numbers as follows:
|>
|>
|> CREATE OR REPLACE PACKAGE RANDOM AS
|>
|> PROCEDURE ChangeSeed(p_NewSeed IN NUMBER);
|>
|> FUNCTION Rand RETURN NUMBER;
|> PRAGMA RESTRICT_REFERENCES(rand,WNDS);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take out this line.

|>
|> PROCEDURE GetRand(p_RandomNumber OUT NUMBER);
|>
|> FUNCTION RandMax(P_MaxVal IN NUMBER) RETURN NUMBER;
|> PRAGMA RESTRICT_REFERENCES(RandMax,WNDS);
|>
|> PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER,
|> p_MaxVal IN NUMBER);
|> END Random;
|> /
|> CREATE OR REPLACE PACKAGE BODY RANDOM AS
|>
|> v_Multiplier CONSTANT NUMBER := 22695477;
|> v_Increment CONSTANT NUMBER := 1;
|> v_seed number := 1;
|>
|> Procedure ChangeSeed(p_NewSeed IN NUMBER) IS
|> BEGIN
|> v_seed := p_newSeed;
|> END ChangeSeed;
|>
|> FUNCTION Rand RETURN NUMBER IS
|> BEGIN
|> v_seed := MOD(v_multiplier * v_seed + v_increment,(2 ** 32));
|> RETURN BITAND (v_seed/(2 ** 16), 32767);
|> END Rand;
|>
|> PROCEDURE GetRand (p_RandomNumber OUT NUMBER) IS
|> BEGIN
|> p_randomNumber := Rand;
|> END GetRand;
|>
|> FUNCTION RandMax (p_MaxVal IN NUMBER) RETURN NUMBER IS
|> BEGIN
|> RETURN MOD (Rand,p_MaxVal) + 1;
|> END RandMax;
|>
|> PROCEDURE GetRandMax(p_RandomNumber OUT NUMBER,
|> p_MaxVal IN NUMBER) IS
|> BEGIN
|> p_RandomNumber := RandMax(P_MaxVal);
|> END GetRandMax;
|>
|> BEGIN
|> ChangeSeed(TO_NUMBER(TO_CHAR(SYSDATE,'SSSSS')));
|> END Random;
|> /
|>
|> This gives me the error
|>
|> PLS-00452 'rand' violates its associated pragma.
|>
|> Can someone explain why I get these pragma errors despite copying it
|> right from the Oracle Press book.

I haven't figured out why you get this error, since none of the functions write database state. I personally think this is a bug of some sort, but haven't done enough research on it to determine it absolutely. If you remove the PRAGMA RESTRICT_REFERENCES line it will work fine, but you won't be able to call Rand from a SQL statement.

|>
|> Thanks,
|>
|> Scott Humphrey
|> Senior Programmer Analyst
|> xsah1_at_sunyit.edu
|>
|>



Scott Urman Oracle Corporation surman_at_us.oracle.com

Author of _Oracle PL/SQL Programming_ ISBN 0-07-882176-2 Published by Oracle Press - http://www.osborne.com/oracle/index.htm

"The opinions expressed here are my own, and are not necessarily that of  Oracle Corporation"
Received on Wed Nov 27 1996 - 00:00:00 CET

Original text of this message