Solved

Oracle Procedure

Posted on 2011-03-14
11
450 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
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 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fastest way to replace data in Oracle 5 62
Queries 15 39
PL SQL Search Across Columns 4 38
Where Does Time Value Come From for Database Insert or Update 4 28
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

778 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