Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle Procedure

Posted on 2011-03-14
11
Medium Priority
?
472 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 2000 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

610 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