Need to update multiple columns using another table
I have 2 tables. and i need to update rows of 1 table using another table
Table1
Serial_no. payment_date Payment_amt
101 22/11/2010 150
101 18/03/2011 355
102 15/04/2011 488
103 20/05/2011 178
102 14/06/2011 269
101 28/06/2011 505
Table2
Serial_no Charge_amt Last_paymt_dt Last_paymt_amt
101 255
102 648
103 475
I want to update Last_paymt_dt and Last_paymt_amt of table2 using Table1, I have written following update statement but it gives error that single row subquery return multiple row.
Update Table2
set (Last_paymt_dt,Last_paymt_amt) = (select max(payment_date, payment_amt) from table1
where table1.Serial_no = table2.Serial_no group by payment_amt)
kindly suggest how should i update.
SQL> select * from table1
2 /
SERIAL_NO PAYMENT_DA PAYMENT_AMT
101 22/11/2010 150
101 18/03/2011 355
102 15/04/2011 488
103 20/05/2011 178
102 14/06/2011 269
101 28/06/2011 505
6 rows selected.
SQL> select * from table2
2 /
SERIAL_NO CHARGE_AMT LAST_PAYMT LAST_PAYMT_AMT
101 255
102 648
103 475
SQL> update table2
2 set (last_paymt_dt,last_paymt_amt) = (
3 select max(payment_date),
4 max(payment_amt) keep(dense_rank last order by payment_date)
5 from table1
6 where table1.serial_no = table2.serial_no
7 )
8 /
3 rows updated.
SQL> select * from table2
2 /
SERIAL_NO CHARGE_AMT LAST_PAYMT LAST_PAYMT_AMT
101 255 28/06/2011 505
102 648 14/06/2011 269
103 475 20/05/2011 178
SQL> SY.
Similar Messages
-
How to update multiple columns from different tables using cursor.
Hi,
i have two tables test1 and test2. i want to udpate the column(DEPT_DSCR) of both the tables TEST1 and TEST2 using select for update and current of...using cursor.
I have a code written as follows :
DECLARE
v_mydept1 TEST1.DEPT_CD%TYPE;
v_mydept2 TEST2.DEPT_CD%TYPE;
CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO v_mydept1,v_mydept2;
EXIT WHEN C1%NOTFOUND;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
END LOOP;
COMMIT;
END;
The above code when run says that it runs successfully. But it does not updates the desired columns[DEPT_DSCR].
It only works when we want to update single or multiple columns of same table...i.e. by providing these columns after "FOR UPDATE OF"
I am not sure what is the exact problem when we want to update multiple columns of different tables.
Can anyone help me on this ?oops my mistake.....typo mistake...it should have been as follows --
UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
Now here is the upated PL/SQL code where we are trying to update columns of different tables --
DECLARE
v_mydept1 TEST1.DEPT_CD%TYPE;
v_mydept2 TEST2.DEPT_CD%TYPE;
CURSOR C1 IS SELECT TEST1.DEPT_CD,TEST2.DEPT_CD FROM TEST1,TEST2 WHERE TEST1.DEPT_CD = TEST2.DEPT_CD AND TEST1.DEPT_CD = 'AA' FOR UPDATE OF TEST1.DEPT_DSCR,TEST2.DEPT_DSCR;
BEGIN
OPEN C1;
LOOP
FETCH C1 INTO v_mydept1,v_mydept2;
EXIT WHEN C1%NOTFOUND;
UPDATE TEST1 SET DEPT_DSCR = 'PLSQL1' WHERE CURRENT OF C1;
UPDATE TEST2 SET DEPT_DSCR = 'PLSQL2' WHERE CURRENT OF C1;
END LOOP;
COMMIT;
END;
Please let us know why it is not updating by using using CURRENT OF -
Need to update multiple records using store procedure
Hi i am trying to update multiple records using store procedure but failed to achieve pls help me with this
for example my source is
emp_name sal
abhi 2000
arti 1500
priya 1700
i want to increase salary of emp whose salary is less than 2000 it means rest two salary should get update..using stored procedure only
i have tried following code
create or replace procedure upt_sal(p_sal out emp.sal%type, p_cursor out sys_refcursor)
is
begin
open p_cursor for
select sal into p_sal from emp;
if sal<2000 then
update emp set sal= sal+200;
end i;f
end;
and i have called the procedure using following codes
set serveroutput on
declare
p_sal emp.sal%type;
v_cursor sys_refcursor;
begin
upt_sal(p_sal,v_cursor);
fetch v_cursor into p_sal;
dbms_output.put_line(p_sal);
end;
the program is executing but i should get o/p like this after updating
1700
1900
but i am getting first row only
2000
and record is not upsating...please help me with this
thanksHi Alberto,
thanx for your valuable suggestion. but still i have doubt. the code which i have mentioned above might be simple but what if i have big requirement where i need update the data by using loops and conditional statement.
and i have similar kind of requirement where i need to deal with procedure which returns more than one row
my source is
empno ename salary
111,abhi,300
112,arti,200
111,naveen,600
here i need to write a store procedure which accepts the empno (111) as input para and display ename and salary
here i have written store procedure like this
create or replace procedure show_emp_det(p_empno in emp.empno%type, p_ename out emp.ename%type,p_salary out emp.salary%type, p_cursor out sys_refcursor)
is
begin
open p_cursor for
select ename,salary into p_ename,p_salary from emp where empno=p_empno;
end;
and i have called this by using
declare
p_salary emp.salary%type;
p_ename emp.ename%type
v_cursor sys_refcursor;
begin
show_emp_det(111,p_ename,p_salary,v_cursor);
fetch v_cursor into p_ename,p_salary;
dbms_output.put_line(p_ename);
dbms_output.put_line(p_salary);
end;
here i should get
abhi,300
naveen,600
but i am getting first row only
abhi,300
but i want to fetch both rows...pls help me to find the solution -
How to update the COST column using another table's column
Dear All,
I have table:
table parts: pno, pname, qoh, price, olevel
table orders: ono, cno, eno, received, shipped
table odetails: ono, pno, qty
view:orders_view: ono, cno, eno, received, shipped,sum(qty*price)order_costview:odetails_view: ono, pno, qty, (qty*price)cost
after I update the price in parts, I need to update COST and ORDER_COST too. The orders_view does not have pno, qty, and price, the odetails_view does not have price, how can I update the COST and ORDER_COST. Please help and Thanks in advance!!!
I wrote the update the price in parts:
create or replace procedure change_price(ppno in parts.pno%type, pprice in parts.price%type) as
begin
update parts
set price = pprice
where pno = ppno;
end;
show errorsthis procedure works fine.
I wrote the trigger:
create or replace trigger update_orders_v
after update of price on parts
for each row
begin
update orders_view
set order_cost = sum(parts.(:new.price)*parts.qty)
where parts.pno = :new.pno;
end;
show errorsIt gives me:Errors for TRIGGER UPDATE_ORDERS_V:
LINE/COL ERROR
3/5 PL/SQL SQL Statement ignored
4/22 PL/SQL ORA-00934: group function is not allowed hereplease help!You could add the columns to the tables and then you would need a trigger to update those columns. However, you could just as easily select the price * qty to get the cost, without creating an additional column or trigger. I have no idea what you might want to do with a global temporary table. I think you need to explain what your purpose is, before any of us can suggest the best method. Since I have already demonstrated an update with a view, I will demonstrate an update with the cost column added to the odetails table and a trigger as you asked about. Notice that you will need triggers on both tables, the one that has qty and the one that has price.
scott@ORA92> create table parts
2 (pno number(5) not null primary key,
3 pname varchar2(30),
4 qoh integer check(qoh >= 0),
5 price number(6,2) check(price >= 0.0),
6 olevel integer)
7 /
Table created.
scott@ORA92> create table odetails
2 (ono number(5),
3 pno number(5) references parts,
4 qty integer check(qty > 0),
5 cost number,
6 primary key (ono,pno))
7 /
Table created.
scott@ORA92> create or replace procedure change_price
2 (ppno in parts.pno%type,
3 pprice in parts.price%type)
4 as
5 begin
6 update parts
7 set price = pprice
8 where pno = ppno;
9 end;
10 /
Procedure created.
scott@ORA92> create or replace trigger update_cost1
2 after insert or update of price on parts
3 for each row
4 begin
5 update odetails
6 set cost = qty * :new.price
7 where pno = :new.pno;
8 end update_cost1;
9 /
Trigger created.
scott@ORA92> show errors
No errors.
scott@ORA92> create or replace trigger update_cost2
2 before insert or update of qty on odetails
3 for each row
4 declare
5 v_price parts.price%type;
6 begin
7 select price
8 into v_price
9 from parts
10 where pno = :new.pno;
11 --
12 :new.cost := :new.qty * v_price;
13 end update_cost2;
14 /
Trigger created.
scott@ORA92> show errors
No errors.
scott@ORA92> insert into parts values (1, 'name1', 1, 10, 1)
2 /
1 row created.
scott@ORA92> insert into odetails values (1, 1, 22, null)
2 /
1 row created.
scott@ORA92> -- starting data:
scott@ORA92> select * from parts
2 /
PNO PNAME QOH PRICE OLEVEL
1 name1 1 10 1
scott@ORA92> select * from odetails
2 /
ONO PNO QTY COST
1 1 22 220
scott@ORA92> -- update:
scott@ORA92> execute change_price (1, 11)
PL/SQL procedure successfully completed.
scott@ORA92> -- results:
scott@ORA92> select * from parts
2 /
PNO PNAME QOH PRICE OLEVEL
1 name1 1 11 1
scott@ORA92> select * from odetails
2 /
ONO PNO QTY COST
1 1 22 242
scott@ORA92> -- select works without extra cost column or trigger:
scott@ORA92> select o.ono, o.pno, o.qty, (o.qty * p.price) as cost
2 from odetails o, parts p
3 where o.pno = p.pno
4 /
ONO PNO QTY COST
1 1 22 242
scott@ORA92> -
Need to update corresponding columns in details table
Hi,
I have a header and lines table.
In header table we have a comments field, requirement is whenever someone is updating comments in header it should update lines comment column as well.
I tried using fire action on Comments field like as soon as user is entering something in Comments field I am capturing that event and firing and update statement for lines table
but the problem here is, if user enters the comments and doesn't save it at header level it stills fire an update statement in lines and updating the comment field of lines table.
I need to update header comments in lines level at Apply button.
Please someone suggest something.
Best Regards
Ragni GuptaHi Gurus/Expert,
Please suggest something, its really urgent!!
Thanks
Ragni Gupta -
How to transpose rows into multiple columns using pivot table
I have 1 row containing 12 columns with value "JAN", "FEB", "MAR", "J-1","F-1","M-1","J-2","F-2","M-2","J-3","F-3","M-3"
I want to display as
JAN J-1 F-1 M-1
FEB J-2 F-2 M-2
MAR J-3 F-3 M-3
How do I achieve the above?Today you have only 3 months JAN, FEB, MAR. Is it always the same number of columns. What if there are more months added to this row?
Is your data really coming from relational source or some sort of text file?
There is a better way to do this in narrative view using HTML if your requirement is just to show them in multiple rows and do some calculations.
Go to Narrative View;
In prefix, use <html> <table>
In 'Narrative' text box add something like this
<tr> <td> @1 </td> <td> @4 </td> <td> @7 </td> </tr>
<tr> <td> @2 </td> <td> @5 </td> <td> @8 </td> </tr>
<tr> <td> @3 </td> <td> @6 </td> <td> @9 </td> </tr>
In Suffix, use </table> </html>
You can also add simple calculations like sum etc at the very bottom of these rows as grand totals.
kris -
Need to update one column of all table in that particular schema
hi all,
i am using db10g.
my task is to update one particular column's value of all the table in that schema.
to acheive the above
do i have to write update statement for each table or is there any way to do it in bulk?
i hope my question make sense.
Thanks..Hi karthick,
I want to know something which is not related to this thread .
How to include formatted post while replying to thread .
I know using will be used format code before posting . But how can we do it for posted message .
I hope you understand my question .
Thanks in advance . -
UPDATE multiple columns with conditional SET parameters
I have a procedure that updates multiple columns of a table using the procedure's parameter. Is it possible to have one update statement with conditional SET parameter?
CREATE TABLE TEMP
(POL_NUM NUMBER,
OED DATE,
TERM NUMBER,
TRANS_CD CHAR(2));
INSERT INTO TEMP VALUES (1, '1 AUG 2009', 12, 'NB');
INSERT INTO TEMP VALUES (2, '4 AUG 2009', 12, 'XL');
INSERT INTO TEMP VALUES (3, '2 AUG 2009', 12, 'RN');
COMMIT;
CREATE OR REPLACE PROCEDURE TMP_PROC (
pPOL_NUM NUMBER,
pOED IN DATE,
pTERM IN NUMBER,
pTRANS_CD CHAR2)
AS
BEGIN
IF pOED IS NOT NULL THEN
UPDATE TEMP SET OED = pOED WHERE POL_NUM = pPOL_NUM;
END IF;
IF pTERM IS NOT NULL THEN
UPDATE TEMP SET TERM = pTERM WHERE POL_NUM = pPOL_NUM;
END IF;
IF pTRAN_CD IS NOT NULL THEN
UPDATE TEMP SET TRANS_CD = pTRANS_CD WHERE POL_NUM = pPOL_NUM;
END IF;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;Is it possible to replace multiple IFs from the code to have only one UPDATE statement with condition that update the column only if the passed parameter is not null? In real scenario I have more than 3 columns and I don't want to write many IF blocks.
Please help Gurus!!
Edited by: Kuul13 on Sep 18, 2009 1:26 PMHi,
You certainly don't want to issue separate UPDATE statements for every column; that will be really inefficent.
SQL has several ways to implement IF-THEN-ELSE logic. CASE is the most versatile, but NVL will do everything you need for this job. You can use one of those to set a column to itself (and therefore not really update that column) when appropriate.
For example:
CREATE OR REPLACE PROCEDURE TMP_PROC (
pPOL_NUM IN NUMBER,
pOED IN DATE,
pTERM IN NUMBER,
pTRANS_CD IN CHAR
AS
BEGIN
UPDATE temp
SET oed = NVL (poed, oed)
, term = NVL (pterm, term)
, trans_cd = NVL (ptrans_cd, trans_cd)
WHERE pol_num = ppol_num;
COMMIT; -- Maybe
END tmp_proc;"EXCEPTION WHEN OTHERS THEN NULL" is almost always a bad idea. If there's an error, don't you want to know about it? Shouldn't you at least log a message in a warnings table or something?
Think careflully about whether or not you want to COMMIT every time you call this procedure.
Just as it's inefficient to issue a separate UPDATE statement for every column, it's also inefficient to issue a separate UPDATE statement for every row. If efficiency is important, it should be possible to UPDATE several rows in a single UPDATE statement, using NVL (or CASE, or COALESCE, or NULLIF, or NVL2, or ...).
This was a very well-written question! Thanks for providing the CREATE TABLE and INSERT statements, and such a clear explanation. -
Mapping multiple columns of a table to single dimension using odi
Hi John,
Can we map multiple columns of a table to a single dimnesion?
For example, in RDBMS, for the employee details, Grade position etc will be in different columns, and in Planning these would be as members of one dimension.
So while loading data from oracle to essbase can we map these multiple columns to single dimension?
If yes how?Hi,
In your staging area/target you can concatentate the columns.
So in your interface and on your target datastore, pick the column which is going to hold the details of the concatenation.
Then in the expression editor use the CONCAT function, or you could use ||
eg CONCAT(<sourceCol1>, <sourceCol2>)
or <sourceCol1> || <sourceCol2>
obviously you need to change the information between <sourceCol1> to your source datastore column
Cheers
John
http://john-goodwin.blogspot.com/ -
How to use a USER_DATASTORE to index multiple columns in different tables
I would appreciate if somebody can give an example or point to links with examples on how to use USER_DATASTORE on multiple columns in different tables. THe Oracle Text documentation only shows multiple columns in the same table.
Thanks in advance.I am not sure why your getting the wrong results but you should seriously reconsider the approach your are taking. Using functions like this is very ineffecient and should be avoided at all cost.
-
Sort functionality using MULTIPLE columns in a table control
Hi all,
I have a custom screen with table control.Now i need to provide SORT functinality in this screen for the columns in the table control.
My questins:
1.Is it possible to seelct MULTIPLE columns in a table control for SORTING?If yes,what explicit settings do i need to do while creatng the TABEL CONTROL in the screen?DO I need to select "Column selection " as MULTIPLE??
2.How do I write the code for SORT functinonality for multiple columns?
I know how to write the code for SORTING on basis of single column .
Thanks!Hi Rob,
Thanks for the reply.
However I was thinking to apply the same logic as for single columns as follows:
types : begin of ty_fields,
c_fieldname(20),
end of ty_fields.
data : t_fields type table of ty_fields,
wa_fields like line of t_fields.
WHEN 'SORTUP'.(Ascending)
loop at TABLE tc01-cols INTO wa_tc01 where selected = 'X'.
SPLIT wa_tc01-screen-name AT '-' INTO g_help g_fieldname.
wa_fields-c_fieldname = g_fieldname.
append wa_fields to t_fields.
endloop.
describe table t_fields lines l_index.
c_count = 1.
if c_count <= l_index.
read table t_fields into wa_fields index c_count.
case c_count.
when '1'.
l_field1 = wa_fields-c_fieldname.
when '2'.
l_field2 = wa_fields-c_fieldname.
and so on depending on the no of columns in the table control...
endcase.
endif.
SORT t_tvbdpl_scr BY l_fields1 l_fields 2......l_fieldn.
Let me know if the above method will work!Also for the above method to work will the type of fields(columns on whihc sort function will be applied) matter???
Thanks again for your time. -
Need to compare values in two columns of one table against values in two columns in another table
Hi, as the title reads, I'm looking for an approach that will allow me to compare values in two columns of one table against values in two columns in another table.
Say, for instance, here are my tables:
Table1:
Server,Login
ABCDEF,JOHN
ABCDEF,JANE
FEDCBA,SEAN
FEDCBA,SHAWN
Table2:
Server,Login
ABCDEF,JOHN
ABCDEF,JANE
FEDCBA,SHAWN
In comparing the two tables, I'd like my query to report the rows in table1 NOT found in table2. In this case, it'll be the 3rd row of table one:
Server,Login
FEDCBA,SEAN
Thanks.create table Table1([Server] varchar(50), Login varchar(50))
Insert into Table1 values ('ABCDEF','JOHN'),('ABCDEF','JANE'),('FEDCBA','SEAN'),('FEDCBA','SHAWN')
create table Table2([Server] varchar(50), Login varchar(50))
Insert into Table2 values ('ABCDEF','JOHN'),('ABCDEF','JANE'), ('FEDCBA','SHAWN')
select [Server] ,Login from Table1
Except
select [Server] ,Login from Table2
select [Server] ,Login from Table1 t1
where not exists(Select 1 from Table2 where t1.[Server] = t1.[Server] AND Login=t1.Login)
drop table Table1,Table2 -
I am unable to sort multiple columns in a table created in Pages.
I had been using Appleworks up until I installed Lion and have now switched to iWork. I created a table within a Pages document and am able to sort a single column (using the Table Inspector and choosing Sort from Edit Rows and Columns) but the Sort option is grayed out when I attempt to sort multiple columns.
In another post, someone talked about this being a problem if you have merged fields. I do not believe I have done this (to be honest I don't know the function of merging fields).
This is very frustrating as I was easily able to sort these tables in Appleworks.Sharon Anderson wrote:
Thanks for your quick response! I have been trying that but then found that Numbers would only let me print in landscape view so I had to paste the table back into Pages. Is there a way to print in portrat view (from Numbers?)
Not so. In the lower left corner of the window, there's an icon that looks like a piece of paper. If you see this:
you are in Sheet View, or normal, mode. If you see this:
You are in Print View mode. Now you see the icons for portrait and landscape modes. Click your choice. Then arrange your content to fit the pages as you wish.
Jerry -
Update Multiple Columns when concerned about redo/undo log sizes.
Hi ,
I have update statements that updates multiple columns at once if any of them is changed. What I see that even though the value of column is not changed it still increases the redo size.
Below is a sample code similar to the ones in my code. Basically I check whether there is a difference in any of the columns to be updated and update all of them.
Is there a way to improve redo log size without splitting the update statement for every column that I will be updating. Redo/Undo log size is a concern for us..
For i In 1.rec.Count Loop
Update employees e
Set e.first_name = rec(i).first_name, e.last_name = rec(i).last_name
Where e.first_name != rec(i).first_name
Or e.last_name != rec(i).last_name;
End Loop;My database is 10g.Muhammed Soyer wrote:
Redo/Undo log size is a concern for us..You are worried about the wrong thing.
If you are concerned about the amount of undo and redo, you should be less concerned about the small diffrence between updating 1 or 3 columns and remove the loop that is contributing to a massive increase in both undo and redo.
Re: global temporary table row order
Name Run1 Run2 Diff
STAT...undo change vector size 240,500 6,802,736 6,562,236
STAT...redo size 1,566,136 24,504,020 22,937,884Run2 shows what adding a loop to a regular SQL statement will do to undo and redo. It made the redo used 15 times greater and the undo almost 30 times greater. -
Update some columns using case....
Hi ,
Is it possible to update some columns using case statement...????
For example when col1 is null then update to a value 'x' else update it to the value '*' , when col2 is null then update to a value y else update it to compute the running total up to that time....
This update statement is contained in db packaged procedure and it receives the values...as parameters....
How can i write down this update statement...?????
Many thanks,
SimonHi ,
Cant' it be used for two or more columns that have to be updated....????
i mean
update table set col_a = case when col_a is null then col_a else '*',
col_b = case when col_b is null then col_b else col_b+col_b_var
end
where .....The above in bold is running total.... This update is defined in a procedure and it receives numbers as parameters, so the need is to add them for every record it receives...., that's why i set above col_b+col_b_var... where col_b_var is the parameter of the procedure....
SORRY...IT IS POSSIBLE.....
Thanks , a lot
Simon
Message was edited by:
sgalaxy
Maybe you are looking for
-
HT1688 How to factory unlock iphone4 at&t carrier
My iPhone 4 is not working in my country I need to unlock it!!! Please help
-
HT1369 i cant find my ipod shuffle on itunes
i am trying to download music to my ipod use itunes for windows 7 and i cant find my ipod in itunes
-
HI Abapers, Please help me out from the following situation... Here i have one screen called 1000. in that i hav to include 2 table controls . so is it possible to use 2 table controls in a single screen. If possible please give me one exampl
-
I recently found some Google and Youtube search results on my boyfriend's computer in Firefox that were "questionable" and when I asked him about them, he adamantly denied knowing what they were and said he recently had some viruses on his computer a
-
Template Technical Spec for a Dialog Program
Hi guys, I need a template for a technical spec of program using the screen painter (an example would be very welcome as well), can any one send me some dox about this? my email is [[email protected]] .Thank you