Home » SQL & PL/SQL » SQL & PL/SQL » using function in select statement giving same value
using function in select statement giving same value [message #633715] Tue, 24 February 2015 06:41 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi all,

I have written one function in the package and used in the select Query in the For loop

But it is return same value for all items why? & I tried Query manually it is fine
Re: using function in select statement giving same value [message #633719 is a reply to message #633715] Tue, 24 February 2015 06:44 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And the function looks like? Is it invisible? Because I don't see anything.

Once again, if you have not yet bookmarked the links. Please bookmark it now, read and follow the OraFAQ Forum Guide and How to use [code] tags, to enable us to help you.

[Updated on: Tue, 24 February 2015 06:46]

Report message to a moderator

Re: using function in select statement giving same value [message #633721 is a reply to message #633719] Tue, 24 February 2015 06:47 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
FUNCTION Get_rate(from_currency_code IN VARCHAR2, 
                  to_currency_code   IN VARCHAR2, 
                  conversion_type    IN VARCHAR2, 
                  conversion_date    IN DATE) 
RETURN NUMBER 
IS 
  v_rate NUMBER; 
  CURSOR c1 IS 
    SELECT conversion_rate 
    FROM   gl_daily_rates 
    WHERE  from_currency = from_currency_code 
           AND to_currency = to_currency_code 
           AND conversion_type = conversion_type 
           AND conversion_date = conversion_date; 
BEGIN 
    dbms_output.Put_line(from_currency_code ||to_currency_code ||conversion_type ||To_char(conversion_date, 'DD-MON-YYYY')); 
  
    FOR c1rec IN c1 LOOP 
        dbms_output.Put_line(c1rec.conversion_rate); 
        v_conversion_rate := c1rec.conversion_rate; 
    END LOOP; 

    RETURN v_rate ; 
EXCEPTION 
  WHEN OTHERS THEN 
             dbms_output.Put_line(SQLERRM); 

             v_rate := 1; 

             RETURN v_rate ; 
END Get_rate; 

Re: using function in select statement giving same value [message #633722 is a reply to message #633719] Tue, 24 February 2015 06:51 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi Lalit ,

select * from gl_daily_rates
where from_currency = 'RMB' 
 and conversion_type = 'Corporate' 
and conversion_date = '13-AUG-14' 
and to_currency = 'USD';

output
------
0.16344

select Pkg.Get_rate('RMB','USD','Corporate' , '13-AUG-14'  ) from dual  
output
------
0.16258  


Re: using function in select statement giving same value [message #633725 is a reply to message #633722] Tue, 24 February 2015 07:01 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You are a liar.
Your function can't return this.

Re: using function in select statement giving same value [message #633726 is a reply to message #633725] Tue, 24 February 2015 07:03 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Thank you for reply Michel
Quote:
Your function can't return this.

Which one??
Re: using function in select statement giving same value [message #633727 is a reply to message #633726] Tue, 24 February 2015 07:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Get_rate

Is this not the only one you posted?

Re: using function in select statement giving same value [message #633728 is a reply to message #633727] Tue, 24 February 2015 07:09 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

Get_rate

Is this not the only one you posted?

You mean you want package also or function output??
Re: using function in select statement giving same value [message #633729 is a reply to message #633725] Tue, 24 February 2015 07:13 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

You are a liar.
Your function can't return this.

SQL> ed
Wrote file afiedt.buf

  1  create or replace  FUNCTION Get_rate(from_currency_code IN VARCHAR2, to_currency_code
  2                                        conversion_type IN VARCHAR2,conversi
  3  v_rate NUMBER;
  4  CURSOR c1 IS
  5    SELECT conversion_rate
  7     FROM GL_DAILY_RATES
  8     WHERE from_currency     = from_currency_code
  9     AND to_currency     = to_currency_code
 10     AND conversion_type     = conversion_type
 11    AND conversion_date     = conversion_date;
 12  BEGIN
 13   dbms_output.put_line(from_currency_code ||to_currency_code||conversion_type||TO_CHAR(conversio
 14  FOR c1rec IN c1 LOOP
 15   dbms_output.put_line(c1rec.conversion_rate);
 16   v_rate := c1rec.conversion_rate;
 17  END LOOP;
 18   RETURN v_rate ;
 19   EXCEPTION
 20     WHEN OTHERS THEN
 21    dbms_output.put_line(SQLERRM);
 22     v_rate := 1;
 23      RETURN v_rate ;
 24* END   Get_rate;
 25  /

Function created.


SQL> select CONVERSION_RATE from gl_daily_rates
  2  where from_currency = 'RMB' 
  3   and conversion_type = 'Corporate' 
  4  and conversion_date = '13-AUG-14' 
  5  and to_currency = 'USD';

CONVERSION_RATE
---------------
         .16344

[Updated on: Tue, 24 February 2015 07:14]

Report message to a moderator

Re: using function in select statement giving same value [message #633731 is a reply to message #633728] Tue, 24 February 2015 07:18 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>To_char(conversion_date, 'DD-MON-YYYY'));

And,

>and conversion_date = '13-AUG-14'

You really don't understand, do you?
Re: using function in select statement giving same value [message #633732 is a reply to message #633731] Tue, 24 February 2015 07:22 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

>To_char(conversion_date, 'DD-MON-YYYY'));

And,

>and conversion_date = '13-AUG-14'

You really don't understand, do you?

No.Can you please explain me??

I have used conversion_date = '13-AUG-2014', getting same problem
Re: using function in select statement giving same value [message #633733 is a reply to message #633732] Tue, 24 February 2015 07:23 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Your names aren't going to help any.

 AND conversion_type     = conversion_type
Re: using function in select statement giving same value [message #633735 is a reply to message #633733] Tue, 24 February 2015 07:25 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

Your names aren't going to help any.

AND conversion_type = conversion_type

hi,I can't understand .can you please explain , what are the problems we are facing??
Re: using function in select statement giving same value [message #633736 is a reply to message #633735] Tue, 24 February 2015 07:27 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
When is that ever going to be anything but true?

It's equivalent to 1=1
Re: using function in select statement giving same value [message #633737 is a reply to message #633733] Tue, 24 February 2015 07:27 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
I wish he executes the select query in the cursor explicitly, and finds the reason himself.
Re: using function in select statement giving same value [message #633738 is a reply to message #633737] Tue, 24 February 2015 07:29 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>I can't understand .can you please explain

What happens when you execute,

Select * from your_table where 1 = 1


And,'13-AUG-14' is NOT a DATE, it is a string. Oracle provides to_date to convert string into date.

[Updated on: Tue, 24 February 2015 07:32]

Report message to a moderator

Re: using function in select statement giving same value [message #633739 is a reply to message #633738] Tue, 24 February 2015 07:33 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

What happens when you execute,

Select * from your_table where 1 = 1

It is if the columns are values 1234=1234 ,abc123=abc123 etc..
Re: using function in select statement giving same value [message #633740 is a reply to message #633739] Tue, 24 February 2015 07:37 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
So, if you understand that, then what would happen with this:

16   v_rate := c1rec.conversion_rate;
 17  END LOOP;
 18   RETURN v_rate ;


It always returns the same value. Since you select the same value, always.

[Updated on: Tue, 24 February 2015 07:41]

Report message to a moderator

Re: using function in select statement giving same value [message #633741 is a reply to message #633739] Tue, 24 February 2015 07:39 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
mist598 wrote on Tue, 24 February 2015 13:33
Quote:

What happens when you execute,

Select * from your_table where 1 = 1

It is if the columns are values 1234=1234 ,abc123=abc123 etc..


I have no idea if that means you understand the problem or not.

Plus you have another problem.
You loop over a cursor and return the last value it selects. The select has no order by so the value you get is random.
Re: using function in select statement giving same value [message #633742 is a reply to message #633729] Tue, 24 February 2015 07:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
mist598 wrote on Tue, 24 February 2015 14:13
Quote:

You are a liar.
Your function can't return this.

SQL> ed
Wrote file afiedt.buf

  1  create or replace  FUNCTION Get_rate(from_currency_code IN VARCHAR2, to_currency_code
  2                                        conversion_type IN VARCHAR2,conversi
  3  v_rate NUMBER;
  4  CURSOR c1 IS
  5    SELECT conversion_rate
  7     FROM GL_DAILY_RATES
  8     WHERE from_currency     = from_currency_code
  9     AND to_currency     = to_currency_code
 10     AND conversion_type     = conversion_type
 11    AND conversion_date     = conversion_date;
 12  BEGIN
 13   dbms_output.put_line(from_currency_code ||to_currency_code||conversion_type||TO_CHAR(conversio
 14  FOR c1rec IN c1 LOOP
 15   dbms_output.put_line(c1rec.conversion_rate);
 16   v_rate := c1rec.conversion_rate;
 17  END LOOP;
 18   RETURN v_rate ;
 19   EXCEPTION
 20     WHEN OTHERS THEN
 21    dbms_output.put_line(SQLERRM);
 22     v_rate := 1;
 23      RETURN v_rate ;
 24* END   Get_rate;
 25  /

Function created.


SQL> select CONVERSION_RATE from gl_daily_rates
  2  where from_currency = 'RMB' 
  3   and conversion_type = 'Corporate' 
  4  and conversion_date = '13-AUG-14' 
  5  and to_currency = 'USD';

CONVERSION_RATE
---------------
         .16344


This does not prove anything as you don't call the function!

Re: using function in select statement giving same value [message #633743 is a reply to message #633742] Tue, 24 February 2015 07:43 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
This does not prove anything as you don't call the function!

Hi, i can't understand please explain me practically
Re: using function in select statement giving same value [message #633744 is a reply to message #633741] Tue, 24 February 2015 07:45 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

Plus you have another problem.
You loop over a cursor and return the last value it selects. The select has no order by so the value you get is random.

Yes,can you please provide me the code what are the required one
Re: using function in select statement giving same value [message #633745 is a reply to message #633744] Tue, 24 February 2015 07:50 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
No I can't.
Because, as is almost always the case with your questions, you haven't told us anything about your table structures or the logic you're trying to apply to them.
All we have to go on is some code that doesn't do what you think it should.
Re: using function in select statement giving same value [message #633746 is a reply to message #633745] Tue, 24 February 2015 07:53 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
And you still haven't posted the complete function code. Your last attempt is obviously missing stuff from lines 2 and 13.
Re: using function in select statement giving same value [message #633747 is a reply to message #633746] Tue, 24 February 2015 07:56 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Create or replace 
FUNCTION Get_rate(from_currency_code IN VARCHAR2, 
                  to_currency_code   IN VARCHAR2, 
                  p_conversion_type    IN VARCHAR2, 
                  p_conversion_date    IN DATE) 
RETURN NUMBER 
IS 
  v_rate NUMBER; 
  CURSOR c1 IS 
    SELECT conversion_rate 
    FROM   gl_daily_rates 
    WHERE  from_currency = from_currency_code 
           AND to_currency = to_currency_code 
           AND conversion_type = p_conversion_type 
           AND conversion_date = p_conversion_date; 
BEGIN 
    dbms_output.Put_line(from_currency_code ||to_currency_code ||p_conversion_type ||To_char(p_conversion_date, 'DD-MON-YYYY')); 
    FOR c1rec IN c1 LOOP 
        dbms_output.Put_line(c1rec.conversion_rate); 
        v_rate := c1rec.conversion_rate; 
    END LOOP; 
    RETURN v_rate ; 
EXCEPTION 
  WHEN OTHERS THEN 
             dbms_output.Put_line(SQLERRM); 
             v_rate := 1; 
             RETURN v_rate ; 
END Get_rate; 

select Get_rate('RMB','USD','Corporate' , '13-AUG-14'  )  from dual  



The above working fine.But why i am getting same values for all the items while using in the select Query Please help
Re: using function in select statement giving same value [message #633748 is a reply to message #633747] Tue, 24 February 2015 08:04 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Why do always post different code? The one you posted as a reply to Michel is different from what you posted now. Which one is supposed to be the correct one?
Re: using function in select statement giving same value [message #633749 is a reply to message #633748] Tue, 24 February 2015 08:08 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Why do always post different code? The one you posted as a reply to Michel is different from what you posted now. Which one is supposed to be the correct one?

Sorry Lalit i am not inform you, the latest posted one is the correct one & i have changed the parameter's
--conversion_type IN VARCHAR2, 
--conversion_date IN DATE      
p_conversion_type IN VARCHAR2, 
p_conversion_date IN DATE      

And all other is same

Re: using function in select statement giving same value [message #633750 is a reply to message #633749] Tue, 24 February 2015 08:12 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
>And all other is same

Show us using exact copy paste without any manipulation.

Open SQL*Plus, execute the function, execute the SQL, copy paste the session and post it here. The post should prove what you claim.
Re: using function in select statement giving same value [message #633751 is a reply to message #633750] Tue, 24 February 2015 08:15 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Wrote file afiedt.buf

  1  CREATE OR REPLACE FUNCTION Get_rate (from_currency_code   IN VARCHAR2,
  2                                       to_currency_code     IN VARCHAR2,
  3                                       p_conversion_type    IN VARCHAR2,
  4                                       p_conversion_date    IN DATE)
  5     RETURN NUMBER
  6  IS
  7     v_rate   NUMBER;
  8     CURSOR c1
  9     IS
 10        SELECT conversion_rate
 11          FROM gl_daily_rates
 12         WHERE     from_currency = from_currency_code
 13               AND to_currency = to_currency_code
 14               AND conversion_type = p_conversion_type
 15               AND conversion_date = p_conversion_date;
 16  BEGIN
 17     DBMS_OUTPUT.Put_line (
 18           from_currency_code
 19        || to_currency_code
 20        || p_conversion_type
 21        || TO_CHAR (p_conversion_date, 'DD-MON-YYYY'));
 22     FOR c1rec IN c1
 23     LOOP
 24        DBMS_OUTPUT.Put_line (c1rec.conversion_rate);
 25        v_rate := c1rec.conversion_rate;
 26     END LOOP;
 27     RETURN v_rate;
 28  EXCEPTION
 29     WHEN OTHERS
 30     THEN
 31        DBMS_OUTPUT.Put_line (SQLERRM);
 32        v_rate := 1;
 33        RETURN v_rate;
 34* END Get_rate;
SQL> /

Function created.

SQL> select Get_rate('RMB','USD','Corporate' , '13-AUG-14'  )  from dual  
  2  /

GET_RATE('RMB','USD','CORPORATE','13-AUG-14')
---------------------------------------------
                                       .16235

SQL> select CONVERSION_RATE from gl_daily_rates
  2  where from_currency = 'RMB' 
  3   and conversion_type = 'Corporate' 
  4  and To_char(conversion_date, 'DD-MON-YYYY') = '13-AUG-2014' 
  5  and to_currency = 'USD';

CONVERSION_RATE
---------------
         .16235

but in the select Statement 
output
0.16258

[Updated on: Tue, 24 February 2015 08:16]

Report message to a moderator

Re: using function in select statement giving same value [message #633752 is a reply to message #633751] Tue, 24 February 2015 08:18 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Your queries are different.

Also - what select statement? Is there another one we've not seen?

[Updated on: Tue, 24 February 2015 08:19]

Report message to a moderator

Re: using function in select statement giving same value [message #633753 is a reply to message #633752] Tue, 24 February 2015 08:20 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
Your queries are different.

Sorry for that please consider as the Latest one , manually it is working fine, but in the select statement it is giving same result
Re: using function in select statement giving same value [message #633754 is a reply to message #633753] Tue, 24 February 2015 08:23 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
I'm referring to the latest post. They are not the same.


It's also (at least to me) still not clear where this select that is getting the "wrong" answer is, or how it is formed.

[Updated on: Tue, 24 February 2015 08:26]

Report message to a moderator

Re: using function in select statement giving same value [message #633755 is a reply to message #633754] Tue, 24 February 2015 08:26 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
select 
,f.primary_uom_code     Attribute5   
,PKG.get_rate(ooh.transactional_curr_code,'USD',ooh.conversion_type_code,TRUNC(ooh.ordered_date))
.
.
.
from tables 

It is giving different value and same value for all the fetched items
Re: using function in select statement giving same value [message #633756 is a reply to message #633755] Tue, 24 February 2015 08:27 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Based on that, the problem is probably on line 4. Or maybe 6.

Working test case required.
Re: using function in select statement giving same value [message #633759 is a reply to message #633756] Tue, 24 February 2015 08:31 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:

Based on that, the problem is probably on line 4. Or maybe 6.

Working test case required.

What is the code on on line 4. Or maybe 6.based on your view
Re: using function in select statement giving same value [message #633761 is a reply to message #633759] Tue, 24 February 2015 08:42 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Go through the entire thread once. And you let us know where you think is the issue. You confuse us with your repeatedly manipulated code.
Re: using function in select statement giving same value [message #633765 is a reply to message #633761] Tue, 24 February 2015 08:58 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
Reading through this whole thread I got dizziness Smile
Re: using function in select statement giving same value [message #633766 is a reply to message #633755] Tue, 24 February 2015 09:03 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
mist598 wrote on Tue, 24 February 2015 14:26
select 
,f.primary_uom_code     Attribute5   
,PKG.get_rate(ooh.transactional_curr_code,'USD',ooh.conversion_type_code,TRUNC(ooh.ordered_date))
.
.
.
from tables 

It is giving different value and same value for all the fetched items


Do you realize how vague that is. You might as well have just said you're getting some values back.

You need to give us an actual example where it gives us a result that isn't what you expect. You need to tell us what result you do expect and why you expect it. you need to give us the data that's in the gl_daily_rates table.

And you still need to explain why you're using an un-ordered for loop to get the rate.
Re: using function in select statement giving same value [message #633767 is a reply to message #633765] Tue, 24 February 2015 09:03 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
And your suggestion is?
Re: using function in select statement giving same value [message #633769 is a reply to message #633767] Tue, 24 February 2015 09:07 Go to previous messageGo to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>And your suggestion is?

do NOT respond to OP, unless & until actually conforms to all Posting Guidelines
Previous Topic: PL/SQL for Insert/Update
Next Topic: Oracle dbms scheduler
Goto Forum:
  


Current Time: Fri Apr 26 02:05:39 CDT 2024