Home » SQL & PL/SQL » SQL & PL/SQL » Assign the SQL statement to a variable (Oracle 9.1)
icon4.gif  Assign the SQL statement to a variable [message #633094] Tue, 10 February 2015 22:13 Go to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
Hello ,

I want the following query to be assigned to a variable so that I can use execute immedate in running the sql statement .


SELECT DMOG.hic_num AS HIC_NUM, 
       DMOG.mem_num AS MEM_NUM, 
       ELIG.bg_dt   AS BG_DT, 
       ELIG.end_dt  AS END_DT 
FROM   bi_ika_mem.ma_ika_mem_dmog DMOG, 
       bi_ika_mem.ma_ika_mem_elig ELIG, 
       bi_edw_hist.lab_pat_hsty PAT, 
       bi_edw_hist.lab_req_hsty LRH 
WHERE  LRH.serv_req_dt_key BETWEEN 20140101 AND 20140131 
       AND DMOG.src_sys_cd = 'MA' 
       AND ELIG.src_sys_cd = 'MA' 
       AND ELIG.mem_num = DMOG.mem_num 
       AND DMOG.mem_num = PAT.ctrc_num 
       AND LRH.pat_sk = PAT.pat_sk 
       AND PAT.edw_end_dt_key = 99991231 
       AND LRH.serv_req_dt_key >= To_number(To_char(DMOG.bg_dt, 'YYYYMMDD')) 
       AND LRH.serv_req_dt_key <= To_number(To_char(DMOG.end_dt, 'YYYYMMDD')); 


*BlackSawn added {codetags}. Please do so yourself in the future.

[Updated on: Tue, 10 February 2015 22:23] by Moderator

Report message to a moderator

Re: Assign the sql to a variable [message #633097 is a reply to message #633094] Tue, 10 February 2015 22:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and read http://www.orafaq.com/forum/t/174502/


how many rows are returned by posted SELECT statement?
Re: Assign the SQL statement to a variable [message #633104 is a reply to message #633094] Wed, 11 February 2015 00:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
I want the following query to be assigned to a variable


var := '<query>';

Quote:
so that I can use execute immedate in running the sql statement .


Why do you want to use execute immediate to execute a static query?

Re: Assign the SQL statement to a variable [message #633125 is a reply to message #633104] Wed, 11 February 2015 06:00 Go to previous messageGo to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
The where clause between parameters ..are dynamic - I just happened to give an example

WHERE LRH.serv_req_dt_key BETWEEN 20140101 AND 20140131
Re: Assign the SQL statement to a variable [message #633126 is a reply to message #633125] Wed, 11 February 2015 06:22 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

You can declare a cursor of the select statment and use the parameter in where clause.
create or replace procedure p(p_from_dt in date,p_to_dt in date)is
CURSOR c1 IS 
SELECT * FROM emp WHERE hiredate BETWEEN p_from_dt AND p_to_dt;
BEGIN
FOR i IN c1 loop
dbms_output.put_line(i.sal);
END loop;
end;

[Updated on: Wed, 11 February 2015 06:24]

Report message to a moderator

Re: Assign the SQL statement to a variable [message #633128 is a reply to message #633125] Wed, 11 February 2015 06:33 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
mikelala wrote on Wed, 11 February 2015 17:30
The where clause between parameters ..are dynamic - I just happened to give an example

WHERE LRH.serv_req_dt_key BETWEEN 20140101 AND 20140131


You need to explain in detail what exactly you are trying to do. Why not use bind variables?

And, why is LRH.serv_req_dt_key a NUMBER and not a DATE data type?
Re: Assign the SQL statement to a variable [message #633131 is a reply to message #633125] Wed, 11 February 2015 07:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

mikelala wrote on Wed, 11 February 2015 13:00
The where clause between parameters ..are dynamic - I just happened to give an example

WHERE LRH.serv_req_dt_key BETWEEN 20140101 AND 20140131


What is dynamic? The values or the column?
If this is the values then it is not a dynamic query but a static one and you have to use variables in place of the values.
What is your programming language?
What is the application which will send the query to the database?

[Updated on: Wed, 11 February 2015 07:24]

Report message to a moderator

Re: Assign the SQL statement to a variable [message #633135 is a reply to message #633131] Wed, 11 February 2015 07:57 Go to previous messageGo to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
The values are dynamic which comes as parameters to a procedure that I am going to include in the said sql statement.
Re: Assign the SQL statement to a variable [message #633136 is a reply to message #633128] Wed, 11 February 2015 08:00 Go to previous messageGo to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
The column field in table is number so I had to convert to number datatype and valuate .

My question is how I will assign the sql statement to a variable .
Re: Assign the SQL statement to a variable [message #633137 is a reply to message #633135] Wed, 11 February 2015 08:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
mikelala wrote on Wed, 11 February 2015 05:57
The values are dynamic which comes as parameters to a procedure that I am going to include in the said sql statement.


Lalit Kumar B wrote on Wed, 11 February 2015 04:33

Why not use bind variables?
Re: Assign the SQL statement to a variable [message #633138 is a reply to message #633094] Wed, 11 February 2015 08:18 Go to previous messageGo to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
The values are the variables coming as a procedure parameters .
Re: Assign the SQL statement to a variable [message #633139 is a reply to message #633135] Wed, 11 February 2015 08:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

mikelala wrote on Wed, 11 February 2015 14:57
The values are dynamic which comes as parameters to a procedure that I am going to include in the said sql statement.


So the statement is static and you must NOT use execute immediate.

You didn't answer my questions:

Quote:
What is your programming language?
What is the application which will send the query to the database?

[Updated on: Wed, 11 February 2015 08:19]

Report message to a moderator

Re: Assign the SQL statement to a variable [message #633140 is a reply to message #633138] Wed, 11 February 2015 08:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
mikelala wrote on Wed, 11 February 2015 06:18
The values are the variables coming as a procedure parameters .


which is EXACTLY what bind variables are for!
Try Reading The Fine Manual for a change.

https://docs.oracle.com/apps/search/search.jsp?category=database&product=e50529-01&q=bind+variable
Re: Assign the SQL statement to a variable [message #633141 is a reply to message #633126] Wed, 11 February 2015 09:02 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
sss111ind wrote on Wed, 11 February 2015 17:52
You can declare a cursor of the select statment and use the parameter in where clause.
create or replace procedure p(p_from_dt in date,p_to_dt in date)is
CURSOR c1 IS 
SELECT * FROM emp WHERE hiredate BETWEEN p_from_dt AND p_to_dt;
BEGIN
FOR i IN c1 loop
dbms_output.put_line(i.sal);
END loop;
end;


You could do a task in many ways, and in programming language, such as PL/SQL in your post, it won't be efficient.

In my opinion, try to do it in plain SQL, else do it in PL/SQL, but keep in mind to do it efficiently. Please correct me if I am wrong.

Just as a comment(out of so many other options), why not use a CURSOR FOR loop, or why not do it in BULK? Ultimately, in OP's case, I would go with bind variables.
Re: Assign the SQL statement to a variable [message #633165 is a reply to message #633141] Wed, 11 February 2015 18:46 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.akadia.com/services/ora_bind_variables.html
Re: Assign the SQL statement to a variable [message #633170 is a reply to message #633165] Thu, 12 February 2015 00:18 Go to previous messageGo to next message
mamalik
Messages: 268
Registered: November 2008
Location: Pakistan
Senior Member

Very informatic link provided by Black Swan.

https://docs.oracle.com/apps/search/search.jsp?category=database&product=e50529-01&q=bind+variable
Re: Assign the SQL statement to a variable [message #633246 is a reply to message #633170] Thu, 12 February 2015 22:47 Go to previous messageGo to next message
mikelala
Messages: 6
Registered: February 2015
Junior Member
Thanks for all for taking time and replying . The issue is resolved .
Re: Assign the SQL statement to a variable [message #633247 is a reply to message #633246] Thu, 12 February 2015 22:55 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
post working code solution
Previous Topic: Need help on using regexp_substr in SQL
Next Topic: Split row into two or more rows
Goto Forum:
  


Current Time: Fri Apr 26 08:12:36 CDT 2024