Solved

Oracle Procedure

Posted on 2011-03-14
11
442 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 73

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
 

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
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.

 
LVL 73

Expert Comment

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

create or replace
0
 
LVL 73

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.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now