Cursors vs while loop
Hi,
We know cursors are evil, use lot of memory, adds up tempdb activity, not scalable, hinders concurrency etc...Say if I replace 10 heavily used cursors in OLTP system with while loops how much do I gain if any and
how can I measure that. How can I convince my code review DBA to make this change? Does this change help the server?
The big gains (orders of magnitude) will come by changing cursors to set-based statements as you've done. If you can avoid row-by-row looping (through cursors or otherwise), then you should see some good gains there as well. In SQL Server 2005's CTEs and MARS we've removed some of the remaining need to use cursors and loops. But there are some situations where row-by-row processing still seems to be needed, and performing some non-set-based statement for a set of rows is the primary example .. executing a DBCC command for each database, for example.
If you find you are calling a stored proc for each row, perhaps you can pass a table containing the rows into the stored proc (perhaps by using a temp table) and then use set-base operations inside the stored proc, but there are times when you just need to call the sp row by row. If, after investigating all the set-based alternatives, you find you really do have to process rows one by one, then cursors are one way of iterating through a set of rows, and they do provide some good functionality with a well-defined behavior and you'll probably use your cursor together with a WHILE loop.
If you don't use a cursor to hold the rows to process, you'll have to retrieve a single row yourself each time through the loop; that'll probably be more coding for you, increase the potential for more bugs in your code, perhaps be more costly during execution, etc. So the trade-off becomes one of using cursors with a known downside, versus custom code with other potential drawbacks.
I'd say the "change cursors to while loops" statement oversimplifies the situation and falls way below the "change cursors to set-based operations where possible" primary guideline .. and it's unfortunate that it's at the top of the list in the article you mention.
Don
Similar Messages
-
I have always seen and used CURSORs with WHILE loops.
I am looking for great examples where CURSORs are used without WHILE loops. Thanks.
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014You can implement any logic that you want. We need to know what is your needs in order to post a code. If all you need is to make a loop on all the SET in the same way while do, without using while loop then here is a simple example. You can implement any
WHILE loop with IF condition and GOTO. This is only for sake of discussion and I will not use this code in production :-)
use AdventureWorks2014
GO
SET NOCOUNT ON;
DECLARE @vendor_name nvarchar(50)
DECLARE vendor_cursor CURSOR SCROLL FOR
SELECT Name
FROM Purchasing.Vendor
WHERE PreferredVendorStatus = 1
OPEN vendor_cursor
PRINT '******** Using While loop ********'
FETCH NEXT FROM vendor_cursor INTO @vendor_name
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @vendor_name
FETCH NEXT FROM vendor_cursor INTO @vendor_name
END
CLOSE vendor_cursor;
OPEN vendor_cursor
PRINT '******** Without Using While loop! ********'
FETCH NEXT FROM vendor_cursor INTO @vendor_name
Starting_Read_From_Cursor:
-- Using If with GO TO
IF @@FETCH_STATUS = 0 BEGIN
PRINT @vendor_name
FETCH NEXT FROM vendor_cursor INTO @vendor_name
GOTO Starting_Read_From_Cursor
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;
I hope this useful :-)
I am not sure for what, at the moment
[Personal Site] [Blog] [Facebook] -
What is the best way for writing cursors in while loops
i just chage my flatform from visual foxpro to sql
for every report part using query analyser i have to make report on any tables
suppose : - mastempno wise report
so i heve to use cursor or store procedure in while loop
for searching in transaction table
so sometime i get confunsion of using cursors two times in while loop for fetching the values
so help me for writing some simple codes for using cusrors in while loop while can fetch all values in transaction tableIs this what u r looking for
A Simple Cursor Example Which gives all records
from Employee table
Declare
Cursor Emp is
Select Empno,Ename
FRom Employee;
Begin
For x in Emp Loop
:block.empname := x.ename;
End Loop;
End;
Regards,
Ashutosh -
FUNCTION Cursor / WHILE LOOP vs LOOP / Logic help (RESOLVED)
(I HAVE RESOLVED - making it more efficient, I hope - see FUNCTION) if anyone has a different =, more efficient way to write this, please share...
I need help with a Function:
I am trying to get a handle on a rate. This table has and employee column this is usually populated with 'ALL'. On the off chance that it has an actual employee number, I want that rate; otherwise, the rate with ALL. Everything in both records will be the same except the employee field. (if there happens to be an ALL rate and an individual employee rate. Usually there will only be one record).
As I started to write this, I tested the WHILE loop logic and did not get a value back, but when I test a simple loop, I do. I have not finished the entire logic only getting a handle on a record that has the employee.
I would like help:
Understanding why WHILE loop isn't working
How to go about testing if CURSOR c_job_payroll_rate_emp FOUND - get this value and EXIT otherwise IF NOT FOUND then get value from c_job_payroll_rate_all
Start to my function is below w/ testing comments(** THIS IS THE RESOLVED FUNCTION.
FUNCTION job_pay_rate(in_comp_code IN jobpayrate.jpr_comp_code%TYPE,
in_job_code IN jobpayrate.jpr_job_code%TYPE,
in_phs_code IN jobpayrate.jpr_phs_code%TYPE,
in_cat_code IN jobpayrate.jpr_cat_code%TYPE,
in_trd_code IN jobpayrate.jpr_trd_code%TYPE,
in_emp_code IN jobpayrate.jpr_emp_no%TYPE)
RETURN jobpayrate.jpr_billing_rate%TYPE
IS
v_val jobpayrate.jpr_billing_rate%TYPE;
CURSOR c_job_payroll_rate_all IS
SELECT max(jpr_billing_rate) rate
FROM jobpayrate
WHERE jpr_comp_code = in_comp_code
AND jpr_job_code = in_job_code
AND jpr_trd_code = in_trd_code
AND jpr_phs_code = in_phs_code
AND jpr_cat_code = in_cat_code
AND jpr_emp_no <> in_emp_code;
CURSOR c_job_payroll_rate_emp IS
SELECT max(jpr_billing_rate) rate
FROM jobpayrate
WHERE jpr_comp_code = in_comp_code
AND jpr_job_code = in_job_code
AND jpr_trd_code = in_trd_code
AND jpr_phs_code = in_phs_code
AND jpr_cat_code = in_cat_code
AND jpr_emp_no = in_emp_code;
BEGIN
FOR rec_e IN c_job_payroll_rate_emp LOOP
v_val := rec_e.rate;
IF rec_e.rate IS NULL THEN
FOR rec_a IN c_job_payroll_rate_all LOOP
v_val := rec_a.rate;
END LOOP;
END IF;
END LOOP;
RETURN v_val;
END job_pay_rate;
Message was edited by:
KBRather than using cursors for zero or 1 row queries, use SELECT ... INTO
Untested
FUNCTION job_pay_rate(in_comp_code IN jobpayrate.jpr_comp_code%TYPE,
in_job_code IN jobpayrate.jpr_job_code%TYPE,
in_phs_code IN jobpayrate.jpr_phs_code%TYPE,
in_cat_code IN jobpayrate.jpr_cat_code%TYPE,
in_trd_code IN jobpayrate.jpr_trd_code%TYPE,
in_emp_code IN jobpayrate.jpr_emp_no%TYPE)
RETURN jobpayrate.jpr_billing_rate%TYPE
IS
v_val jobpayrate.jpr_billing_rate%TYPE;
BEGIN
SELECT max(jpr_billing_rate) rate INTO v_val
FROM jobpayrate
WHERE jpr_comp_code = in_comp_code
AND jpr_job_code = in_job_code
AND jpr_trd_code = in_trd_code
AND jpr_phs_code = in_phs_code
AND jpr_cat_code = in_cat_code
AND jpr_emp_no = in_emp_code;
RETURN v_val;
EXCEPTION
WHEN NO_DATA_FOUND THEN
SELECT max(jpr_billing_rate) rate INTO v_val
FROM jobpayrate
WHERE jpr_comp_code = in_comp_code
AND jpr_job_code = in_job_code
AND jpr_trd_code = in_trd_code
AND jpr_phs_code = in_phs_code
AND jpr_cat_code = in_cat_code
AND jpr_emp_no <> in_emp_code;
RETURN v_val;
--OTHER EXCEPTION HANDLING
END; -
What's wrong with this while loop?
Hi, folks. for the code fragment blow, resultSetArray holds two objects of type ResultSet. From those println statement, i can see the whole while loop(the most outter one) process goes alrite the first time right from begining to the end. However, when variable j increases from 0 to 1, the code stops executing at the the line while(rs.next). I just couldnt figure out what causes the problem while i've been fighting with it for several hours. Could someone plz throw me some lights? With your help, i could possibly go to bed before sun rises...
while(j<resultSetArray.length)
//for(int j=0; j<resultSetArray.length; j++)
System.out.println("show me j is called twice " + j);
ResultSet rs = resultSetArray[j];
System.out.println("the converting rs object is called twice and it is not null " + rs);
int numWantedColumns = wantedColumnNames.size();
//if it's about go or single trip
if(j==0 && rs != null)
go = new Element("Go");
//go.setText("go");
//result.addContent(go);
//if it's about return trip
else if(j==1 && rs != null)
back = new Element("Back");
//back.setText("back");
//result.addContent(back);
if(rs!= null)
System.out.println("this hell is called twice coz it's not null");
while(rs.next())
System.out.println("what about here?");
Element flightInfo = new Element("FlightInfo");
System.out.println("while rs.next() is called");
for (int i = 0; i < numWantedColumns; i++)
String columnName = (String)wantedColumnNames.get(i);
System.out.println("column name is " + columnName);
String value = rs.getString(columnName);
System.out.println("column value is " + value);
flightInfo.addContent(new Element(columnName).setText(value));
if(j==0)
go.addContent(flightInfo);
else if(j==1)
back.addContent(flightInfo);
else if(rs == null)
break;
j++;
}i've got the problem sort out, eventually. there was actually nothing wrong with the while loop. it was caused by a typo in databse, instead of having Brisbane, i typed Bisbane. The single letter r cost me more than 6 hours to figure it out. it was such a painful feeling while i realized i made a such stupid mistake.
by the way, as jnw777 mentioned, output rs.next() info instead of the test line. i did try it, however i didnt realize even System.out.println(rs.next()) would cause the cursor move down one row from its current position. so, plus the original while(rs.next()) statement, it was moving the cursor two rows down at a time! And i just couldnt think of this caused me the fact i was only getting the even number row of the ResultSet while i was sniffing the bug from a class with 700+ lines of code. I was so excited and just couldnt stop yelling at the moment i got over it! That was a damn execiting moment...
now, i am wondering if anyone in this wonderland would like to share his/her stories. -
How can i use the same cursor in a loop fro multipletimes.
I am using two cursors.One to fetch sites and the other to fetch participants under each site.I am performing some job with that participants data.Now the problem is i am using the 2nd cursor in a loop.So it fetches the data of participants falling under one state.But when it comes to the second state,as the second cursor is already open it is unable to fetch the records.Please help me .How can i use the same cursor in a loop fro multipletimes.
I am sending the code which i have written in When-Button-Pressed-Trigger...
declare
sid number;
pid number;
cursor csid is select distinct(site_id) from cyber_ppt;
cursor cpid is select pc_id,st_dt,ed_dt from cyber_ppt where site_id = sid;
stdt varchar2(10);
eddt varchar2(10);
nom number;
stmonth varchar2(10);
edmonth varchar2(10);
cjan number:=0;
cfeb number:=0;
cmar number:=0;
capr number:=0;
cmay number:=0;
cjun number:=0;
cjul number:=0;
caug number:=0;
csep number:=0;
coct number:=0;
cnov number:=0;
cdec number:=0;
i number:=1;
begin
open csid ;
loop
fetch csid into sid;
exit when csid %notfound;
message(sid);
open cpid;
loop
fetch cpid into pid,stdt,eddt ;
exit when cpid %notfound;
message(sid||'-'||pid);
stmonth:=substr(stdt,4,3);
edmonth:=substr(eddt,4,3);
nom:= months_between(eddt,stdt);
while i <= round(nom)
loop
stmonth:=substr(stdt,4,3);
if stmonth='JAN' then
cjan:=cjan+1;
elsif stmonth='FEB' then
cfeb:=cfeb+1;
elsif stmonth='MAR' then
cmar:=cmar+1;
elsif stmonth='APR' then
capr:=capr+1;
elsif stmonth='MAY' then
cmay:=cmay+1;
elsif stmonth='JUN' then
cjun:=cjun+1;
elsif stmonth ='JUL' then
cjul:=cjul+1;
elsif stmonth ='AUG' then
caug:=caug+1;
elsif stmonth ='SEP' then
csep:=csep+1;
elsif stmonth ='OCT' then
coct:=coct+1;
elsif stmonth ='NOV' then
cnov:=cnov+1;
elsif stmonth ='DEC' then
cdec:=cdec+1;
end if;
stdt:=add_months(stdt,1);
i:=i+1;
end loop;
end loop;
end loop;
end;
try this /* untested */
DECLARE
sid NUMBER;
pid NUMBER;
CURSOR csid IS SELECT DISTINCT(site_id) FROM cyber_ppt;
CURSOR cpid(nSid NUMBER) is SELECT pc_id,st_dt,ed_dt FROM cyber_ppt WHERE site_id = nSid;
stdt VARCHAR2(10);
eddt VARCHAR2(10);
nom NUMBER;
stmonth VARCHAR2(10);
edmonth VARCHAR2(10);
cjan NUMBER:=0;
cfeb NUMBER:=0;
cmar NUMBER:=0;
capr NUMBER:=0;
cmay NUMBER:=0;
cjun NUMBER:=0;
cjul NUMBER:=0;
caug NUMBER:=0;
csep NUMBER:=0;
coct NUMBER:=0;
cnov NUMBER:=0;
cdec NUMBER:=0;
i NUMBER:=1;
BEGIN
FOR rec IN csid
LOOP
sid := rec.csid;
FOR cRec IN cpid(sid)
LOOP
pid := cRec.pc_id;
stdt := cRec.st_dt;
eddt := cRec.ed_dt;
stmonth:= SUBSTR(stdt,4,3);
edmonth:= SUBSTR(eddt,4,3);
nom:= months_between(eddt,stdt);
WHILE i <= round(nom)
LOOP
stmonth := SUBSTR(stdt,4,3);
IF stmonth='JAN'
THEN
cjan:=cjan+1;
ELSIF stmonth='FEB' THEN
cfeb:=cfeb+1;
ELSIF stmonth='MAR' THEN
cmar:=cmar+1;
ELSIF stmonth='APR' THEN
capr:=capr+1;
ELSIF stmonth='MAY' THEN
cmay:=cmay+1;
ELSIF stmonth='JUN' THEN
cjun:=cjun+1;
ELSIF stmonth ='JUL' THEN
cjul:=cjul+1;
ELSIF stmonth ='AUG' THEN
caug:=caug+1;
ELSIF stmonth ='SEP' THEN
csep:=csep+1;
ELSIF stmonth ='OCT' THEN
coct:=coct+1;
ELSIF stmonth ='NOV' THEN
cnov:=cnov+1;
ELSIF stmonth ='DEC' THEN
cdec:=cdec+1;
END IF;
stdt:=add_months(stdt,1);
i:=i+1;
END LOOP;
END LOOP;
END LOOP;
END; -
Program times out while looping at internal table with huge records - Needs fine tuning suggestions
Hi,
I am trying to execute a report. It times out while looping at vbap internal table. This internal table has 140000 records and does the validation within this loop and geenrates data for the output. Due to this huge volume, the program times out when executed foreground.
There are no nested loops, so I cannot apply 'Parallel Cursor' here.
Is there any way I can fine tune the program so that it doesn't timeout? Is it possible to apply 'Parallel Processing' . If yes, how?
Thanks,
PavanHi Pavan ,
->sort your internal table by all primary key for vbap.
->Read a Record from the table (use your condition here)
->if record satisfys your where condition ,get that record index .
->loop the table from the index (without condition) .
its like parallel cursor only but for single loop .;-)
->use field symbols ,wherever possible .
if still dump is coming ,contact your basis team .
regards,
Krishna. -
While loop is not working properly
Dear all,
I have a procedure which takes data form other tables and fill one table.
See partial code
DECLARE
-- WELLS
CURSOR c_well
IS
SELECT *
FROM well
where well_s = 3419740
-- Welltest
CURSOR c_welltest ( v_well_s NUMBER )
IS
SELECT *
FROM welltest_msr
WHERE well_s = 3419740
AND check_ind = 1
ORDER BY msr_date DESC
-- FBHP
CURSOR c_fbhp_dsr ( v_well_s NUMBER )
IS
select m.pfnu_s
, m.msr_date
, p.value
from msr_act m
, msr_act_par p
, r_msr_act_par r
where m.msr_act_s = p.msr_act_s
and p.r_msr_act_par_s = r.r_msr_act_par_s
and r.abbr = 'FBH1'
and m.pfnu_s = 3419740
and p.check_ind = 1
ORDER BY m.msr_date DESC
r_fbhp_dsr c_fbhp_dsr%ROWTYPE;
r_fbhp_dsr_leeg c_fbhp_dsr%ROWTYPE;
DELETE FROM so_welltest_plus;
FOR r_well IN c_well
LOOP
r_fbhp_dsr := r_fbhp_dsr_leeg;
OPEN c_fbhp_dsr ( r_well.well_s );
FETCH c_fbhp_dsr INTO r_fbhp_dsr;
FOR r_welltest IN c_welltest ( r_well.well_s )
LOOP
WHILE NVL(r_fbhp_dsr.msr_date,r_welltest.msr_date - 1) >=
r_welltest.msr_date
LOOP
FETCH c_fbhp_dsr INTO r_fbhp_dsr;
-- dbms_output.put_line (r_fbhp_dsr.msr_date);
IF c_fbhp_dsr%NOTFOUND
THEN
BEGIN
r_fbhp_dsr := r_fbhp_dsr_leeg;
EXIT;
END;
END IF;
END LOOP;
INSERT INTO SO_WELLTEST_PLUS
( WELL_S
, WELLNAME
, WELLTEST_DATE
, BFPD
, BOPD
, BWPD
, WATERCUT
, FBHP
, FBHP_SRO
, FBHP_DMG
, FBHP_DATE
, FBHP_SRO_DATE
, FBHP_DMG_DATE
VALUES
( r_well.well_s
, r_well.wellname
, r_welltest.msr_date
, r_welltest.fld_rate
, r_welltest.oil_rate
, r_welltest.wtr_rate
, v_watercut
, r_fbhp.value
, r_fbhp_dsr.value
, r_fbhp_dmg.value
, r_fbhp.msr_date
, r_fbhp_dsr.msr_date
, r_fbhp_dmg.msr_date
END LOOP;
CLOSE c_fbhp_dsr;
COMMIT;
END LOOP;
END;I will explain what the code is doing now:
Result cursor C_WELL:
+WELL_S+ +MSR_DATE+
3419740 23-OCT-12
3419740 18-SEP-12
3419740 28-AUG-12
3419740 16-JUL-12
3419740 14-JUN-12
3419740 11-MAY-12
3419740 12-APR-12
3419740 15-MAR-12
3419740 16-FEB-12
3419740 23-JAN-12
3419740 05-JAN-12
3419740 07-DEC-11Result for CURSOR c_fbhp_dsr:
+PFNU_S+ +MSR_DATE+ +VALUE+
3419740 23-OCT-12 445
3419740 19-SEP-12 447
3419740 28-AUG-12 441
3419740 16-JUL-12 449
3419740 14-JUN-12 449
3419740 09-MAY-12 451
3419740 11-APR-12 447
3419740 12-DEC-11 434
3419740 01-DEC-11 426After the while loop the data is inserted into the table:
+well_s+ +wellname+ +welltest_date+ +fbhp_sro+ +fbhp_sro_date+
3419740 3Z22 23-OCT-12 447 19-SEP-12
3419740 3Z22 18-SEP-12 441 28-AUG-12
3419740 3Z22 28-AUG-12 449 16-JUL-12
3419740 3Z22 16-JUL-12 449 14-JUN-12
3419740 3Z22 14-JUN-12 451 09-MAY-12
3419740 3Z22 11-MAY-12 451 09-MAY-12
3419740 3Z22 12-APR-12 447 11-APR-12
3419740 3Z22 15-MAR-12 434 12-DEC-11
3419740 3Z22 16-FEB-12 434 12-DEC-11
3419740 3Z22 23-JAN-12 434 12-DEC-11
3419740 3Z22 05-JAN-12 434 12-DEC-11
3419740 3Z22 07-DEC-11 426 01-DEC-11As you can see the value 23 oct 2012 from CURSOR c_fbhp_dsr is not being inserted.
That value should also be inserted.
The result should be like this:
+well_s+ +wellname+ +welltest_date+ +fbhp_sro+ +fbhp_sro_date+
3419740 3Z22 23-OCT-12 445 23-OCT-12
3419740 3Z22 18-SEP-12 441 28-AUG-12
3419740 3Z22 28-AUG-12 441 28-AUG-12
3419740 3Z22 16-JUL-12 449 16-JUL-12
3419740 3Z22 14-JUN-12 449 14-JUN-12
3419740 3Z22 11-MAY-12 451 09-MAY-12
3419740 3Z22 12-APR-12 447 11-APR-12
3419740 3Z22 15-MAR-12 434 12-DEC-11
3419740 3Z22 16-FEB-12 434 12-DEC-11
3419740 3Z22 23-JAN-12 434 12-DEC-11
3419740 3Z22 05-JAN-12 434 12-DEC-11
3419740 3Z22 07-DEC-11 426 01-DEC-11I did a dbms output on the while loop and it isn't collecting 23-oct-12.
Some please on this please.
Thanks,
DianaHi,
My procedure is not exactly doing what i want it to do.
My question is, how can i get it to do this:
Loop through cursor c_fbhp_dsr and c_welltest based on parameter well_s from c_well (see the data in first post).
When the MSR_DATE of c_fbhp_dsr is the same as MSR_DATE of c_welltest for the same WELL_S ( see cursor c_well), then data is inserted in the SO_WELLTEST_PLUS table. When the dates are not equal, then insert should be done on condition MSR_DATE of c_fbhp_dsr should be > than MSR_DATE of c_welltest.
INSERT INTO SO_WELLTEST_PLUS
( WELL_S
, WELLNAME
, WELLTEST_DATE
, FBHP_SRO
, FBHP_SRO_DATE
VALUES
( r_well.well_s
, r_well.wellname
, r_welltest.msr_date
, r_fbhp_dsr.value
, r_fbhp.msr_date
);Example:
CURSOR C_RBHP:
+PFNU_S+ +MSR_DATE+ +VALUE+
3419740 23-OCT-12 445
3419740 19-SEP-12 447
3419740 28-AUG-12 441
3419740 16-JUL-12 449
3419740 14-JUN-12 449
3419740 09-MAY-12 451
3419740 11-APR-12 447
3419740 12-DEC-11 434
3419740 01-DEC-11 426CURSOR C_WELLTEST:
+WELL_S+ +MSR_DATE+
3419740 23-OCT-12
3419740 18-SEP-12
3419740 28-AUG-12
3419740 16-JUL-12
3419740 14-JUN-12
3419740 11-MAY-12
3419740 12-APR-12
3419740 15-MAR-12
3419740 16-FEB-12
3419740 23-JAN-12
3419740 05-JAN-12
3419740 07-DEC-11Desired output:
+well_s+ +wellname+ +welltest_date+ +fbhp_sro+ +fbhp_sro_date+
3419740 3Z22 23-OCT-12 445 23-OCT-12
3419740 3Z22 18-SEP-12 441 28-AUG-12
3419740 3Z22 28-AUG-12 441 28-AUG-12
3419740 3Z22 16-JUL-12 449 16-JUL-12
3419740 3Z22 14-JUN-12 449 14-JUN-12
3419740 3Z22 11-MAY-12 451 09-MAY-12
3419740 3Z22 12-APR-12 447 11-APR-12
3419740 3Z22 15-MAR-12 434 12-DEC-11
3419740 3Z22 16-FEB-12 434 12-DEC-11
3419740 3Z22 23-JAN-12 434 12-DEC-11
3419740 3Z22 05-JAN-12 434 12-DEC-11
3419740 3Z22 07-DEC-11 426 01-DEC-11 -
SubVI with while loop + event structure not working in multi tab VI
Hello Everyone,
I am developing an interface for the control of a prober using Labview 2012, and I am stuck with some issue.
To start with I provide you with a simplified version of my control interface VI, and with the sub-VI used to build and manage the wafer maps.
The VI consists of several tabs: Prober Initialization, Wafer Handling, Wafer Map, Status, Error.
The sub-VI can:
1/ initialize the grid to display the map (sub VI Init Grid not provided here)
2/ import XY coordinates from a txt file (sub VI Wafer Map Import)
3/ display the coordinates and index of the die below the cursor
4/ and when a die position is double clicked, and the boolean "Edit Wafer Map" is true, then the user can change the state (color) of the die between On-wafer die and Selected Die
My issue:
If I use the sub-VI by itself, it works fine. However when I use it as a sub-VI in the tab "Wafer Map", the map does not build up and I can no further use the embedded functionalities in the sub-VI.
I suspect the while loop + event structure of the sub-VI to be the bottleneck here.
However I don't know which way to go, that's why I'd be glad to have some advice and help here.
Thank you.
Florian
Solved!
Go to Solution.
Attachments:
Control Interface.zip 61 KBHi NitzZ,
Thank you for your reply.
I tried to save the VIs in LV10, please tell me if you can open them now.
Inside he event structure there is quite some code, and since I don't want to make the main vi too bulky, I would like to keep it as a sub-VI.
As you can see from the sub-VI, the event structure is used for extracting cursor position and tracking the double click action. These events are linked, through a property node, to the image "Wafer Map" which is passed to the main vi through connector pane.
All values are passed this way as well (through connector pane). Is there another way?
Maybe "refnum", but I don't really understand how to use them...
If I use the event structure in the main vi, the wafer map is still not working. I tried it earlier.
To implement the multi tab front panel, I used a tab control, and a for loop + case structure. For each element of the case structure, there is a corresponding action.
For the case where I put the code (element=2) for Wafer Map, I also control the execution of the code with a case structure activated by the button "REFRESH". Otherwise I end up with a freezing of the panel right after the start.
I hope these comments help you understand better.
Regards,
Florian
Attachments:
Control Interface.zip 104 KB -
Busy Waiting While Loop in SubVI?
I'm working with a motor drive that sets a busy waiting flag while an instruction is executing and clears the flag when an instruction has been completed. I need to poll the busy flag, so I was planning on creating a subVI that contains a while loop to poll the flag. But, I know that it is recommended that while loops should not be placed in subVIs. I was wondering if there's an alternative. Would it be ok to put my code in a subVI? I've posted the code as an attachment.
Thank You,
Molana
Message Edited by Molana on 08-21-2006 02:18 PM
Attachments:
BusyWaiting.GIF 12 KBI don't see any "flag" in your code. Is this just a boolean indicator on the calling VI that is lit while the subVI executes and reset once the call completes? Are you just setting the "busy" cursor?
You should make sure to place this in a paralell loop in the main VI so you're not blocking the UI code and other processes. Also, as others have mentioned, your while loop needs a small wait.
LabVIEW Champion . Do more with less code and in less time . -
I am migrating a project from Java 1.3 to Java 1.7 update 25 and there is one search button functionality in which search is performed correctly but the UI flickers continuously because EventdispatchThread's pumpEventsForFilter goes to infinite while loop.
This same code works correctly in java 1.3.
Below is the part of the code related to searchbutton. Can you please suggest regarding this because the issue is within java api and not sure how to solve this.
SearchButton.java:
private JButton searchButton;
private String textsearchButton =
search.util.Language.getString("buttonStartSearch");
searchButton = new JButton(textsearchButton);
buttonPanel.add(searchButton);
searchButton.addActionListener(this);
searchButton.addActionListener(actionListener);
actionListener.actionPerformed(
new ActionEvent(
searchButton,
ActionEvent.ACTION_PERFORMED,
"toFront"));
actionListener.actionPerformed(
new ActionEvent(
searchButton,
ActionEvent.ACTION_PERFORMED,
"searchComplete"));
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
if (hasSearch) {
actionListener.actionPerformed(
new ActionEvent(
searchButton,
ActionEvent.ACTION_PERFORMED,
"searchComplete"));
else {
actionListener.actionPerformed(
new ActionEvent(
searchButton,
ActionEvent.ACTION_PERFORMED,
"search"));
searchButton.setEnabled(!hasSearch && hasCriterions());
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
else if (source == searchButton)
startSearch();
else if (source == exportButton)
exportSearch();
else { // ActionEvents from the panels occur
changesOccured();
EventDispatchThread:
void pumpEventsForFilter(int id, Conditional cond, EventFilter filter) {
addEventFilter(filter);
doDispatch = true;
//This while goes to infinite loop
[b] [color=red]while (doDispatch && !isInterrupted() && cond.evaluate()) {
pumpOneEventForFilters(id);
}[/color][/b]
removeEventFilter(filter);Please provide your code example as SSCCE . I'm not going to spent time guessing what your application code (other than shown) meigt do or not do.
bye
TPD -
Can I do while loop in a mapping. I know I can do this in process flow but can I do it in a mapping.
The OWB code generator produces a number of different code modes including set based SQL and various modes of row based code. In the row based code there are essentially (implicit) loops over the cursors, so there are implicit loops that you can do stuff, depends on what you are after.
Cheers
David -
Hi ,
I execute the below code in TOAD.
DECLARE
CURSOR c1
IS
SELECT *
FROM tab
WHERE ROWNUM < 5;
cd c1%ROWTYPE;
BEGIN
OPEN c1;
FETCH c1
INTO cd;
WHILE c1%FOUND
LOOP
DBMS_OUTPUT.put_line ( 'tname '
|| cd.tname
|| ' tabtype '
|| cd.tabtype
END LOOP;
-- FETCH c1
-- INTO cd;
END;
And got the below error..
Error at line 2
ORA-20000: ORU-10027: buffer overflow, limit of 20000 bytes
ORA-06512: at "SYS.DBMS_OUTPUT", line 32
ORA-06512: at "SYS.DBMS_OUTPUT", line 97
ORA-06512: at "SYS.DBMS_OUTPUT", line 112
ORA-06512: at line 19
If I add another FETCH INTO statement in the while loop block ,I will get the output.
But please let me know why I am getting this error exactly and how another FETCH INTO is preventing it.
Thanks.930414 wrote:
I execute the below code in TOAD.There is a more serious problem here - abusing server memory by abusing DBMS_OUTPUT.
Your code violates the basic principle of client-server. It copies (potentially large chunks) of data from server memory (buffer cache) into other server memory (PL/SQL engine's PGA memory).
Then the client (TOAD in this case), reads the data from the PGA. Which does NOT cause the PGA to shrink, and allocated memory to be given back to the o/s as free memory.
If your code is executed by 10 clients, there will be 10 clients abusing server memory.
The code is not performant. Is not scalable. And just bloody dangerous as it could lead to a server with insufficient memory, swapping virtual memory pages between disk cache and RAM so frantically, that even the kernel stuggles to get a single millisec of execution time.
DBMS_OUTPUT does not "print" stuff. It is not intended to be used for "printing" data for the client. DBMS_OUTPUT is a primitive interface for keeping a text buffer on the server, for recording basic debug-like messages and data. After server code has been executed (and recorded their debug messages), the client can read and display this buffer. That is how DBMS_OUTPUT should be used - and not as some kind of print device to "print" server data for the client. -
How to use one single boolean button to control a multiple while loops?
I've posted the attached file and you will see that it doesn't let me use local variable for stop button, but I need to stop all the action whenever I want but more than one single button on the front panel would look ugly... The file represents the Numeric Mode of
HP 5371A. thanks for your time
Attachments:
NUMERIC.vi 580 KBIn order to use a local variable, you can change the mechanical action of stop button (Switch When Pressed will work), or create a property node for it and select values. You'll also have to do a lot of work changing those for loops into while loops so that you can abort them.
-
While loop doing AO/AI ... Performanc​e?
Hi!
I have been trying to get a VI to do concurrent Analog
data in and out and both the input and output rates and
waveforms can change while the VI is running. My problem
is this:
(a) If I try putting the read and write operations in
separate while loops, one or the other loop will
die in a buffer over/underrun.
(b) If I put both into the same loop, then this works
but I am limited in the choice of data-rate parameters
because eventually one or the other DAQ VI will take
too long.
At this point I have only one loop and the buffers are big
enough to hold 10 iteration. Every time one of the loops
dies I reset it. Still this seems awkward. Is there a
way of improving on the loop overhead and putting t
he
input and output in separate loops? Or any other way to
improve performance?
RudolfOk, I knew that ocurences did something useful but I am
still a bit confused:
* Can you set an occurrence for an output event. None
of the examples I've seen say so but it looks like
it should work
* How do occurrences actually help with the "hanging"
problem. Does the compiler see the occurrence in
a loop and change the wait parameters?
I looked at devzone but most of the stuff I found there,
even the promising looking stuff was all about
synchronizing and triggering, not about occurrences.
Rudolf
JB wrote:
: Once in the read function, the program "hangs" until the number of
: data points is in the buffer. The same applies to the write function.
: This means that most of the time, your program is waiting.
: To sol
ve this problem, you must use DAQ occurrences (--> hardware
: events).
: For examples for using daq occurrences, type "daq occurrence" in the
: search of the LabVIEW help or even better, at
: http://zone.ni.com/devzone/devzoneweb.nsf
: Hope this helps
Maybe you are looking for
-
How do I move tabs from one window to another window?
With the previous version of firefox, I could drag a tab from one window to another window. (and if there was only one tab left in that window, it would automatically close that window because there were no more tabs open in it) With the latest versi
-
6921 Wall Mount - The Clip to keep the phone on hook keeps falling out!!
Has anyone experienced this issue with the 6921 on-hook clips. We turn the clip around when the phone is wall mounted and it hold the phone in. But it comes out so easily, as soon as you lift the handset out it pulls the clips out!!! we have like 3
-
XIF or BDoc Trigger even with errors in Business Document
Hello Experts, I am stuck in an issue regarding BDoc trigger. My query is as per my understanding, if a business document like Sales order or Opportunity is in error but is saved, there is no BDoc triggered. Please correct if I am wrong. My actual Re
-
Question being asked multiple times but need to sort the questions into the report
question needs to be placed 150 times on the form, but needs to be sorted as if it were one question on the report
-
How to access automation tools in photoshop elements 11?
I have photoshop elements 11 and the automation tools button is greyed out and cannot be selected. How can I access this function?