Loop through records in a table and update
Hi there,
Need some help here please. Sorry new to Oracle. I'm working with Oracle 10g.
I have a table like this;
ID Amount Date
123 5000 Oct-07-2011
123 null Oct-09-2011
124 7000 Oct-14-2011
124 null Oct-17-2011
124 null Oct-24-2011
What I'm trying to do here is loop thruogh the records and update the amount that's null with the previous amount with the same ID.
Some sample code that I follow for this?
Thanks very much for your help.
maybe some of this example migth help.
SQL> select id, amount, tdate
2 from (select 123 id, 5000 amount, to_date('Oct-07-2011','mon-dd-rrrr') tdate from dual union all
3 select 123 id, null amount, to_date('Oct-09-2011','mon-dd-rrrr') tdate from dual union all
4 select 124 id, 7000 amount, to_date('Oct-14-2011','mon-dd-rrrr') tdate from dual union all
5 select 124 id, null amount, to_date('Oct-17-2011','mon-dd-rrrr') tdate from dual union all
6 select 124 id, null amount, to_date('Oct-24-2011','mon-dd-rrrr') tdate from dual union all
7 select 124 id, 8000 amount, to_date('Oct-25-2011','mon-dd-rrrr') tdate from dual union all
8 select 124 id, null amount, to_date('Oct-28-2011','mon-dd-rrrr') tdate from dual)
9 order by id, tdate;
ID AMOUNT TDATE
123 5000 07-Oct-11
123 09-Oct-11
124 7000 14-Oct-11
124 17-Oct-11
124 24-Oct-11
124 8000 25-Oct-11
124 28-Oct-11
7 rows selected
SQL>
SQL> select id,
2 last_value(amount ignore nulls) over (partition by id order by tdate) amount,
3 tdate
4 from (select 123 id, 5000 amount, to_date('Oct-07-2011','mon-dd-rrrr') tdate from dual union all
5 select 123 id, null amount, to_date('Oct-09-2011','mon-dd-rrrr') tdate from dual union all
6 select 124 id, 7000 amount, to_date('Oct-14-2011','mon-dd-rrrr') tdate from dual union all
7 select 124 id, null amount, to_date('Oct-17-2011','mon-dd-rrrr') tdate from dual union all
8 select 124 id, null amount, to_date('Oct-24-2011','mon-dd-rrrr') tdate from dual union all
9 select 124 id, 8000 amount, to_date('Oct-25-2011','mon-dd-rrrr') tdate from dual union all
10 select 124 id, null amount, to_date('Oct-28-2011','mon-dd-rrrr') tdate from dual)
11 order by id, tdate;
ID AMOUNT TDATE
123 5000 07-Oct-11
123 5000 09-Oct-11
124 7000 14-Oct-11
124 7000 17-Oct-11
124 7000 24-Oct-11
124 8000 25-Oct-11
124 8000 28-Oct-11
7 rows selected
SQL>
Similar Messages
-
Insert one table and update another???
Hello,
I was wondering if it is possible to insert a record in one table and update another another record in a different table.
I have a form on my company's intranet that allows employees to add comments (ADDT Insert transaction) about new products we are going to bring to the market. At the same time, I would like to count the numbers of comments on a particular product and update that number for each product to see which product is getting the most reviews.
Right now the products are on the homepage with the title and inserted date. From there, the employees click on the product get the info and make comments about it. The problem for me is that I would like to see the comment counts for each product on the home page, which means I would have to update the product table with the count.
Sorry, I am using PHP as the technology.
When I used to do it in ASP, I would insert the comment using the POST from the form, but add another hidden field with the count in it and I would use the "Command" Server behavior to retrieve the number and update the other table field.
I noticed that dreamweaver removed the "Command" server behavior when using PHP.
All help is greatly appreciated
Charles.Hi Charles,
you can generally execute queries on a different table using Custom Triggers, and in regards to your "update the product table with the count" scenario the ADDT help file has a helpful pointer in the chapter "Custom transactions and triggers : Save additional information on login" -- in here you´ll find a sample "incremental counter" query which should be easy to adapt.
Cheers,
Günter Schenk
Adobe Community Expert, Dreamweaver -
New to PL/SQL and wondered if you can simply loop through records in order to ouptut as single string in a file?
DECLARE
v_string_rec VARCHAR(4000);
type v_cols is record;
BEGIN
FOR rec IN (SELECT * FROM PLAY_OWNER.STG_CUSTOMER)
-- somehow need to get the single record into record var then loop through that?
LOOP
FOR i IN v_cols.FIRST .. v_cols.LAST
LOOP
v_string_rec := recs(i) || v_string_rec;
END LOOP;
DBMS_OUPTPUT.PUT_LINE (v_string_rec);
-- UTL_FILE operations, insert rows
END LOOP;
END;
Give me a slap by all means, as I say kind of new to thisJust as a curiosity, using some xml capabilities we can obtain a strig concatenation of all columns of a given query this way:
SQL> with t as
2 (select xmltype(dbms_xmlgen.getxml(DBMS_XMLGEN.newContext('SELECT * FROM emp'))) doc
3 from dual)
4 select extract(column_value,'//text()') myrow
5 from t, table(xmlsequence(extract(t.doc,'/ROWSET/ROW')));
MYROW
7369SMITHCLERK790217-12-198080020
7499ALLENSALESMAN769820-02-1981160030030
7521WARDSALESMAN769822-02-1981125050030
7566JONESMANAGER783902-04-1981297520
7654MARTINSALESMAN769828-09-19811250140030
7698BLAKEMANAGER783901-05-1981285030
7782CLARKMANAGER783909-06-1981245010
7788SCOTTANALYST756619-04-1987300020
7839KINGPRESIDENT17-11-1981500010
7844TURNERSALESMAN769808-09-19811500030
7876ADAMSCLERK778823-05-1987110020
7900JAMESCLERK769803-12-198195030
7902FORDANALYST756603-12-1981300020
7934MILLERCLERK778223-01-1982130010
14 rows selected.And all the single attribute values this way:
SQL> with t as
2 (select xmltype(dbms_xmlgen.getxml(DBMS_XMLGEN.newContext('SELECT * FROM emp'))) doc
3 from dual)
4 select extract(column_value,'//text()') attribute_value
5 from (select column_value myrow
6 from t, table(xmlsequence(extract(t.doc,'/ROWSET/ROW')))
7 ) r, table(xmlsequence(extract(r.myrow,'/ROW/*')));
ATTRIBUTE_VALUE
7369
SMITH
CLERK
7902
17-12-1980
800
20
7499
ALLEN
SALESMAN
7698
20-02-1981
1600
300
30
7521
WARD
SALESMAN
7698
22-02-1981
1250
500
30
7566
JONES
MANAGER
7839
02-04-1981
2975
20
7654
MARTIN
SALESMAN
7698
28-09-1981
1250
1400
30
7698
BLAKE
MANAGER
7839
01-05-1981
2850
30
7782
CLARK
MANAGER
7839
09-06-1981
2450
10
7788
SCOTT
ANALYST
7566
19-04-1987
3000
20
7839
KING
PRESIDENT
17-11-1981
5000
10
7844
TURNER
SALESMAN
7698
08-09-1981
1500
0
30
7876
ADAMS
CLERK
7788
23-05-1987
1100
20
7900
JAMES
CLERK
7698
03-12-1981
950
30
7902
FORD
ANALYST
7566
03-12-1981
3000
20
7934
MILLER
CLERK
7782
23-01-1982
1300
10
101 rows selected.Max
[My Italian Oracle blog|http://oracleitalia.wordpress.com/2010/01/23/la-forza-del-foglio-di-calcolo-in-una-query-la-clausola-model/] -
Adding a new field in BCONT table and updating it
Hi All,
Is there any BAPI/BADI which will allow to add a new field in standard BCONT table and update its value?
Your solution will be really helpful.
Thanks,
NirupamHi Santosh,
This is required for UCES. We need to add a field in BCONT table (SE11/SE16), which will hold a flag value, i.e Y/N.
The value will be updated through a custom FM being used in UCES, and also based on the value there is a validation inside UCES.
This field need not be displayed inside any SAP screen.
thanks,
Nirupam
Edited by: Nirupam Deb on Jun 8, 2011 11:44 AM -
Delete records in a table and at the same time print out for reference
I am working on a req to delete some records from a table and at the same time, record/print the deleted records in the outstream (DBMS_OUTPUT.PUT_LINE)
DECLARE
v_rec_po hst_po%ROWTYPE;
BEGIN
DELETE FROM hst_po po
WHERE abbrpoid = '&opportunity_code'
AND updatedby = (SELECT employeeid
FROM tes_employee
WHERE name = &emp_name)
AND audittimestamp BETWEEN TO_DATE ('&start_timestamp',
'DD-MON-YYYY HH24:MI'
AND TO_DATE ('&end_timestamp',
'DD-MON-YYYY HH24:MI'
END;
I was thinking of using returning into and then using DBMS_output to pull out the delted records, but there will be multiple rows deleted fro this statements. I am stuck here..Can anyone helpHow about:
SQL> create table t
2 (x int)
3 /
Table created.
SQL> insert into t
2 select rownum
3 from all_objects
4 where rownum <= 10
5 /
10 rows created.
SQL> declare
2 cnt pls_integer;
3 begin
4 delete from t;
5 cnt := sql%rowcount;
6 dbms_output.put_line ('Removed: '||to_Char (cnt));
7 end;
8 /
Removed: 10
PL/SQL procedure successfully completed.Edited by: Alex Nuijten on Jun 4, 2009 8:58 AM -
How to count records from 2 tables and show in RDLC Report
hi all,
its being a one day searching for the solution but No Luck.
I have two SQL tables tblstudetail and tblfeereceiptdetail.
i just want to count records from both tables and show in RDLC report.
I tried SQl Query Like This:
select a.session, a.course,
Count(CASE a.ADstatus WHEN 'OK' THEN 1 ELSE 0 END ) AS Admission,
Count(CASE s .I_receiptstatus WHEN 'OK' THEN 1 ELSE 0 END) AS Feeprint
from
tblstudetail a
FULL join
tblfeereceiptdetail s on s.studentID = a.studentID
where a.session = '2015' AND s.Fsession = '2015' AND a.adcat = 'Regular'
GROUP BY a.session,a.course
ORDER by a.course
The result Show the Same Value in Both columns
Session Course Admission FeeDetail
2015 B.A. I 275 275
2015 B.A. II 307 307
2015 B.A. III 255 255
2015 B.Sc. I 110 110
2015 B.Sc. II 105 105
2015 B.Sc. III 64 64
Actully I want to Count How many ADMISSION have been Taken(FROM tblstudetail) and How many FEE RECEIPT have been Print (From tblfeereceiptdetail).
please guide me for this as soon as possible.
thanks in advance...I am counting 'OK' in both the table columns I.e 'ADstatus' in tblstudetail and 'feereceiptstatus' in tblfeereceiptdetail
please suggest me -
Help reg: Removing sel records in af:table and to prevent it from Commit
I have a af:table in a page with some records.The table is editable and the records in the table are fetched from the DB. Now, I edit some of the records in the table.
I would now like to delete one of the edited records.
The delete action listener method should remove the record from the table and when I click save on the page (to save the other edited records), the removed record should not get committed to the database.At the same time, the removed record (from the table) should not get deleted from the database either.
Can someone suggest on how can i achieve this?
I use a delete action listener method to remove the record from the table in the UI but not sure how to prevent the removed record from getting committed (since its been modified).
Thanks
Sreekanth VijayIf you remove a row with the delete method, the row will be deleted from the db if you don't change the eo behavior.
You can, as mentioned in the last post, use the remove from collection to remove three row from the current repeat without removing it from the db. To get three row back you have to execute the query again.
Timo -
Problem while looping through record set and tem table for matching data
hi I am using one record set and ane temp table and looping through both to find the match between dates.
If date matches then it shud do some processing otherwise it will return default values(null values).
FOR i IN student_rec .FIRST..student_rec .LAST /*student_rec.school_date has 01-MAR-2012,02-MAR-2012,03-MAR-2012,04-MAR-2012,05-MAR-2012*/
LOOP
l_return_out.school_date := student_rec(i).school_date;
l_return_out.marks_obtained := student_rec(i).marks_obtained;
FOR i IN selected_dates .FIRST..selected_dates .LAST /*selected_dates has 02-MAR-2012,03-MAR-2012,05-MAR-2012*/
LOOP
DBMS_OUTPUT.PUT_LINE(selected_dates(i));
IF selected_dates(i)=student_rec(i).sett_date
THEN
EXIT;
end if;
---------call procedure P1
-----------get output as ret_val1 /*getting ret_val1 as 10 for 02-MAR-2012,03-MAR-2012,05-MAR-2012 */
----------call procedure P2
---------get ouput as ret_val2 /*getting ret_val1 as 20 for 02-MAR-2012,03-MAR-2012,05-MAR-2012 */
if ret_val1>0 0r ret_val2>0
then
l_return_out.has_csts := yes;
l_return_out.cst_present := 10;
l_return_out.cst_absent := 20;
else
l_return_out.has_csts :=No;
l_return_out.cst_present:= 0;
l_return_out.cst_absent := 0;
end if;
end loop;
l_return_out.has_cst := student_rec(i).has_csts;
l_return_out.cst_missing := student_rec(i).cst_present;
l_return_out.cst_existing := student_rec(i).cst_absent;
PIPE ROW(l_return_out);
END LOOP;
RETURN ;
I am expecting this as result
school_date marks_obtained has_csts cst_present cst_absent
01-MAR-2012 20
02-MAR-2012 30 yes 10 20
03-MAR-2012 40 yes 10 20
04-MAR-2012 70
05-MAR-2012 60 yes 10 20
but this as result
school_date marks_obtained has_csts cst_present cst_absent
01-MAR-2012 20
02-MAR-2012 30
03-MAR-2012 40
04-MAR-2012 70
05-MAR-2012 60
Can anybody please highlight the mistake i am doing while processing the logic??
Edited by: 942390 on Jul 13, 2012 8:44 PM
Edited by: 942390 on Jul 13, 2012 8:45 PMI am getting a set values from a record set....student_rec
and on pipelining this record set from 1st till last
i am getting this
school_date marks_obtained has_csts cst_present cst_absent
01-MAR-2012 20
02-MAR-2012 30
03-MAR-2012 40
04-MAR-2012 70
05-MAR-2012 60
so initially has_csts, cst_present and cst_absent is null for all dates.
now have a temp table of selected_dates(which contains these dates 02-MAR-2012,03-MAR-2012,05-MAR-2012)
now I am want to populate has_csts, cst_present and cst_absent with data only for those dates which is present in selected_dates temp table(02-MAR-2012,03-MAR-2012,05-MAR-2012) and that too has_csts, cst_present and cst_absent will be populated with some condition (based on the values from procedure got from P1 and P2).
so want result set to look like
school_date marks_obtained has_csts cst_present cst_absent
01-MAR-2012 20
02-MAR-2012 30 yes 10 20
03-MAR-2012 40 yes 10 20
04-MAR-2012 70
05-MAR-2012 60 yes 10 20
so what could be the possible solution to obtained this.... -
How to loop through records in a block
How do I get the count of records in a data block after the EXECUTE_QUERY?
If I want to manually insert or update records into the table from the datablock how do I loop thru' records in the block.?
Also for 'WHERE' clause of update statement is there anyway to tie up the rowid to the current row in the form.Use get_block_property('block',QUERY_HITS).
For the second question :
Set the properties of block to be INSERT_ALLOWED=FALSE,UPDATE_ALLOWED=FALSE, next do:
go_block('block');
first_record;
set a local handler of errors
loop
next_record;
end loop;
when others then
catch the error "Record must be inserted first...'
For the third question set the DML Returning Value = Yes in property palette of the block and access the rowid as
declare
rowid_ rowid;
begin
rowid_:=:block.rowid;
end;
HTH -
How to insert some records in one table and some records in another table
Interview question
how insert records in two tables by using trigger
CREATE or REPLACE TRIGGER Emp_Ins_Upd_Del_Trig
BEFORE delete or insert or update on EMP
FOR EACH ROW
BEGIN
if UPDATING then
UPDATE emp2
SET
empno = :new.empno,
ename = :new.ename
--, job = :new.job
--, mgr = :new.mgr
--, hiredate = :new.hiredate
, sal = :new.sal
--, comm = :new.comm
--, deptno = :new.deptno;
sdate = :new.sdate,
edate = :new.edate
end if;
if INSERTING then
INSERT INTO emp2
VALUES
( :new.empno
, :new.ename
--, :new.job
--, :new.mgr
--, :new.hiredate
, :new.sal
--, :new.comm
--, :new.deptno
new.sdate,
new.edate);
end if;
if DELETING then
DELETE FROM emp2
WHERE empno = emp2.empno;
end if;
END;
it is working fine but he wants to insert some specific litimit on one table and some specified limit of records in one ..
In this senerio can i insert records by use count of records...
please help me..Can you be more specific on the "Limit"
Conditional insert can be used in this case. -
Data type with large size in a table and updating it
Hi guys,
i am working on a requirement in which i need to create 4 tables. Here, 2 tables will be updated by the user and a program will update this content to the other 2 table.
2 days before i posted a query on this, Previously i stucked on a part where, table need to entries with size char 400 and char 9000. As from the reply, i changed it to STRING with which table was sucessfully created and i was able to update data through a report program.
Now we ned to provide an sm30 option to users, so they can insert new contents and update the status of old contents. But when i tried this table maintanance generator, i got an error that table contains string, so maintanance generator cannot be done ! i changed the data type to rawstring also. Now maintanance generator got saved, but when i open the table in sm30, i am getting dump saying tht table is using string/char etc.
can anyone tell me what data type to use in this scenario with which we can enter data through sm30 ?Hi Kithu,
you can use STRING (or XSTRING) fields. Then there is no limit on the field size. As long as it is not (fully) filled with data, no space is wasted.
The disadvantage is that STRING type data are treated as 'deep' structures. That leads to some trouble in the definition of interfaces. Give it a try!
Regards,
Clemens -
Select records based on criteria and update those records once read
hi,
I am very new to bpel and DB adapters.
I have a requirement where in I need to query two tables to fetch some records and update these selected records with a new value for field to indicate that bpel has processed these records.
Once I select these I needs the output to be mapped to the output variable.
I am able to select the records based on criteria , but how will i lock these records so that these records do not get processed again. This should be a very simple usecase just that I am not aware.
Thanks,
RobinOnce you have finished reading the records fire an update query , update some field in the table so that it does not get picked up next time.
if you are using polling for picking up the records, then use logical delete scenario, refer....http://docs.oracle.com/cd/E15523_01/integration.1111/e10231/adptr_db.htm#BABEEBIH -
Split the records of interanl table and upload it to fields
Hi All,
I have a internal table filled with records in following format.
XXXXXXXX~~Export the invoice
2~19980501~19980531
// The first invoice:
0~00130698114000010004119980512059611000276233.350.1711076.66????321000789010005???????????????????130601000000000??????????18? 3352051????532611-3357211???~~~
0~????????176233.350.1711076.6676233.350~1510
// The second invoice:
0~00130698114000010007219980512059611000440482.000.175882.00????110108078901007?????????61? 68744479?????????????462088-07?????130601000000000??????????18? 3352051????532611-3357211???????????~~~
0~????????139780.000.175780.0039780.000~1510
0~????3.5"10702.000.17102.0070.20~1510
I want to sort this out field by field and fill it to the respective fields.
If i try to do so with work area
read table itgt_frmgto into waitgt_frmgto index 4.
i have a out put of one line that is 4th.Butbbecause these are random records,i cannot count on lines .So i have to go by fields.
Could anybody tell me how to do this with multiple lines coming in without using
index concept.
Thank you,
Priyahi,
loop the table and inside the loop split each line into other variables if the line has a fix structure or split into an another itab if not.
SPLIT <line> AT '~~' INTO <f1> ... <fn>.
or
SPLIT <line> AT '~~' INTO <itab>.
you might want to take a look at the syntax of SPLI statement.
br -
How to loop through columns of a table?
Hi, guys
Is there a way to loop through each column of a table? If there is a way, then how?
I have a table with columns of different datatypes , and I want to set default values for each column with a loop but don't know how to make it happen.
For example,
Table: Employees
declare
rec Employees%ROWTYPE;
begin
for col in rec.empno .. rec.location loop
if col = rec.empno then -- set default value for column empno;
end loop;
end;
/Sorry, I am a newbie to PL/SQL. Please help!
Thanks in advance.
Edited by: HappyJay on 2010/05/11 10:36Hi,
You can query the data dictionary views all_tab_columns (or user_tab_columns) to get the names of all the columns in a particular table.
Here's an easy way (but not a very efficient way) to loop through them:
SET SERVEROUTPUT ON
BEGIN
FOR c IN ( SELECT column_name
FROM all_tab_columns
WHERE owner = 'SCOTT' -- Remember ot use UPPER CASE inside quotes
AND table_name = 'EMP'
LOOP
dbms_output.put_line (c.column_name || ' = column_name inside loop');
END LOOP;
END;
/I'm confused about what you want to do, though.
Do you want the PL/SQL code to write and/or execute an ALTER TABLE command for each column? -
Trying to loop through records
Hi
I trying to loop through my records and write to a database each time, but my code only seems to write the first time through...
//check for the stock levels...
Iterator stockLevels = getItems().iterator();
pstmt = dbConn.prepareStatement(sql_bundle.getString("findStock"));
while (stockLevels.hasNext()) {
CartItem stockL = (CartItem) stockLevels.next();
pstmt.setString(1, stockL.getProduct().getISBN());
ResultSet rscp = pstmt.executeQuery();
if(rscp.next()){
if (rscp.getInt("STOCK_LEVEL") > stockL.getQuantity()) {
int newAmount = rscp.getInt("STOCK_LEVEL") - stockL.getQuantity();
PreparedStatement pstmte = dbConn.prepareStatement(sql_bundle.getString("updateStock"));
pstmte.setInt(1, newAmount);
pstmte.setString(2, stockL.getProduct().getISBN());
pstmte.executeUpdate();
} else {
cat.error("Right Bloddy mess");
throw new Exception();
}else{
throw new Exception("No results from known good query.");
pstmt.close();I get the following error in my browser:
org.apache.jasper.JasperException
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:248)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:289)
and no errors when I compile using javac...
Thanksexcellent my looping is working...
thanks
Maybe you are looking for
-
My iPhone 5 dies at 20-24% it doesn't go all the way to 0%. Will apple replace my phone or fix my phone for free since its hardware problem and I have no money. This is my second iphone 5 replacement. I have no apple care so I'm worried. My phone has
-
Why does the video window keep minimising then opening again?
I'm slowly working through all the problems I am having with my laptop, and this one is just a bit weird - nothing more than an inconvenience, but it sorts itself out. If i am watching a video / TV show in iTunes (opened in new window, not in iTunes
-
when installing third party software, how do I temporarily turn off the factory installed virus sw and firewall? Is it necessary on a Mac to do so? I come from the Windows world and am still in the learning curve on the Mac.
-
Hi All, I am facing a performance related problem. We had a recent hardware upgrade . before the upgrade , during the non peak hours the CPU idle time was 95 % but now (after upgrade) even though the data load and query load remains the same , the c
-
*Hello everybody,* I just bought a eSata PCI card for my MacPro along with a LaCie D2 Quadra. It looks like my drive now takes a bit more time to be mounted by the system which is not really a problem. My issue is actually that some apps installed ar