Home » SQL & PL/SQL » SQL & PL/SQL » substitution variable with value assigned and without value assigned(2 Merged) (Oracle 10g R2)
substitution variable with value assigned and without value assigned(2 Merged) [message #511700] Tue, 14 June 2011 12:18 Go to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Hi All,

I have a table emp with columns [id,name,job_id,dept_id].
now I make a simple select query.

select id,name,job_id,dept_id
from emp
where id = &a
and name = &b
and job_id =&c
and dept_id =&d;

now in this case it will ask the values for all the columns and will according print result.

Now my questions is what will happen if I just pass the value for only id and name not for other two...?

Thanks
Deepak



Re: substitution variable with value assigned and without value assigned [message #511702 is a reply to message #511700] Tue, 14 June 2011 12:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Are really too LAZY to do yourself rather than ask here?

>Now my questions is what will happen if I just pass the value for only id and name not for other two...?
Please do so & post the results.
Re: substitution variable with value assigned and without value assigned [message #511706 is a reply to message #511702] Tue, 14 June 2011 12:37 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Sorry Sir I my question found you stupid thing to answer. My actual question is that , If I just enter the value for any of the four particular substitution variable, then how can i ignore the remaning prompts.

Like if I just enter the value for id only or dept_id only, or you can say If I just want to enter values for two variable, it should ignore the remaining prompts if I do not want to enter.

Thanks
Deepak
Re: substitution variable with value assigned and without value assigned [message #511708 is a reply to message #511700] Tue, 14 June 2011 12:38 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
Now my questions is what will happen if I just pass the value for only id and name not for other two...?

Then what should be the result?

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #511711 is a reply to message #511706] Tue, 14 June 2011 12:41 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
You will have to code for it. Either use DECODE or CASE for each of your WHERE clauses and code it accordingly.
Re: substitution variable with value assigned and without value assigned [message #511720 is a reply to message #511708] Tue, 14 June 2011 13:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Michel Cadot wrote on Tue, 14 June 2011 19:38
Quote:
Now my questions is what will happen if I just pass the value for only id and name not for other two...?

Then what should be the result?

Regards
Michel


I mean "remove the condition" or "the column is null" or ...

Regards
Michel

Re: substitution variable with value assigned and without value assigned [message #511721 is a reply to message #511708] Tue, 14 June 2011 13:33 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Here is the result for the question

SQL> select empno,ename,job,deptno from emp where empno= &a and deptno= &b;
Enter value for a: 7900
Enter value for b:
old   1: select empno,ename,job,deptno from emp where empno= &a and deptno= &b
new   1: select empno,ename,job,deptno from emp where empno= 7900 and deptno=
select empno,ename,job,deptno from emp where empno= 7900 and deptno=
                                                                    *
ERROR at line 1:
ORA-00936: missing expression


Regards
Deepak
Re: substitution variable with value assigned and without value assigned [message #511722 is a reply to message #511721] Tue, 14 June 2011 13:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
And what is the answer to my question?

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #511723 is a reply to message #511720] Tue, 14 June 2011 13:34 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Now can I clear my question what is the exact result I want?

Regards
Deepak
Re: substitution variable with value assigned and without value assigned [message #511724 is a reply to message #511711] Tue, 14 June 2011 13:38 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Hey thanks a lot Sir.. I think you got my point. what I want to say.. I have just given an example, If you can just show an example using the table EMP which I have used.

You can just use it for the two prompts conditions which I have used one for the empno and other for dept_no.

It will be great help from your side.

Thanks & Regards
Deepak
Re: substitution variable with value assigned and without value assigned [message #511725 is a reply to message #511724] Tue, 14 June 2011 13:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
But the answer depends on what you want to do with the condition when no value is given. What does it become? Is this you want the query ignore the condition or is this you want to convert this condition to "the column is null" or is this another thing?

Regards
Michel

[Edit: Quickly answer to the question if you want an answer from me as I'm going to disconnect in a few minutes.]


[Updated on: Tue, 14 June 2011 13:58]

Report message to a moderator

Re: substitution variable with value assigned and without value assigned [message #511727 is a reply to message #511725] Tue, 14 June 2011 14:12 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>ORA-00936: missing expression
What will you do to eliminate this error?
Re: substitution variable with value assigned and without value assigned [message #511750 is a reply to message #511725] Tue, 14 June 2011 19:04 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Right Michel Sir,, now your are very close to what I want the condition to do if I do not enter any value for the condition of particluar column.

I want to design the query such a way the :

let me explain you taking example of GUI:

I have created a simple form which is going to display details of the employes in the company.
Now What I have done is I have created four filter ( ie. the substitution varialble in where conditions) according to which it will display result.

Now I want that If I just enter value only for take dept_id then it should filter the result only assuming this is the only condition. It should ignore other three conditions and take all the value in those particular columns.

Similary If I want to enter the value for any of the two conditions then it should filter accordingly by taking all the values for the other two columns for which I have not enter any value.

It should not give me the error of missing expression.

Thanks n Regards
Deepak
Re: substitution variable with value assigned and without value assigned [message #511783 is a reply to message #511750] Wed, 15 June 2011 02:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
If it is a GUI you write, why don't you build the query on the fly depending on the parameter given by the use?
Or is this a SQL script you will run in SQL*Plus?
Or what?
Post a clear description of your or the end user environment.
If I give you a specific solution for SQL*Plus based on what you posted and you will not use SQL*Plus we just waste our both time.
It is not to bother you if I ask questions, just to have a clear picture of what is the case and what it has to do.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #511872 is a reply to message #511783] Wed, 15 June 2011 12:00 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Sir, I have to build a sql query for the same and then that query will be called in the code..

So We can proceed with building the SQL query if you can help me out with that SIR.

Thanks
Deepak
Re: substitution variable with value assigned and without value assigned [message #511873 is a reply to message #511872] Wed, 15 June 2011 12:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
What is the tool?
What is the prorgamming language?
What is your problem in writing the string for the query in your language?

You don't answer my questions.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #511895 is a reply to message #511873] Wed, 15 June 2011 17:03 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
The following will only work from SQL*Plus. Note that you need to have single quotes around the substitution variables in order for them to be recognized as null when no input is provided, then you can use an nvl function to make it so that it results in evaluating column=column so that the condition is eliminated. If you are designing a form in SQL*Plus, then this will work. Otherwise, you need to use whatever sort of variable your front end uses, instead of SQL*Plus substitution variables.

SCOTT@orcl_11gR2> select empno, ename, job, deptno
  2  from   emp
  3  where  empno  = nvl ('&a', empno)
  4  and    ename  = nvl ('&b', ename)
  5  and    job    = nvl ('&c', job)
  6  and    deptno = nvl ('&d', deptno)
  7  /
Enter value for a: 7369
old   3: where  empno  = nvl ('&a', empno)
new   3: where  empno  = nvl ('7369', empno)
Enter value for b: SMITH
old   4: and    ename  = nvl ('&b', ename)
new   4: and    ename  = nvl ('SMITH', ename)
Enter value for c: 
old   5: and    job    = nvl ('&c', job)
new   5: and    job    = nvl ('', job)
Enter value for d: 
old   6: and    deptno = nvl ('&d', deptno)
new   6: and    deptno = nvl ('', deptno)

     EMPNO ENAME      JOB           DEPTNO
---------- ---------- --------- ----------
      7369 SMITH      CLERK             20

1 row selected.

SCOTT@orcl_11gR2>

Re: substitution variable with value assigned and without value assigned [message #511903 is a reply to message #511895] Wed, 15 June 2011 19:37 Go to previous messageGo to next message
dkdms2124
Messages: 369
Registered: April 2010
Location: INDIA
Senior Member

Thanks a lot mam, I'll try this and see if it helped me out.

Regards
Deepak Smile
Re: substitution variable with value assigned and without value assigned [message #511992 is a reply to message #511895] Thu, 16 June 2011 08:03 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
Barbara Boehmer wrote on Wed, 15 June 2011 18:03
The following will only work from SQL*Plus. Note that you need to have single quotes around the substitution variables in order for them to be recognized as null when no input is provided, then you can use an nvl function to make it so that it results in evaluating column=column so that the condition is eliminated. If you are designing a form in SQL*Plus, then this will work. Otherwise, you need to use whatever sort of variable your front end uses, instead of SQL*Plus substitution variables.

SCOTT@orcl_11gR2> select empno, ename, job, deptno
  2  from   emp
  3  where  empno  = nvl ('&a', empno)
  4  and    ename  = nvl ('&b', ename)
  5  and    job    = nvl ('&c', job)
  6  and    deptno = nvl ('&d', deptno)
  7  /
Enter value for a: 7369
old   3: where  empno  = nvl ('&a', empno)
new   3: where  empno  = nvl ('7369', empno)
Enter value for b: SMITH
old   4: and    ename  = nvl ('&b', ename)
new   4: and    ename  = nvl ('SMITH', ename)
Enter value for c: 
old   5: and    job    = nvl ('&c', job)
new   5: and    job    = nvl ('', job)
Enter value for d: 
old   6: and    deptno = nvl ('&d', deptno)
new   6: and    deptno = nvl ('', deptno)

     EMPNO ENAME      JOB           DEPTNO
---------- ---------- --------- ----------
      7369 SMITH      CLERK             20

1 row selected.

SCOTT@orcl_11gR2>



The only problem with this is if any of the columns is NULL, that row will not match.

SQL> select * from emp;

     EMPNO ENAME      JOB            DEPTNO
---------- ---------- ---------- ----------
      7369 SMITH      CLERK

SQL> get afiedt.buf
  1  select empno, ename, job, deptno
  2      from   emp
  3      where  empno  = nvl ('&a', empno)
  4      and    ename  = nvl ('&b', ename)
  5      and    job    = nvl ('&c', job)
  6*     and    deptno = nvl ('&d', deptno)
SQL>/
Enter value for a: 7369
old   3:     where  empno  = nvl ('&a', empno)
new   3:     where  empno  = nvl ('7369', empno)
Enter value for b:
SMITHold   4:     and    ename  = nvl ('&b', ename)
new   4:     and    ename  = nvl ('', ename)
Enter value for c:
old   5:     and    job    = nvl ('&c', job)
new   5:     and    job    = nvl ('SMITH', job)
Enter value for d:
old   6:     and    deptno = nvl ('&d', deptno)
new   6:     and    deptno = nvl ('', deptno)

no rows selected
Re: substitution variable with value assigned and without value assigned [message #511997 is a reply to message #511992] Thu, 16 June 2011 08:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
The only problem with this is if any of the columns is NULL, that row will not match.

The ONLY one?
The MAIN one is that it is a SQL*Plus (or a SQL tool) solution but we still don't know what OP uses.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #511999 is a reply to message #511997] Thu, 16 June 2011 08:18 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
Michel Cadot wrote on Thu, 16 June 2011 09:14
Quote:
The only problem with this is if any of the columns is NULL, that row will not match.

The ONLY one?
The MAIN one is that it is a SQL*Plus (or a SQL tool) solution but we still don't know what OP uses.


Yeah, not the ONLY one. It's just a figure of speech. I'm good in math, not in english.
Re: substitution variable with value assigned and without value assigned [message #512051 is a reply to message #511992] Thu, 16 June 2011 13:45 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
joy_division wrote on Thu, 16 June 2011 06:03

... problem with this is if any of the columns is NULL, that row will not match.


The following fixes that. It ignores the condition if the parameter is null, as originally specified and when the corresponding column value is null it does not constitute a mismatch with a null parameter, but does constitute a mismatch with a parameter that has a value.

SCOTT@orcl_11gR2> update emp
  2  set deptno = null
  3  where empno = 7369
  4  /

1 row updated.

SCOTT@orcl_11gR2> accept a prompt 'enter value for a:  '
enter value for a:  7369
SCOTT@orcl_11gR2> accept b prompt 'enter value for b:  '
enter value for b:  
SCOTT@orcl_11gR2> accept c prompt 'enter value for c:  '
enter value for c:  
SCOTT@orcl_11gR2> accept d prompt 'enter value for d:  '
enter value for d:  
SCOTT@orcl_11gR2> select empno, ename, job, deptno
  2  from   emp
  3  where  (empno  = '&a' or '&a' is null)
  4  and    (ename  = '&b' or '&b' is null)
  5  and    (job    = '&c' or '&c' is null)
  6  and    (deptno = '&d' or '&d' is null)
  7  /
old   3: where  (empno  = '&a' or '&a' is null)
new   3: where  (empno  = '7369' or '7369' is null)
old   4: and    (ename  = '&b' or '&b' is null)
new   4: and    (ename  = '' or '' is null)
old   5: and    (job    = '&c' or '&c' is null)
new   5: and    (job    = '' or '' is null)
old   6: and    (deptno = '&d' or '&d' is null)
new   6: and    (deptno = '' or '' is null)

     EMPNO ENAME      JOB           DEPTNO
---------- ---------- --------- ----------
      7369 SMITH      CLERK

1 row selected.

SCOTT@orcl_11gR2>



Re: substitution variable with value assigned and without value assigned [message #512061 is a reply to message #512051] Thu, 16 June 2011 14:29 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
empno = '&a'

empno is a number, this is a very bad condition.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512068 is a reply to message #512061] Thu, 16 June 2011 15:05 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Thu, 16 June 2011 15:29
empno is a number, this is a very bad condition.


Well, a tiny bit inefficient but not "very bad". All it causes is one implicit conversion.

SY.
Re: substitution variable with value assigned and without value assigned [message #512069 is a reply to message #512068] Thu, 16 June 2011 15:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Quote:
All it causes is one implicit conversion.

Which is for me a very bad way to program (in the addition to the invalidation of index usage, the conversion is on the column not on the constant value).

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512080 is a reply to message #512069] Thu, 16 June 2011 18:28 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
If there is a value for the parameter, then there is no problem without the quotes, but if there is no value, then it produces an error without the quotes, as shown below. This is the problem that I was trying to work around by enclosing the substitution variable within quotes.

SCOTT@orcl_11gR2> accept d prompt 'enter value for d:  '
enter value for d:  10
SCOTT@orcl_11gR2> select empno, ename, job, deptno
  2  from   emp
  3  where  (deptno = &d or &d is null)
  4  /
old   3: where  (deptno = &d or &d is null)
new   3: where  (deptno = 10 or 10 is null)

     EMPNO ENAME      JOB           DEPTNO
---------- ---------- --------- ----------
      7782 CLARK      MANAGER           10
      7839 KING       PRESIDENT         10
      7934 MILLER     CLERK             10

3 rows selected.

SCOTT@orcl_11gR2> accept d prompt 'enter value for d:  '
enter value for d:  
SCOTT@orcl_11gR2> select empno, ename, job, deptno
  2  from   emp
  3  where  (deptno = &d or &d is null)
  4  /
old   3: where  (deptno = &d or &d is null)
new   3: where  (deptno =  or  is null)
where  (deptno =  or  is null)
               *
ERROR at line 3:
ORA-00936: missing expression


SCOTT@orcl_11gR2>

Re: substitution variable with value assigned and without value assigned [message #512108 is a reply to message #512080] Fri, 17 June 2011 01:06 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I didn't say you should remove the quotes (I knew the error will come if there is none), I just say it is a bad thing to let an implicit conversion, maybe adding an explicit conversion should be better.

Anyway, with SQL*Plus it is not the best way to do it.
As I said, the best way depends on developer and user environments which we don't know.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512249 is a reply to message #512108] Fri, 17 June 2011 12:18 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
I suppose you could add to_number to avoid the implicit conversion:

accept a prompt 'enter value for a:  ' 
accept b prompt 'enter value for b:  '
accept c prompt 'enter value for c:  '
accept d prompt 'enter value for d:  '
select empno, ename, job, deptno
from   emp
where  (empno  = to_number ('&a') or '&a' is null)
and    (ename  = '&b' or '&b' is null)
and    (job    = '&c' or '&c' is null)
and    (deptno = to_number ('&d') or '&d' is null)
/

Re: substitution variable with value assigned and without value assigned [message #512250 is a reply to message #512249] Fri, 17 June 2011 12:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Yes, this is what I meant but there is a far better way to do the wanted feature in SQL*Plus.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512252 is a reply to message #512250] Fri, 17 June 2011 12:24 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9106
Registered: November 2002
Location: California, USA
Senior Member
Michel Cadot wrote on Fri, 17 June 2011 10:20

... there is a far better way to do the wanted feature in SQL*Plus.


Care to share?
Re: substitution variable with value assigned and without value assigned [message #512256 is a reply to message #512252] Fri, 17 June 2011 12:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Still waiting for an answer from oP.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512264 is a reply to message #512069] Fri, 17 June 2011 13:41 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Thu, 16 June 2011 16:09
in the addition to the invalidation of index usage, the conversion is on the column not on the constant value


Really??? Comparing number to string causes string conversion to number, not other way around, so empno = '&a' will use index:

SQL> set autotrace traceonly
SQL> select  empno
  2    from  emp
  3    where empno = '&a'
  4  /
Enter value for a: 1
old   3:   where empno = '&a'
new   3:   where empno = '1'

no rows selected


Execution Plan
----------------------------------------------------------
Plan hash value: 56244932

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |     4 |     0   (0)| 00:00:01 |
|*  1 |  INDEX UNIQUE SCAN| PK_EMP |     1 |     4 |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("EMPNO"=1)
SQL> 


And give optimizer some credit:

1 - access("EMPNO"=1)

SY.

[Updated on: Fri, 17 June 2011 13:42]

Report message to a moderator

Re: substitution variable with value assigned and without value assigned [message #512268 is a reply to message #512264] Fri, 17 June 2011 14:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
I should say: depending on version... as it does.
How do you explain the following:
SQL> set autotrace traceonly explain
SQL> select  empno
  2  from  emp
  3   where empno = to_char(&a)
  4  /
Enter value for a: 1

Execution Plan
----------------------------------------------------------
Plan hash value: 2088047273

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |    13 |     0   (0)| 00:00:01 |
|*  1 |  INDEX UNIQUE SCAN| EMP_PK |     1 |    13 |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("EMPNO"=1)

Where my TO_CHAR is gone?
And then:
SQL> /
Enter value for a: 'a'

Execution Plan
----------------------------------------------------------
Plan hash value: 2088047273

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |    13 |     0   (0)| 00:00:01 |
|*  1 |  INDEX UNIQUE SCAN| EMP_PK |     1 |    13 |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("EMPNO"=TO_NUMBER('a'))

When:
SQL> select  empno
  2  from  emp
  3  where empno = '&a'
  4  /
Enter value for a: a

Execution Plan
----------------------------------------------------------
Plan hash value: 2088047273

----------------------------------------------------------------------------
| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |     1 |    13 |     0   (0)| 00:00:01 |
|*  1 |  INDEX UNIQUE SCAN| EMP_PK |     1 |    13 |     0   (0)| 00:00:01 |
----------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("EMPNO"=TO_NUMBER('a'))

But:
SQL> set autotrace traceonly
SQL> /
Enter value for a: a
where empno = 'a'
              *
ERROR at line 3:
ORA-01722: invalid number

Funny, isn't it?
SQL*Plus is not a neutral tool to make this kind of test, it is smarter than we think.
This is why you must never rely on implicit behaviour.

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512277 is a reply to message #512268] Fri, 17 June 2011 15:14 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Fri, 17 June 2011 15:24
Funny, isn't it?
SQL*Plus is not a neutral tool to make this kind of test, it is smarter than we think.
This is why you must never rely on implicit behaviour.


Not sure what you mean. It has nothing to to with SQL*Plus or any tool for that matter. As I said, give optimizer some credit. Go to Toad, SQL*Navigator, SQL*Delevloper, etc. and issue:

explain plan for
select empno from emp where empno = '123';
select * from table(dbms_xplan.display);


and

explain plan for
select empno from emp where empno = 'abc';
select * from table(dbms_xplan.display);


This is SQL*Navigator output for 123:

1 	Plan hash value: 56244932                                                   
2 	                                                                            
3 	----------------------------------------------------------------------------
4 	| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
5 	----------------------------------------------------------------------------
6 	|   0 | SELECT STATEMENT  |        |     1 |     4 |     0   (0)| 00:00:01 |
7 	|*  1 |  INDEX UNIQUE SCAN| PK_EMP |     1 |     4 |     0   (0)| 00:00:01 |
8 	----------------------------------------------------------------------------
9 	                                                                            
10	Predicate Information (identified by operation id):                         
11	---------------------------------------------------                         
12	                                                                            
13	   1 - access("EMPNO"=123)                                                  


And for abc:

1 	Plan hash value: 56244932                                                   
2 	                                                                            
3 	----------------------------------------------------------------------------
4 	| Id  | Operation         | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
5 	----------------------------------------------------------------------------
6 	|   0 | SELECT STATEMENT  |        |     1 |     4 |     0   (0)| 00:00:01 |
7 	|*  1 |  INDEX UNIQUE SCAN| PK_EMP |     1 |     4 |     0   (0)| 00:00:01 |
8 	----------------------------------------------------------------------------
9 	                                                                            
10	Predicate Information (identified by operation id):                         
11	---------------------------------------------------                         
12	                                                                            
13	   1 - access("EMPNO"=TO_NUMBER('abc'))                                     


As you can see, it is optimizer, not tool who is making decision.

SY.
Re: substitution variable with value assigned and without value assigned [message #512279 is a reply to message #512277] Fri, 17 June 2011 15:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Another example:
SQL> select * from dual where dummy=1;
select * from dual where dummy=1
                         *
ERROR at line 1:
ORA-01722: invalid number

Regards
Michel
Re: substitution variable with value assigned and without value assigned [message #512344 is a reply to message #512252] Sun, 19 June 2011 09:10 Go to previous message
Michel Cadot
Messages: 68765
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Barbara Boehmer wrote on Fri, 17 June 2011 19:24
Michel Cadot wrote on Fri, 17 June 2011 10:20

... there is a far better way to do the wanted feature in SQL*Plus.


Care to share?


The answer is in one of my previous post:

Quote:
you build the query on the fly depending on the parameter given by the user


undef a
undef b
undef c
undef d
accept a number             prompt "Empno? "
accept b char   format a10  prompt "Name? "
accept c char   format a9   prompt "Job? "
accept d number             prompt "deptno? "
set termout off
col a new_value ca
col b new_value cb
col c new_value cc
col d new_value cd
select nvl2 (nullif(&a, 0), ' and empno = &a',     '') a,
       nvl2 ('&b',          ' and ename = ''&b''', '') b,
       nvl2 ('&c',          ' and job = ''&c''',   '') c,
       nvl2 (nullif(&d, 0), ' and deptno = &d',    '') d
from dual
/
set termout on
select empno, ename, job, deptno
from emp
where 1 = 1 &ca &cb &cc &cd
/


SQL> @c:\t
Empno? 7369
Name? 
Job? 
deptno? 
     EMPNO ENAME      JOB           DEPTNO
---------- ---------- --------- ----------
      7369 SMITH      CLERK             20

1 row selected.

SQL> l
  1  select empno, ename, job, deptno
  2  from emp
  3* where 1 = 1 &ca &cb &cc &cd
SQL> def
DEFINE _DATE           = "19/06/2011 16:06:22" (CHAR)
DEFINE _USER           = "MICHEL" (CHAR)
DEFINE _PRIVILEGE      = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1002000400" (CHAR)
DEFINE _EDITOR         = "Notepad" (CHAR)
DEFINE _O_VERSION      = "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining
and Real Application Testing options" (CHAR)
DEFINE _O_RELEASE      = "1002000400" (CHAR)
DEFINE A               =       7369 (NUMBER)
DEFINE B               = "" (CHAR)
DEFINE C               = "" (CHAR)
DEFINE D               =          0 (NUMBER)
DEFINE CA              = " and empno =       7369" (CHAR)
DEFINE CB              = "" (CHAR)
DEFINE CC              = "" (CHAR)
DEFINE CD              = "" (CHAR)


Regards
Michel

Previous Topic: decode function?
Next Topic: select on MV
Goto Forum:
  


Current Time: Wed Aug 20 07:23:12 CDT 2025