Solved

Oracle Procedure

Posted on 2011-03-14
11
451 Views
Last Modified: 2012-08-14
Hi:
I can get this script that creates these views ported to Oracle but I’m missing a few things.  The first would be the procedure/function to get the current period.  Please could you help to replicate this procedure in Oracle?

create procedure "rddba".get_common_period( mem_date date )
returning integer;

define mem_month        integer;
define get_month        char(2);
define mem_yr           smallint;
define mem_period_key   integer;

        let mem_period_key = NULL;
        let get_month = LPAD( month(mem_date), 2, "0" );
        let mem_yr = year(mem_date);
       
        select period_key
        into mem_period_key
        from common_period spk
        where ((spk.month_number = get_month) and
                (spk.cal_year = mem_yr));

        return( mem_period_key );

end procedure;

TIA,
N
0
Comment
Question by:Nakuru1234
  • 5
  • 3
  • 3
11 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 35130470
how about this?


CREATE FUNCTION "rddba".get_common_period(mem_date DATE)
    RETURN INTEGER
IS
    mem_month        INTEGER;
    get_month        CHAR(2);
    mem_yr           SMALLINT;
    mem_period_key   INTEGER;
BEGIN
    mem_period_key  := NULL;
    get_month       := TO_CHAR(mem_date, 'mm');
    mem_yr          := TO_NUMBER(TO_CHAR(mem_date, 'yyyy'));

    SELECT period_key
      INTO mem_period_key
      FROM common_period spk
     WHERE ((spk.month_number = get_month) AND (spk.cal_year = mem_yr));

    RETURN (mem_period_key);
END;
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35130475
Something like:

--procedure cannot return values in Oracle
create or replace function rddba.get_common_period( mem_date in date )
returning integer
is

 mem_month        number;
 get_month        char(2) := to_char(mem_date,'MM');
 mem_yr           number := to_char(mem_dte('YYYY');
 mem_period_key   number := null;

        select period_key
        into mem_period_key
        from common_period spk
        where ((spk.month_number = get_month) and
                (spk.cal_year = mem_yr));

        return( mem_period_key );

end;

0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35130706
Go with sdstuber's.  My code has a couple of issues.

"returning integer
is
"

should be:
return integer
is
begin
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:Nakuru1234
ID: 35131271
I understand. I'll go with sdstuber...thx!

N
0
 

Author Comment

by:Nakuru1234
ID: 35132321
I tried to compile sdstuber CREATE FUNCTION but I keep getting this error;

Line # = 1 Column # = 26 Error Text = PLS-00103: Encountered the symbol "CREATE" when expecting one of the following:

   ( return compress compiled wrapped

Any idea why?

TIA,
N


0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35132327
if your function already exists you will need

create or replace
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35132338
but your syntax error would seem to indicte you have some other text you are trying to compile
0
 

Author Comment

by:Nakuru1234
ID: 35132604
Is "CREATE" supposed to be on the beginning of the SQL? This is how I tried to compile it...

CREATE FUNCTION "rddba".get_common_period(mem_date DATE)
    RETURN INTEGER
IS
    mem_month        INTEGER;
    get_month        CHAR(2);
    mem_yr           SMALLINT;
    mem_period_key   INTEGER;
BEGIN
    mem_period_key  := NULL;
    get_month       := TO_CHAR(mem_date, 'mm');
    mem_yr          := TO_NUMBER(TO_CHAR(mem_date, 'yyyy'));
 
    SELECT period_key
      INTO mem_period_key
      FROM common_period spk
     WHERE ((spk.month_number = get_month) AND (spk.cal_year = mem_yr));
 
    RETURN (mem_period_key);
END;


TIA,
N
0
 

Author Comment

by:Nakuru1234
ID: 35132690
Don't worry...it had already been created that's why I was getting the error. Thanks so much!.

N
0
 

Author Closing Comment

by:Nakuru1234
ID: 35132723
Excellent!!!
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35137544
>>it had already been created that's why I was getting the error

Even though you already closed this, get in the habit of adding 'or replace' to the create and you won't get the error.

create or replace function
...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question