Solved

Oracle Procedure

Posted on 2011-03-14
11
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 77

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 77

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 77

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to take different types of Oracle backups using RMAN.

724 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