0

In Oracle form I have a query just like

if po_no is not null then
    str := str ||'and po_no = '||:block.po_no;
end if;

now I want to append the str in below query.

select po_no
   from po,
        po_det
   where po_id = p_det_id
      &str

I want to append the str in end of query but it gives ORA-01722 error ..how i can do in right way..

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
khan
  • 3
  • 3
  • Does this answer your question? [Oracle SQL - How to build where clause with optional search parameters](https://stackoverflow.com/questions/30454886/oracle-sql-how-to-build-where-clause-with-optional-search-parameters) – astentx Jun 24 '23 at 20:01

1 Answers1

0

Concatenate str to the rest of the query.

Pay attention to leading space (in front of the AND):

if po_no is not null then
   str := str ||' and po_no = ' || :block.po_no;
end if;

select po_no
from po, po_det
where po_id = p_det_id || str;

You didn't explain where exactly you'll be using that dynamic WHERE clause; if it is to filter rows fetched in a data block, consider using SET_BLOCK_PROPERTY built-in and its ONETIME_WHERE (or DEFAULT_WHERE) property (read more about it in Oracle Online Help system).


[EDIT]

I'm still not sure what is what in this query, but - to me - it looks as if you don't need IF at all, but use query which looks like this:

select po_no
  into v_po_no
  from po, po_det
  where po_id = p_det_id
    and (po_no = :block.po_no or po_no is null);     --> this substitutes your IF

Once again: to me, it is unclear what is exactly po_no (column? block field? variable?), but that's more or less what you might need to do.


[EDIT #2]

Dynamic SQL:

declare
  str    varchar2(500);
  result number;
begin
  str := 'select po_no from po, po_det where po_id = p_det_id ' ||
         case when po_no is not null then ' and po_no = :a'
         end;
         
  execute immediate str into result using :block.po_no;
end;
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
  • i am using it in when-button-press trigger and want to append the criteria which is build in Str variable and at then end append this Str variable to query just like this mention in below: ``select po_no into v_po_no from po, po_det where po_id = p_det_id || str; `` my question how to concat the str in existing query at the end of where clause. – khan Jun 24 '23 at 12:15
  • OK; and what is PO_NO you use in IF? This: IF PO_NO IS NOT NULL >> what is that PO_NO? Could you also edit original question you posted and post the whole trigger code? It would be simpler for us to assist if we knew what is what. – Littlefoot Jun 24 '23 at 16:34
  • Sir po_no comes from text item and any string value come from lov..like po_no='po-123'..actual problem I am facing is to cancat str with where claus at the end and str just like we do this in Oracle report like &str – khan Jun 24 '23 at 16:57
  • Have a look at edited answer, please. – Littlefoot Jun 24 '23 at 17:02
  • select po_no into v_po_no from po, po_det where po_id = p_det_id and (po_no = :block.po_no or po_no is null) why we can not dynamic it? Means why not we put in variable like str =' and po_no='||:block.text_field; and the put str in where clause at the end – khan Jun 24 '23 at 17:17
  • Because that's not how dynamic SQL works in Oracle. You could compose a SELECT statement and execute it using EXECUTE IMMEDIATE. Though, pay attention to what this query returns - must be a single value because - if it doesn't return anything, Oracle will raise NO_DATA_FOUND; if it returns more than a single value, it'll return TOO_MANY_ROWS. – Littlefoot Jun 24 '23 at 17:20
  • Put this part of yours reply in a variable and the append the variable to existing query forgot the if condition. Str:= and (po_no = :block.po_no or po_no is null) – khan Jun 24 '23 at 17:25
  • Sorry will be count(po_no) no too many rows return only single value returned it's gurunteed – khan Jun 24 '23 at 17:27
  • Can you please write how the above scanario can be build in EXECUTE IMMEDIATE statement – khan Jun 24 '23 at 17:29
  • But it's work in Oracle report we build many criteria in string form then send string to report and put at last of query with &str – khan Jun 24 '23 at 17:32
  • Edited, with dynamic SQL. As of the fact that it works in Oracle Reports: yes, it does, but that's a **different tool** with different rules. – Littlefoot Jun 24 '23 at 17:33
  • Agreed can you forward what and how the execute immediate built for such scanario – khan Jun 24 '23 at 17:36
  • Dynamic SQL is usually used when you have to include different table or column names in the same query. In this (your) case, I'd use code I posted in my 1st EDIT. – Littlefoot Jun 24 '23 at 17:39
  • Ok thanks gotted...I appreciate yours support – khan Jun 24 '23 at 17:51
  • If I used set_block_property and default_where..and execute the block it give more row while I required to count the result in this scanario how I can count(po_no) – khan Jun 24 '23 at 18:06
  • You certainly wouldn't use SET_BLOCK_PROPERTY for that purpose, it just doesn't make any sense. – Littlefoot Jun 24 '23 at 18:22