Report using cursors and loop
Hi,
can anybody suggest what kind of report and in what way I have to use if I want to write query using cursors within loops?
Thanks!
Karina.
Hi,
I found the solution. I just rewrite all cursors within one SQL statement and it works.
Karina.
Similar Messages
-
How to create store procedure using cursor, and looping condition with exce
Hi,
I am new in pl/sql development , please help me for follwoing
1. I have select query by joining few tables which returns lets say 100 records.
2. I want to insert records into another table(lets say table name is tbl_sale).
3. If first record is inserted into tbl_sale,and for next record if value is same as first then update into tbl_sale else
insert new row
4. I want to achieve this using store procedure.
Please help me how to do looping,how to use cursor and all other necessary thing to achieve this.DECLARE
b NUMBER;
BEGIN
UPDATE tbl_sale
SET a = b
WHERE a = 1;
IF SQL%ROWCOUNT = 0
THEN
INSERT INTO tbl_sale
(a
VALUES (b
END IF;
END;note : handle exceptions where ever needed
Regards,
friend
Edited by: most wanted!!!! on Mar 18, 2013 12:06 AM -
Why use cursor and for loop?
Hi All
So in general why would we use a cursor and a for loop to do update in a stored procedure?
Why wouldnt we just use a single update statement ?
is there compelling reason for using a cursor and a for loop: I am reading some code from a co-worker that the business logic for the select (set need to be updated) is complex but the update logic is simple (just set a flag to (0 or 1 or 2 or 3 or 4).
But eventually the select come down to a key (row_id) so I re-write it using just a single sql statement.
The size of the main table is about 2.6 to 3million rows
Any thoughts on that??
The code below I just do a google for cursor for update example in case for something to play with
-Thanks for all your input
create table f (a number, b varchar2(10));
insert into f values (5,'five');
insert into f values (6,'six');
insert into f values (7,'seven');
insert into f values (8,'eight');
insert into f values (9,'nine');
commit;
create or replace procedure wco as
cursor c_f is
select a,b from f where length(b) = 5 for update;
v_a f.a%type;
v_b f.b%type;
begin
open c_f;
loop
fetch c_f into v_a, v_b;
exit when c_f%notfound;
update f set a=v_a*v_a where current of c_f;
end loop;
close c_f;
end;
exec wco;
select * from f;
drop table f;
drop procedure wco;
Joining multiple tables
create table numbers_en (
id_num number primary key,
txt_num varchar2(10)
insert into numbers_en values (1, 'one' );
insert into numbers_en values (2, 'two' );
insert into numbers_en values (3, 'three');
insert into numbers_en values (4, 'four' );
insert into numbers_en values (5, 'five' );
insert into numbers_en values (6, 'six' );
create table lang (
id_lang char(2) primary key,
txt_lang varchar2(10)
insert into lang values ('de', 'german');
insert into lang values ('fr', 'french');
insert into lang values ('it', 'italian');
create table translations (
id_num references numbers_en,
id_lang references lang,
txt_trans varchar2(10) not null
insert into translations values (1, 'de', 'eins' );
insert into translations values (1, 'fr', 'un' );
insert into translations values (2, 'it', 'duo' );
insert into translations values (3, 'de', 'drei' );
insert into translations values (3, 'it', 'tre' );
insert into translations values (4, 'it', 'quattro');
insert into translations values (6, 'de', 'sechs' );
insert into translations values (6, 'fr', 'six' );
declare
cursor cur is
select id_num,
txt_num,
id_lang,
txt_lang,
txt_trans
from numbers_en join translations using(id_num)
left join lang using(id_lang)
for update of translations.txt_trans;
rec cur%rowtype;
begin
for rec in cur loop
dbms_output.put (
to_char (rec.id_num , '999') || ' - ' ||
rpad (rec.txt_num , 10 ) || ' - ' ||
rpad(nvl(rec.txt_trans, ' '), 10 ) || ' - ' ||
rec.id_lang || ' - ' ||
rpad (rec.txt_lang , 10 )
if mod(rec.id_num,2) = 0 then
update translations set txt_trans = upper(txt_trans)
where current of cur;
dbms_output.put_line(' updated');
else
dbms_output.new_line;
end if;
end loop;
end;
/Edited by: xwo0owx on Apr 25, 2011 11:23 AMAdding my sixpence...
PL/SQL is not that different from a SQL perspective than any other SQL client language like Java or C# or C/C++. PL/SQL simply integrates the 2 languages a heck of a lot better and far more transparent than the others. But make no mistake in that PL/SQL is also a "client" language from a SQL perspective. The (internal) calls PL/SQL make to the SQL engine, are the same (driver) calls made to the SQL engine when using Java and C and the others.
So why a cursor and loops in PL/SQL? For the same reason you have cursors and loops in all these other SQL client languages. There are the occasion that you need to pull data from the SQL engine into the local language to perform some very funky and complex processing that is not possible using the SQL language.
The danger is using client cursor loop processing as the norm - always pulling rows into the client language and crunching it there. This is not very performant. And pretty much impossible to scale. Developers in this case views the SQL language as a mere I/O interface for reading and writing rows. As they would use the standard file I/O read() and write() interface calls.
Nothing could be further from the truth. SQL is a very advance and sophisticated data processing language. And it will always be faster than having to pull rows to a client language and process them there. However, SQL is not Turing complete. It is not the procedural type language that most other languages we use, are. For that reason there are things that we cannot do in SQL. And that should be the only reason for using the client language, like PL/SQL or the others, to perform row crunching using a client cursor loop. -
How to pick max value from a column of a table using cursor and iteration
Hello Everybody
I have a table loan_detail
and a column in it loan_amount
now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
here is my table
LOAN_AMOUNT
100
200
300
400
500
5600
700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
Regards
PeeyushSQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
HIGHEST_SAL LOWEST_SAL
5000 800
SQL> set serverout on
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := counter;
10 counter := counter + 1;
11 END LOOP;
12 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
13 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
14 END;
15 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Even smaller
SQL> DECLARE
2 TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
3 sal_tbl tmp_tbl;
4 CURSOR emp_sal IS
5 SELECT sal FROM emp;
6 counter INTEGER := 1;
7 BEGIN
8 FOR i IN emp_sal LOOP
9 sal_tbl(i.sal) := 1;
10 END LOOP;
11 DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
12 DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
13 END;
14 /
Lowest SAL:800
Highest SAL:5000
PL/SQL procedure successfully completed.
SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM -
Hello.
I need help creating a cursor and loop that will update a column based on data from another column. I keep getting the following errors. Any suggestions? These identifiers are valid tables in columns in my data base.
SQL> DECLARE
2
3 CURSOR c1 IS
4 SELECT SectionID, Coursename, StudentID, FinalGrade from Registration, Section, Grade WHERE registration.sectionid = grade.sectionid and registrationid.studentid = grade.studentid;
5
6 v_SectionID number(10);
7 v_CourseName varchar(20);
8 v_StudentID number(10);
9 v_FinalGrade varchar2(5);
10
11 BEGIN
12
13 OPEN c1;
14
15 LOOP
16 FETCH c1
17 INTO v_SectionID, v_CourseName, v_StudentID, v_FinalGrade;
18 EXIT WHEN c1%NOTFOUND;
19
20 update grade
21
22 SET FinalGrade = translate(substr(v_CourseName,-1,1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','AAAAAABBBBBCCCCCDDDDEEEEEE')
23
24 where Grade.StudentID = Registration.StudentID;
25
26 END LOOP;
27 CLOSE c1;
28 END;
29 /
SELECT SectionID, Coursename, StudentID, FinalGrade from Registration, Section, Grade WHERE registration.sectionid = grade.sectionid and registrationid.studentid = grade.studentid;
ERROR at line 4:
ORA-06550: line 4, column 140:
PL/SQL: ORA-00904: "REGISTRATIONID"."STUDENTID": invalid identifier
ORA-06550: line 4, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 24, column 27:
PL/SQL: ORA-00904: "REGISTRATION"."STUDENTID": invalid identifier
ORA-06550: line 20, column 3:
PL/SQL: SQL Statement ignored863737 wrote:
I am taking a intro course online so I am trying to teach myself this. The most basic rule for designing and writing good Oracle code that will perform and will scale is to use SQL for what it is good at and using PL/SQL for what it is good at. And between the two, SQL is by far superior when it comes to crunching data in the database.
The simple maxim is Maximize SQL. Minimize PL/SQL.
What you want to do with that PL/SQL code can be done using SQL only. And since you are learning SQL and PL/SQL, it is very important that you learn the correct way to do this.
Have a look at Oracle® Database SQL Language Reference guide.
The update syntax supports:
UPDATE <dml_expression_clause>
Where this +<dml_expression_clause>+ is a SELECT statement (aka an in-line view) - that can contain joins.
Simple example. You join tables t1 and t2 to identify the rows in t1 to update. The +<dml_expression_clause>+ will define this join. The actual update part (using the SET clause) will update the relevant rows in t1.
This is also called an updatable view - though the +<dml_expression_clause>+ does not need to use an actual view.
Have a look at AskTom for a discussion on this. Also note that you can also create a view and define an "instead-of" trigger on the view - this particular feature enhances the ability to update DML expressions - but is not a prerequisite.
Note that there can be very significant performance degradation by NOT using an UPDATE on a +<dml_expression_clause>+ and instead coding that manually via PL/SQL.
So do yourself a favour and learn the CORRECT way to resolve the type of UPDATE problem you have. -
Very Urgent..How to create a report Using SQ01 and Sq02.
Hi Friends,
It's very urgent.pl help me in generating a report using SQ01 and SQ02.
Help is appreciated.
thanks In advance.
Regards,
Nanditha.Check out these links...
http://www.insightcp.com/res_15.htm
http://www.ams.utoronto.ca/Assets/output/assets/adhoc_2990830.pdf.pdf
Also, do basic search in this forum...you will find a lot of threads related to this.
SKR -
Is it possible to use cursors and zooming in the plotxy function that outputs pictures?
I use the plot multi xy function in my vi that outputs a picture. And I wonder if it is possible to use cursors and zooming with this output picture?
Thanks,
Ogulcan KayaHi Ogulcan,
it is, but not as straightforward as with standard graphs.
As a starting point take a look at the example shipped with LV: Modified Smith Plot with Zooming.vi; you find it in picture examples.
Good luck,
Alberto -
Generate report using CURSORS? - Simple question
Folks,
I'm a real newbie still with PL/SQL so please excuse my ignorance I have a simple report I need to generate. The following SQL statement gets me all my "header" records:
SELECT OHA.ORDER_NUMBER, HEADER_ID, ATT11, ATT12, ATT16
FROM XXXWD.WD_DUPS DUPS, OE_ORDER_HEADERS_ALL OHA
WHERE OHA.ATTRIBUTE11 = DUPS.ATT11
AND OHA.ATTRIBUTE12 = DUPS.ATT12
AND OHA.ATTRIBUTE16 = DUPS.ATT16
AND (OHA.FLOW_STATUS_CODE NOT IN ('CLOSED', 'CANCELLED'))
AND (ATT11 <> 'WESTERN SERVICE')
ORDER BY ATT11, ATT12, ATT16
What I want to do now is have a second script that will display all my detail records. Something like:
SELECT OLA.LINE_NUMBER, OLA.ORDERED_ITEM, OLA.FLOW_STATUS_CODE
FROM OE.ORDER_LINES_ALL OLA
WHERE OLA.HEADER_ID = OHA.HEADER_ID
I expect I'd do this with two cursors, passing the value of my HEADER_ID to my second cursor. But when I've used cursors before, they primarily have been to import data, and manipulate data. But what if I just want to create a report using these?
I essentially want to display my header information, and then any lines below that data (if there is any, there may be a header with no lines).
Can I create a simple report like this with cursors? Any help with this would be IMMENSELY appreciated. I'm really under the gun... :)
Thanks so much!
SteveHere's one query that will give you everything:
SELECT OHA.ORDER_NUMBER
,OHA.HEADER_ID
,DUPS.ATT11
,DUPS.ATT12
,DUPS.ATT16
,OLA.LINE_NUMBER
,OLA.ORDERED_ITEM
,OLA.FLOW_STATUS_CODE
FROM XXXWD.WD_DUPS DUPS
,OE_ORDER_HEADERS_ALL OHA
,OE.ORDER_LINES_ALL OLA
WHERE OLA.HEADER_ID = OHA.HEADER_ID
AND OHA.ATTRIBUTE11 = DUPS.ATT11
AND OHA.ATTRIBUTE12 = DUPS.ATT12
AND OHA.ATTRIBUTE16 = DUPS.ATT16
AND OHA.FLOW_STATUS_CODE NOT IN ('CLOSED', 'CANCELLED')
AND DUPS.ATT11 <> 'WESTERN SERVICE'
ORDER BY OHA.ORDER_NUMBER
,OLA.LINE_NUMBER
,DUPS.ATT11
,DUPS.ATT12
,DUPS.ATT16
;(correction in order by clause)
Message was edited by:
Eric H -
Learning... cursor and loop... suggestions?
Although I'm looking for non version specific information... Here is what I'm running on.
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Solaris: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
I'm trying to figure out the whole relationship and execution of inserting data into a table, manipulating the table and anything else via a procedure using a cursor and a loop.
Sounds simple, but all the examples I find out there all deal with dbms_output.. obviously because it's 'safe'...
So I'm trying to use context that my mind makes quick sense of.
I built a simple table with vehicle makes, with the intention of using exercises to add models, trim levels, engines...pretty much all vehicle specifications.
CREATE TABLE VEH_COMP
MAKE VARCHAR2(20 BYTE),
MODEL VARCHAR2(50 BYTE),
TRIM VARCHAR2(50 BYTE),
ENGINE VARCHAR2(50 BYTE),
TRANSMISSION VARCHAR2(50 BYTE),
HORSEPOWER NUMBER,
TORQUE NUMBER
TABLESPACE USERS
RESULT_CACHE (MODE DEFAULT)
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 80K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
Next I tried to build a simple sql block to insert a handful of Auto makes into the table... I know this is ugly, and here is where I'm looking for suggestions for improvement.
DECLARE
CURSOR m1
IS
SELECT 'Ford' as make FROM DUAL
UNION ALL
SELECT 'Chevy' FROM DUAL
UNION ALL
SELECT 'Dodge' FROM DUAL
UNION ALL
SELECT 'BMW' FROM DUAL
UNION ALL
SELECT 'Audi' FROM DUAL
UNION ALL
SELECT 'Mercedes' FROM DUAL;
BEGIN
FOR v_make IN m1
LOOP
dbms_output.put_line(v_make.make);
insert into veh_comp (make) values (v_make.make);
END LOOP;
END;
Now I'm looking to add models. Like Ford Mustang, Ford Lightning, Chevy Corvette, Chevy Camaro, Dodge Charger, Dodge Challenger, Dodge Viper, BMW M5, BMW M3, BMW M6...
Then I need to add engine sizes... I6, V8
Then I need to add transmission... M6, M5, A5, A6, A8
I'm a little confused as how I should execute this part. Or better yet, I'm sure there is a better way to include it all at once...
I've been looking on line on how to do this and again...only really finding dbms_output.put_line. And I did grasp how I could do something along this lines from using a spool file. But I really want to learn to be more efficient.
What's the best way to do this?
Thanks.From what I can gather from the code, if I create some example data, you just want a query that does something like...
SQL> select * from vw_ta;
LID SID C0 C1 C2 C3
1 1 1 2 3 4
1 2 1 2 3 4
1 3 1 2 3 4
1 4 1 2 3 4
1 5 1 2 3 4
1 6 1 2 3 4
2 1 1 2 3 4
2 2 1 2 3 4
2 3 1 2 3 4
3 1 1 2 3 4
3 2 1 2 3 4
3 3 1 2 3 4
3 4 1 2 3 4
3 5 1 2 3 4
3 6 1 2 3 4
3 7 1 2 3 4
4 1 1 2 3 4
5 1 1 2 3 4
5 2 1 2 3 4
19 rows selected.
SQL> ed
Wrote file afiedt.buf
1 SELECT LID, SID, Cnt, Limits, dense_rank() over (order by lid) as grp, Iter
2 FROM (SELECT LID
3 ,SID
4 ,COUNT(*) OVER (PARTITION BY LID) Cnt
5 ,TRUNC(COUNT(*) OVER (PARTITION BY LID)/4)*4 Limits
6 ,row_number() over (partition by LID order by SID desc) iter
7 FROM VW_TA
8 WHERE C0 > 0 AND C1 > 0 AND C2 > 0 AND C3 > 0
9 ORDER BY LID, SID DESC
10 ) a
11* WHERE ITER <= LIMITS
SQL> /
LID SID CNT LIMITS GRP ITER
1 6 6 4 1 1
1 5 6 4 1 2
1 4 6 4 1 3
1 3 6 4 1 4
3 7 7 4 2 1
3 6 7 4 2 2
3 5 7 4 2 3
3 4 7 4 2 4
8 rows selected.
You already had the iteration part in your query, you just needed the Group number which is achieved using a dense_rank on the result. -
Project Stock report using QBEW and QBEWH tables
Dear All,
First of all Happy New Year to all of you.
Here is my question: I want to build a ABAP report for stock situation for project stock. The report demands the stock by closing month. Suppose I run the report for month 11/2010, it should show the closing stock at the end of the month 11/2010.
Now I am using the values from table QBEW for this purpose.But QBEW table shows only current entries. If a material stock is changed or value is changed, it goes into history table QBEWH.
Now problem is that if I try to make sum of QBEW and QBEWH, the stock value is excessive. Now I am not sure which entries I should consider from QBEWH table for this purpose.
Has anybody worked on similar requirement?
Thanks in advance!
Regards,
Mahendra DigheHi,
Please try the report S_P00_07000140 - Inventory and Raw material for special stock report.
Thanks.
abdul -
How to create a crystal Report using C# and SQL Server
Hi, im new in creating crystal report using SQL Server, and im making a project.. and could someone help me how to connect your .sdf (SQL Server File) from the bin to your crystal report? thanks a lot.. i followed some instructions like this one (https://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/48c0dd48-5b23-49da-8601-878f8406125e/how-to-create-a-crystal-report-using-sql-server-visual-c?forum=csharpgeneral)
but i got an error on the adding of server name and database portion.. thanks a lot in advanceHello,
Crystal Reports are supported on
http://scn.sap.com/community/crystal-reports.
Karl
When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
My Blog: Unlock PowerShell
My Book:
Windows PowerShell 2.0 Bible
My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}}) -
I created a Pages document inserting 2 columns using 1) Inspector 2) Layout 3) columns. How do I decrease the height of the column. Have tried to use the cursor and drag down the top border, but that does not reset/decrease the top border.
Set your columns back to one for the moment. In layout mode, insert a Text box. Place it in the upper left corner of your document, and drag down and right to the size of the container for your two columns. Click inside the Text Box, and now bump up your columns to 2. Your two columns are now contained in this resizable Text Box.
-
Stock report using mbew and mbewh
Hi to all.
i want to prepare o report for month end stock.
i am preparing this report using tables mbew and mbewh..
as per my knowledge goes the current stock is in the table mbew and history stock is in the table mbewh.
i am picking up the current data from the mbew and history data from mbewh..
the report is working fine till for january to august..
but i dont know what wrong happen when i run it for the last month of previous year i.e 01/12/2005.
it is showing some data difference for this period and also due to this some difference for earlier periods..
please suggest me what could be the reason..
because i have worked a lot on it and it is also working fine for this year...
also tell me what could be the problem..
i will surely reward back with points..HI
I guess the problem might be lying with the way you
are handling the date or period.
Can you post your code???
Kind Regards
Eswar -
Standard SAP Reporting Using Classes and Characterstics
Hi Experts,
I am currently interested in if there are any options to using classes and characteristics within standard reporting in SAP ECC. I know some reports have the selection critiera and allow for other fields to be added to reports. Does anyone know the reports that use classes and characteristics or have the ability to use them. Any help is appreciated.
Regards,Consultant 01,
I am not sure of the specifics of your question. The reporting approach depends on the objective of the reports and the business requirements. In general, transaction MC03 can be use to report via classification characteristics. This key figure reporting option covers many classifying attributes.
If you want to report using batch classification and characteristic values, BMBC offers options for reporting on batches. In this case you may use the classification tab of BMBC to enter the batch class for "search with batch class" and/or "search with selection class". The latter allows you to view and/or narrow your search by characteristic values.
I hope that this helps.
Regards,
Wayne -
FI Reports using ABAP and their Related Tables
Hi can anyone tell me some of the commonly developed reports from FI using ABAP and their related Tables.
Thank YouYou can find the details about the FI module here
http://www.sap-img.com/sap-fi.htm
Regards,
Ravi
Note : Please mark all the helpful answers
Maybe you are looking for
-
Can I share my address book with other user profiles on my computer?
My wife and I both logon to our iMac with different user accounts. However, we know all the same people and would like to share the contents of the Address Book. How do we go about this so that if I add a person to the address book, that my wife will
-
Incoming/Outgoing Excise Invoice
What information should be maintained for following fields that appear under Header:- Excise Ref.no., Excise Ref Date & Excise Removal Time. also when I am trying to post an incoming excise invoice it says "no match record found "GL Account" (OACT) O
-
Usage of command node in smartforms
Hi my doubt is can I branch to multiple pages using command node in smart forms.if so how to do it.
-
Iphone 3G - Youtube Native App Problem
Hi all, I'm using Iphone with with firmware 3.1.3. For last 3-4 days Iphone cannot connect to Youtube. Every time I try "Cannot connect to Youtube" screen appears prior to the "No Videos" screen. So far I tried different networks as Edge/3G/Wi-fi but
-
Unable to install updates that came with the macbook
When I am trying to update like iPHoto and Imovie I am getting an error asking me to sign in with the account id that I used at the time of purchase. I have these installed with my Macbook pro and is trying to update after login with my apple id. Can