Unix Technical Forum

pl/sql forcing RHS of = in where clause to be variable?

This is a discussion on pl/sql forcing RHS of = in where clause to be variable? within the Oracle Miscellaneous forums, part of the Oracle Database category; --> This code will compare a column to a variable: declare myx number; begin select * from foo where x=myx; ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-02-2008, 06:11 AM
mh@pixar.com
 
Posts: n/a
Default pl/sql forcing RHS of = in where clause to be variable?

This code will compare a column to a variable:

declare myx number;
begin
select * from foo where x=myx;
end;

but suppose I have a variable with the same name as a column:

declare x number;
begin
select * from foo where x=x;
end;

This will compare column x with column x, and not with the
variable x, right? Is there syntax to force the rightmost
x to be a variable? Or should I do

myx := x;
select * from foo where x=myx;

This is in the context of a parameter name of a public
function, so I don't want to do the obvious choice
of renaming the variable.

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 07-02-2008, 06:11 AM
Maxim Demenko
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

mh@pixar.com schrieb:
> x to be a variable? Or should I do
>
> myx := x;
> select * from foo where x=myx;
>
> This is in the context of a parameter name of a public
> function, so I don't want to do the obvious choice
> of renaming the variable.


I don't think, you have too many choices.

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-02-2008, 06:11 AM
Urs Metzger
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

mh@pixar.com schrieb:
> This code will compare a column to a variable:
>
> declare myx number;
> begin
> select * from foo where x=myx;
> end;
>
> but suppose I have a variable with the same name as a column:
>
> declare x number;
> begin
> select * from foo where x=x;
> end;
>
> This will compare column x with column x, and not with the
> variable x, right? Is there syntax to force the rightmost
> x to be a variable? Or should I do
>
> myx := x;
> select * from foo where x=myx;
>
> This is in the context of a parameter name of a public
> function, so I don't want to do the obvious choice
> of renaming the variable.
>
> Many TIA!
> Mark
>

Use a block with a label:

SQL> create table t(x number);

Table created.

SQL> insert into t values(42);

1 row created.

SQL> set serverout on
SQL> declare
2 x number;
3 d number;
4 begin
5 select * into d from t where x = x;
6 dbms_output.put_line('d = ' || to_char(d));
7 end;
8 /
d = 42

PL/SQL procedure successfully completed.

SQL> begin
2 <<my_label>>
3 declare
4 x number := 5;
5 d number;
6 begin
7 select * into d from t where x = my_label.x;
8 dbms_output.put_line('d = ' || to_char(d));
9 end;
10 end my_label;
11 /
begin
*
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 7

hth,
Urs Metzger
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-02-2008, 06:11 AM
Maxim Demenko
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

Urs Metzger schrieb:
> mh@pixar.com schrieb:
> Use a block with a label:
> Urs Metzger


This could do the job for regular plsql variables, but if the actual one
is a function parameter, i think, you *have* to reassign.

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-02-2008, 06:11 AM
Maxim Demenko
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

Maxim Demenko schrieb:
> Urs Metzger schrieb:
>> mh@pixar.com schrieb:
>> Use a block with a label:
>> Urs Metzger

>
> This could do the job for regular plsql variables, but if the actual one
> is a function parameter, i think, you *have* to reassign.
>
> Best regards
>
> Maxim


I should correct myself, it seems, parameter can be referenced with the
name of code unit...

SQL> create or replace function abc(empno number) return varchar2 is
2 l_ename varchar2(20);
3 begin
4 select ename into l_ename from emp where empno = abc.empno;
5 return l_ename;
6 end;
7 /

Function created.

SQL> select abc(7788) from dual;

ABC(7788)
-----------------------------------------------------------------------------------------------------------------------------
SCOTT


Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 07-02-2008, 06:11 AM
mh@pixar.com
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

Maxim Demenko <mdemenko@gmail.com> wrote:
> 4 select ename into l_ename from emp where empno = abc.empno;


Ah, this is just what I was hoping for.
Thank you so much Maxim!

--
Mark Harrison
Pixar Animation Studios
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-02-2008, 06:11 AM
Maxim Demenko
 
Posts: n/a
Default Re: pl/sql forcing RHS of = in where clause to be variable?

mh@pixar.com schrieb:
> Maxim Demenko <mdemenko@gmail.com> wrote:
>> 4 select ename into l_ename from emp where empno = abc.empno;

>
> Ah, this is just what I was hoping for.
> Thank you so much Maxim!
>


You are welcome, just for the record, all that can be found at usual
place ;-)

http://download.oracle.com/docs/cd/B....htm#sthref431

Best regards

Maxim
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com