OPEN CURSOR using a WITH clause in the select query
Hi,
I am using Oracle 9i. I have a requirement where I have a REFCURSOR as an OUT parameter for my procedure. I have declared the TYPE and created the procedure.
In the procedure, I am using OPEN <cursor_name> FOR <query>;
Ideally this works in most of the cases that I have tried earlier. However, in the current case I am using a WITH clause in my query to get the results.
I need help in understanding if the above mentioned syntax would not allow me to use the WITH clause in the query.
What error do you get , seems to work ok for me on 10g
SQL> begin
2 open :cv for 'with x as (select * from emp) select * from x';
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> print :cv
EMPNO
ENAME
JOB MGR HIREDATE SAL COMM DEPTNO
7521
WARD
SALESMAN 7698 22-FEB-81 1250 500 30
7566
JONES
MANAGER 7839 02-APR-81 2975 20
EMPNO
Similar Messages
-
Using case when statement in the select query to create physical table
Hello,
I have a requirement where in I have to execute a case when statement with a session variable while creating a physical table using a select query. let me explain with an example.
I have a physical table based on a select table with one column.
SELECT 'VALUEOF(NQ_SESSION.NAME_PARAMETER)' AS NAME_PARAMETER FROM DUAL. Let me call this table as the NAME_PARAMETER table.
I also have a customer table.
In my dashboard that has two pages, Page 1 contains a table with the customer table with column navigation to my second dashboard page.
In my second dashboard page I created a dashboard report based on NAME_PARAMETER table and a prompt based on customer table that sets the NAME_ PARAMETER request variable.
EXECUTION
When i click on a particular customer, the prompt sets the variable NAME_PARAMETER and the NAME_PARAMETER table shows the appropriate customer.
everything works as expected. YE!!
Now i created another table called NAME_PARAMETER1 with a little modification to the earlier table. the query is as follows.
SELECT CASE WHEN 'VALUEOF(NQ_SESSION.NAME_PARAMETER)'='Customer 1' THEN 'TEST_MART1' ELSE TEST_MART2' END AS NAME_PARAMETER
FROM DUAL
Now I pull in this table into the second dashboard page along with the NAME_PARAMETER table report.
surprisingly, NAME_PARAMETER table report executes as is, but the other report based on the NAME_PARAMETER1 table fails with the following error.
Error Codes: OPR4ONWY:U9IM8TAC:OI2DL65P
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 16001] ODBC error state: S1000 code: 1756 message: [Oracle][ODBC][Ora]ORA-01756: quoted string not properly terminated. [nQSError: 16014] SQL statement preparation failed. (HY000)
SQL Issued: SET VARIABLE NAME_PARAMETER='Novartis';SELECT NAME_PARAMETER.NAME_PARAMETER saw_0 FROM POC_ONE_DOT_TWO ORDER BY saw_0
If anyone has any explanation to this error and how we can achieve the same, please help.
Thanks.Hello,
Updates :) sorry.. the error was a stupid one.. I resolved and I got stuck at my next step.
I am creating a physical table using a select query. But I am trying to obtain the name of the table dynamically.
Here is what I am trying to do. the select query of the physical table is as follows.
SELECT CUSTOMER_ID AS CUSTOMER_ID, CUSTOMER_NAME AS CUSTOMER_NAME FROM 'VALUEOF(NQ_SESSION.SCHEMA_NAME)'.CUSTOMER.
The idea behind this is to obtain the data from the same table from different schemas dynamically based on what a session variable. Please let me know if there is a way to achieve this, if not please let me know if this can be achieved in any other method in OBIEE.
Thanks. -
Getting NULL with function in the select Query
Hi all,
SELECT
,a.TRANSACTIONAL_CURR_CODE
--,gl.CONVERSION_RATE
,get_rate(a.transactional_curr_code,NVL(gsob.attribute1,gsob.currency_code),a.conversion_type_code,TRUNC(a.ordered_date)) CONVERSION_RATE
FROM
oe_order_headers_all a,
oe_order_lines_all b,
gl_sets_of_books gsob
--gl_daily_rates gl
WHERE 1=1
and a.header_id = b.header_id
--and from_currency = 'USD' --a.transactional_curr_code
--and conversion_type = 'Corporate' --a.CONVERSION_TYPE_CODE
--and conversion_date = '23-APR-12' --a.ORDERED_DATE
and a.header_id =1234;
With commented above Line
0.749400479616307
NULL ---->Why i am getting NULL , i have a Value (0.07809)
18.521
Without comment above Code(Hardcoded Code or Commented Line)
18.521
0.749400479616307
0.07809
select transactional_curr_code,ORDERED_DATE from oe_order_headers_all
where header_id=1234
o/p:-
USD '23-APR-12'
select CONVERSION_RATE from gl_daily_rates
where from_currency = 'USD' --transactional_curr_code
and conversion_type = 'Corporate' --CONVERSION_TYPE_CODE
and conversion_date = '23-APR-12' --ORDERED_DATE
--and to_currency = 'USD'
CONVERSION_RATE
18.521
0.749400479616307
0.07809
select gsob.attribute1,gsob.currency_code from gl_sets_of_books
where SET_OF_BOOKS_ID=1
gsob.attribute1 gsob.currency_code
USD USDHi ,
Same as below code in the Function
select CONVERSION_RATE from gl_daily_rates
where from_currency = 'USD' --transactional_curr_code
and conversion_type = 'Corporate' --CONVERSION_TYPE_CODE
and conversion_date = '23-APR-12' --ORDERED_DATE
and to_currency = 'USD' -
Using Left with Charindex in a Select Query
Hi
I hope someone can help me. I have a field that has text in it for example N00342-01/28 and I am trying to get it to only show N00342-01. This could in the future be N132245-01/25 for example or N00342-132/28. What I am trying to show is
anything before the "/".
The code I have created is this:
SELECT S.ItemNumber, S.DeliveryWeek, S.Pack_Centre, S.Schedule_Qty, S.Delivered_Qty, S.PartPallet, I.Production_Reference, LEFT('I.Production_Reference',
CHARINDEX('/', 'I.Production_Reference') - 1) AS IntouchRef, I.Pallet_Qty, I.Tool_Number, S.Firm
FROM dbo.Scheduled_Items AS S INNER JOIN
dbo.Item_Details AS I ON S.Pack_Centre = I.Pack_Centre AND S.ItemNumber = I.Item_Number
When I run it I get this error:
Does anyone know what I am doing wrong?
Thanks
AaronSELECT S.ItemNumber, S.DeliveryWeek, S.Pack_Centre, S.Schedule_Qty, S.Delivered_Qty, S.PartPallet, I.Production_Reference, LEFT('I.Production_Reference',
CHARINDEX('/', 'I.Production_Reference') - 1) AS IntouchRef,
Hello Aaron,
Remove the single Hyphen around I.Production Reference, this it's treaten as a fix text, not as a column.
Just to test it:
DECLARE @test AS TABLE(MyValue varchar(30))
INSERT @test VALUES ('N132245-01/25')
INSERT @test VALUES ('N003424-132/28')
SELECT LEFT(MyValue, CHARINDEX('/', MyValue) - 1)
FROM @test
Olaf Helper
[ Blog] [ Xing] [ MVP] -
With clause within the cursor ???
Hello gurus,
Can we have a with clause within the cursor ???
something like this ..
cursor test_cur is
WITH unpivoted_data AS
SELECT DISTINCT
f.cust_id
f.office_cd
f.type_cd
FROM refoff ro,
cutomer p,
office f
WHERE f.cust_id = p.cust_id
AND f.office_cd = ro.office_cd
AND f.type_cd IN ('ACCT', 'pay')
SELECT cust_id
, MAX (CASE WHEN type_cd = 'ACCT' THEN office_id END) AS acct_office_id
, MAX (CASE WHEN type_cd = 'pay' THEN office_id END) AS pay_office_id
FROM unpivoted_data
GROUP BY cust_id
test_cur_rec test_cur% rowtype;
Begin
for test_cur_rec in test_cur
loop
insert into test1 (.....) values (.......);
end loop;
end;
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....???
Thank you so much!!!Hi,
user642297 wrote:
Hello gurus,
Can we have a with clause within the cursor ???Sure, but don't take my word for it. Try it and see!
If you get an error, post the error message.
If you get unexpected results, explain.
Whenever you have a problem, post your full Oracle version number.
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....??? Type these 6 characters:
(all small letters, inside curly brackets) before and after formatted sections. -
Using Sansung S3, Android 4.1.2, Firefox 24.0; not yet possible to share all the open tabs (using NFC) with another NFC phone. But only the one main tab I am currently browsing is possible to share. Is this the feature of current NFC feature of Firefox?
If so, when will there be feature to share all the tabs?
Thanks.I'm not sure if we will implement this. Sharing all the tabs you have open have many more security and privacy issues. Someone forgets that they had a tab open they did not want to share or miss-clicks on the share all option.
We are looking into better ways to share tabs with devices that are nearby. Especially if they are on the same WiFi network. -
Hi,
I am using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 .
I am working in a procedure where I want to manipulate the data produced by a WITH AS clause. But it is giving "PL/SQL: ORA-00942: table or view does not exist" error. A small example to illustrate my problem is given below. Here in the example am just trying to get a replaced string by using the out put of a WITH AS clause.
SET SERVEROUTPUT ON;
DECLARE
CNT INTEGER;
LETTER CHAR(1);
REPLACED_STRING VARCHAR2(10);
BEGIN
REPLACED_STRING := 'ABC';
WITH T AS
(SELECT 'ABC' VAL FROM DUAL),
TMP (LEN, SUBVAL) AS
(SELECT LENGTH(VAL) LEN,
SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
FROM T
UNION ALL
SELECT LENGTH(VAL)-1 LEN,
SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
FROM T
SELECT COUNT(*) INTO CNT FROM TMP,T ;
FOR I IN 1..CNT LOOP
SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM TMP,T;
SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
END LOOP;
END;
I thought of declaring a cursor which will hold the data produced by WITH clause but it did not work. Can you please let me know what are the possible options to do this.
ThanksI think, I can not use the WITH inside loop as, I want to manipulate on data resulted by WITH reading row by row from it. I have a complex procedure and I wont be able to explain that scenario here and thus i came up with a similar problem through a very simple example. I will try to explain my problem thru my example here.
The WITH in my example give an out put as :
SUBVAL
AB
BC
WITH T AS
(SELECT 'ABC' VAL FROM DUAL),
TMP (LEN, SUBVAL) AS
(SELECT LENGTH(VAL) LEN,
SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
FROM T
UNION ALL
SELECT LENGTH(VAL)-1 LEN,
SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
FROM T
SELECT subval FROM TMP,T;
and then by using this in the PLSQL block mentioned above
FOR I IN 1..CNT LOOP
SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM TMP,T;
SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
END LOOP;
I want to have an output like:
XBC
XXC
Please note that nature of my original problem is that I want to manipulate the data given by WITH clause in the same procedure by passing it to other functions/procs to get the desired result. So please advice me following this approach only which would be helpful.
Thanks -
ORA-32034: unsupported use of WITH clause-issue
hello all,
i am facing some issue when i use with clause and union all operator.
i have created a dummy code to solve this problem..
my code is ----------
with dept_1 as
(select deptno d1 from detp9 where deptno = 20)
select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
UNION ALL
with dept_1 as
(select deptno d2 from detp9 where deptno = 30)
select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
when i ran this i gort a message-
ORA-32034: unsupported use of WITH clause.
please help me to solve this iisue..
when i ran it separatly without using union/union all it ran sucessfully..
thanks in advance..923315 wrote:
hello all,
i am facing some issue when i use with clause and union all operator.
i have created a dummy code to solve this problem..
my code is ----------
with dept_1 as
(select deptno d1 from detp9 where deptno = 20)
select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
UNION ALL
with dept_1 as
(select deptno d2 from detp9 where deptno = 30)
select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
when i ran this i gort a message-
ORA-32034: unsupported use of WITH clause.
please help me to solve this iisue..
when i ran it separatly without using union/union all it ran sucessfully..
thanks in advance..Well, i don't see anything about these queries that makes sense.
You are essentially joining emp and dept on EMPNO to DEPTNO ... that doesn't usually make any sense.
How about you step back from the query which is almost certainly incorrect, and explain your tables, their data and what you need as output?
Cheers, -
How can i use index in select query.. facing problem with the select query.
Hi Friends,
I am facing a serious problem in one of the select query. It is taking a lot of time to fetch data in Production Scenario.
Here is the query:
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelat LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelatrprctr
WHERE rldnr = c_telstra_accounting
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
And rzzlstar in r_lstar
AND rpmax = c_max_period.
There are 5 indices present for Table ZTFTELAT.
Indices of ZTFTELAT:
Name Description
0 Primary key( RCLNT,RLDNR,RRCTY,RVERS,RYEAR,ROBJNR,SOBJNR,RTCUR,RUNIT,DRCRK,RPMAX)
005 Profit (RCLNT,RPRCTR)
1 Ledger, company code, account (RLDNR,RBUKRS, RACCT)
2 Ledger, company code, cost center (RLDNR, RBUKRS,RCNTR)
3 Account, cost center (RACCT,RCNTR)
4 RCLNT/RLDNR/RRCTY/RVERS/RYEAR/RZZAUFNR
Z01 Activity Type, Account (RZZLSTAR,RACCT)
Z02 RYEAR-RBUKRS- RZZZBER-RLDNR
Can anyone help me out why it is taking so much time and how we can reduce it ? and also tell me if I want to use index number 1 then how can I use?
Thanks in advance.Hi Shiva,
I am using two more select queries with the same manner ....
here are the other two select query :
***************1************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelpt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelptrprctr
WHERE rldnr = c_telstra_projects
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
and the second one is
*************************2************************
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE tbl_summary
FROM ztftelnt LEFT JOIN ztfzberep
ON ztfzberep~gjahr = st_input-gjahr
AND ztfzberep~poper = st_input-poper
AND ztfzberepcntr = ztftelntrprctr
WHERE rldnr = c_telstra_networks
AND rrcty = c_actual
AND rvers = c_ver_001
AND rbukrs = st_input-bukrs
AND racct = st_input-saknr
AND ryear = st_input-gjahr
and rzzlstar in r_lstar
AND rpmax = c_max_period.
for both the above table program is taking very less time .... although both the table used in above queries have similar amount of data. And i can not remove the APPENDING CORRESPONDING. because i have to append the data after fetching from the tables. if i will not use it will delete all the data fetched earlier.
Thanks on advanced......
Sourabh -
My MBP is Early 2011 but dunno why cant use mirroring with apple TV, the icon cannot be found. So cannot use mirroring.
I have follow trouble shooting , they recommend to go system preference -> display , thus can find out the mirroring icon if no mean it doesnt support. But according to the web, Early 2011 MBP should be able to use, dunno why.Just realise I havent updated my OS to mountain lion and this is the reason why I cannot use mirroring before ^^
Thanks all -
Any Problems using SSL with Safari and the move with Internet explorer to require only TLS encryption.
Hi .
Apple no longer supports Safari for Windows if that's what you are asking > Apple apparently kills Windows PC support in Safari 6.0
Microsoft has not written IE for Safari for many years. -
is there a procedure to open keynote 09 files with videos in the new Keynote without converting videos?
I have a big presentation with many video mov and avi files created with Keynote 09. When trying to open the file in the new Keynote the procedured crashes and Keynote crashes abruptly. It seens that an avi file is not converted. I have no more the old Keynote 09. How can this problem be solved?if you right click or control click on the file, do you get an option to 'show package contents'?
-
I cannot open files in excel with error message "The file name or path contains invalid characters
I cannot open files in excel with error message "The file name or path contains invalid characters
Found the solution. The hardrive name was changed to "/" which is not a recognised file path character. So I changed the name to "Mac" then I was able to already open all files in excel and word
-
s there a way that when using airplay with power point the ATV does not sleep/screen saver frequently???
I have done this for several Titles, using Photoshop and a Layer Mask, that gets "erased," revealing the letters. My Titles were of "handwriting with chalk" on a blackboard, so I could keep the edge of that Layer Mask, looking more like the "chalk." Photoshop also makes it easy to output Layer Comps for each "step" in the handwriting process. IIRC, I did about 5 Frames per Layer Comp on Import, but do not recall if I used an extremely short Cross-Dissolve Transition between those - I intened to do so, but just do not remember if I liked that, or went with just the Still Images? Will try to find that animation, and maybe create a full tutorial of the process. In my case, I also added an SFX of chalk on a chalkboard.
If you have Photoshop, it's really quite easy to pull off.
Now, I do not know if Photoshop Elements has Layer Masks yet, which make it so very easy to accomplish. One could do it backward, where the Text Layer gets Erased, and then one would have their full Image as the last in the sequence, and then the next would have a little bit of the Text removed, then a bit more, then a bit more - going backward, until the "writing surface" is clean. One could also do this with a Transparent Background, so that one saw the Video below, and no actual "writing surface" visible.
Good luck,
Hunt -
How to optimize the select query that is executed in a cursor for loop?
Hi Friends,
I have executed the code below and clocked the times for every line of the code using DBMS_PROFILER.
CREATE OR REPLACE PROCEDURE TEST
AS
p_file_id NUMBER := 151;
v_shipper_ind ah_item.shipper_ind%TYPE;
v_sales_reserve_ind ah_item.special_sales_reserve_ind%TYPE;
v_location_indicator ah_item.exe_location_ind%TYPE;
CURSOR activity_c
IS
SELECT *
FROM ah_activity_internal
WHERE status_id = 30
AND file_id = p_file_id;
BEGIN
DBMS_PROFILER.start_profiler ('TEST');
FOR rec IN activity_c
LOOP
SELECT DISTINCT shipper_ind, special_sales_reserve_ind, exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item --464000 rows in this table
WHERE item_id_edw IN (
SELECT item_id_edw
FROM ah_item_xref --700000 rows in this table
WHERE item_code_cust = rec.item_code_cust
AND facility_num IN (
SELECT facility_code
FROM ah_chain_div_facility --17 rows in this table
WHERE chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND div_id = (SELECT div_id
FROM ah_div --8 rows in this table
WHERE division = rec.division)));
END LOOP;
DBMS_PROFILER.stop_profiler;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
WHEN TOO_MANY_ROWS
THEN
NULL;
END TEST;The SELECT query inside the cursor FOR LOOP took 773 seconds.
I have tried using BULK COLLECT instead of cursor for loop but it did not help.
When I took out the select query separately and executed with a sample value then it gave the results in a flash of second.
All the tables have primary key indexes.
Any ideas what can be done to make this code perform better?
Thanks,
Raj.As suggested I'd try merging the queries into a single SQL. You could also rewrite your IN clauses as JOINs and see if that helps, e.g.
SELECT DISTINCT ai.shipper_ind, ai.special_sales_reserve_ind, ai.exe_location_ind
INTO v_shipper_ind, v_sales_reserve_ind, v_location_indicator
FROM ah_item ai, ah_item_xref aix, ah_chain_div_facility acdf, ah_div ad
WHERE ai.item_id_edw = aix.item_id_edw
AND aix.item_code_cust = rec.item_code_cust
AND aix.facility_num = acdf.facility_code
AND acdf.chain_id = ah_internal_data_pkg.get_chain_id (p_file_id)
AND acdf.div_id = ad.div_id
AND ad.division = rec.division;ALSO: You are calling ah_internal_data_pkg.get_chain_id (p_file_id) every time. Why not do it outside the loop and just use a variable in the inner query? That will prevent context switching and improve speed.
Edited by: Dave Hemming on Dec 3, 2008 9:34 AM
Maybe you are looking for
-
This is the message I get: iTunes quit unexpectantly. Process: iTunes [153] Path: /Applications/iTunes.app/Contents/MacOS/iTunes Identifier: com.apple.iTunes Version: 10.7 (10.7) Build Info: iTunes-10702101~1 Code
-
How do I add a new site to my Favorites tab?
I need to add a new site to my favorites tab.
-
Error while loading data from PSA to DSO using DTP
Hi, I have a Unique aplha numeric identifier of type "Char" length "32" . When I am loading the data from PSA to DSO using DTP I get the following error message: "An error occurred while executing a transformation rule: The exact error message is Ov
-
Regarding certification fees at Teched
Hi, I need a small clarification regarding the fees to be paid at Teched, Bangalore. I would like to take the full conference and take up one of the exam. Should i need to pay Rs.14,000/- and Rs.5,500/- or just Rs.14,000/- is enough? Thanks, Chandra
-
if i order ipad 2 now. which ios version will be installed for me in Ipad 2 ?