Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have written one oracle stored procedure.The procedure is compiled sucessfuly.

But when i am executing that stored procedure with paramater value it is giving error in query.
below is my stored procedure query----
SQL
CREATE OR REPLACE PROCEDURE test123(p_Le in varchar2,p_CHK in varchar2,p_Ty in varchar2,p_Po in varchar2,
p_Cl in varchar2,p_CA in varchar2,p_From_Date in date,p_To_Date in date,p_Number in varchar2,
p_recordset OUT SYS_REFCURSOR) as

sqlquery  varchar2(30000);

BEGIN

IF  p_Le = 'SUB' THEN

     IF  p_CHK ='TRUE' THEN

       sqlquery:= ( ' select type, sub_type, amount from (select rank() over(order by count(distinct(id)) desc) r,');

    ELSIF p_CHK='FALSE' THEN

        sqlquery:=('select');

     END IF;
         IF  p_Po='ALL' THEN

             IF   p_Cl = 'ALL' THEN

                   sqlquery:= sqlquery || 't.type, t.sub_type, count(distinct(id)) as amount from logged l, type t where t.category = '''|| p_CA ||''' and l.ftr_type_id = t.type_id and trunc(date) >=  '''|| p_From_Date ||'''  and trunc(date) <=   '''|| p_To_Date ||''' and t.type = '''|| p_Ty ||''' and substr(l.msisdn,1,4) in ('|| p_Number ||') and t.type <> ''Call'' group by t.type,t.sub_type order by t.type, t.sub_type';

OPEN p_recordset FOR sqlquery;

             END IF;

        END IF;

END IF;

END test123;
/

Please help
Posted
Updated 12-Nov-14 22:19pm
v4
Comments
DamithSL 13-Nov-14 0:10am    
what is the error?
Pro86 13-Nov-14 0:31am    
it is giving syntex error on line no 25
Pro86 13-Nov-14 1:11am    
Is there any syntax error near 'CALL' word.

How to write value inside single quote in dynamic query

BETWEEN TO_DATE('''||parm1||''',''dd/MM/yy'') this is just an example of using parametr inside dynamic query....
 
Share this answer
 
Just a guess, but could this be it?

In the line:
select type, sub_type, amount from (select rank() over(order by count(distinct(ftr_id)) desc) r,');

Type is a keyword. You should qualify type column with the table or surround it with quotes ("TYPE")
See the explanation here.[^]

It compiles because sqlquery is correct string, but when called to open a cursor it fails. You should output your sqlquery to console and then check it separately. This is good practice for all dynamic sql queries.

If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
Pro86 13-Nov-14 4:15am    
Thanks for reply,

but above solution is not working for me.

Can u give me step for to get string query from dynamic query?
Sinisa Hajnal 13-Nov-14 8:39am    
If it is problem of apostrophes, you have to escape them by typing two in a row and if they are immediately before pipe symbol, you'll have three as Kan07 writes below.

As I mentioned: use DBMS_OUTPUT.PUT_LINE(sqlquery) before opening the cursor and see what is your query.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900