PL/SQL :: Merging And Updating Rows

Jun 29, 2013

I have a table structure as follows  

Student(Id,First_Name, Last_Name, email, Contact, Address1,Address2,City,Edit_Date,Create_Date,Archived)  

Now if there is more than one row with same email the one with the latest edit date should be updated with missing fields by using same field value other rows (if the field is present in more than one row, the one with the next latest edit date is to be considered) and the archived status of all rows with same email except this master row must be set to 1.

The Create_Date must be set to the minimum of all the create_date values of rows with same email value The create table would be as follows:

CREATE TABLE student(Id NUMBER PRIMARY KEY,first_name VARCHAR2(30) NOT NULL,last_name VARCHAR2(30) NOT NULL,email VARCHAR2(30) NOT NULL,contact NUMBER,adress1 VARCHAR(30),adress2 VARCHAR(30),city VARCHAR(30),edit_date DATE,create_date DATE,archived CHAR(1))

 Sample insert statements would be: insert into student values

View 3 Replies


ADVERTISEMENT

SQL & PL/SQL :: Merging Rows

Oct 17, 2011

there are 2 different types of bar-code which get store in single table

table_structure
item_id
group
bar_code

values are like

item_id group bar_code
1 FG 09878
1 FG 81011

i want output like

item_id group bar_code1 bar_code2
1 FG 09878 81011

What should i need to do...?

View 7 Replies View Related

Updating Millions Of Rows In A Batch

Nov 9, 2011

I have a table which has plenty of rows. In production, I would estimate it to be from 30 millions to 300 millions. I need to update on column (flag) in all the rows (created before certain date).Now saying just:

UPDATE MyTable SET flag = 3 WHERE created < to_date('2010-10-08 23:59:59', 'YY-MM-DD HH24:MI:SS');
COMMIT;

Does not seem like a good idea - the commit-buffer would become too big.I will write a PL/SQL script for this. The question is, whether I should:

a) Update each row separately, and commit after every 10000 rows. ( WHERE RowId = [rowId] )
b) Update 10000 rows with set of dates ( WHERE rowId > [some_row_id] AND RowId < [some_row_id_2]

In the latter example the some_row_ids would naturally be fetched. The rowIds come from sequence. So which one would be more effective?I am not too familiar with PL/SQL or Oracle for that matter.

View 2 Replies View Related

Converting Rows Into Columns And Updating A Table?

Apr 26, 2012

I have a table A, whose table structure is in the below format.

Table A

ID DESC VALUE
123 A 454
123 B 1111
123 C 111
123 D 222
124 A 123
124 B 1
124 C 111
124 D 44

Now i need to insert the data from this table to another table B, the sturcture of which is as below

Table B

ID A B C D
1234541111111222
124123111144

How do i frame a query to fetch data from table A and insert that into table B? I don't want to use max and decode combination. as it would return only single row for an ID. I need all the id's to be displayed.

View 1 Replies View Related

SQL & PL/SQL :: Locking Views (rows Of Data While Updating)

Nov 10, 2011

I have a set of rows based on a complex view from multiple table.

I will be updating some of its columns from front-end . Is there any possible ways to lock those rows of data while updating and no other users can update it;

View 5 Replies View Related

SQL & PL/SQL :: Finding Identical Rows And Updating Records?

May 30, 2013

I need to find the identical rows in the below table based on ID column and update the previous identical record's end_date with latest record's start_date-1.

"ID" "NAME" "START_DATE" "END_DATE"
1 "a" 05-MAR-10 31-DEC-99
1 "B" 30-MAY-12 31-DEC-99
1 "C" 30-MAY-13 31-DEC-99
2 "A" 02-APR-10 31-DEC-99
2 "B" 02-APR-10 31-DEC-99
2 "C" 30-MAY-12 31-DEC-99
3 "C" 04-MAR-10 31-DEC-99

Result should be like below format..
"ID" "NAME" "START_DATE" "END_DATE"
1 "a" 05-MAR-10 29-MAY-12
1 "B" 30-MAY-12 29-MAY-13
1 "C" 30-MAY-13 31-DEC-99
2 "A" 02-APR-10 01-APR-10
2 "B" 02-APR-10 29-MAY-12
2 "C" 30-MAY-12 31-DEC-99
3 "C" 04-MAR-10 31-DEC-99

View 7 Replies View Related

SQL & PL/SQL :: Updating Multiple ROWS With Different Values Using Single Statement?

Feb 16, 2011

The requirement I have is :

I have two tables eim_asset and eim_asset1.I want to update the table eim_asset1 using the following update SQL (Or Logic)

update eim_asset1
set emp_emp_login = (select login from s_user where row_id in
(select row_id from s_emp_per where row_id in
(select pr_emp_id from s_postn where row_id in
(select position_id from s_accnt_postn where ou_ext_id in
(select row_id from s_org_ext where row_id in
(select owner_accnt_id from s_asset where owner_accnt_id is not null)))))

It gives me the ORA error : ORA-01427:single-row subquery returns more than one row.know why I am getting it, because of the one-to-many relationship between owner accounts and their assets.

View 1 Replies View Related

SQL & PL/SQL :: Updating Multiple Rows With Single Update Statement?

Aug 20, 2013

create table temp_tst
(
FILENAME VARCHAR2(200),
EDITED_BY VARCHAR2(50),
EDITED_TO VARCHAR2(50)
)

[code]....

Can I write a single update statement to update filename column replacing "_tst" with "_check"?

View 1 Replies View Related

Updating Rows To Highest Security Level Of Missions Of Same Type

Jan 25, 2009

I am trying to update the security_level of a mission to the highest security level of missions of the same type.

Attributes of the missions table:

mission_id, code_name, mission_type_id, mission_date, security_level

The following is an intermediate output.

MISSION_ID MISSION_TYPE_ID SECURITY_LEVEL
318 3 6
329 3 2
286 5 6
521 5 3
281 6 4
396 7 3
331 8 4
14 9 4
230 9 0
486 10 2

The maximum output for each mission_type_id

MAX_LEVEL TYPE
6 3
6 5
4 6
3 7
4 8
4 9
2 10

According to this 3 records (329,521 and 230) should update.

But my code returns an error.
ERROR at line 4:
cannot update (......"SECURITY_LEVEL") to NULL

this is my code

UPDATE
AM_X_442_2 amx
SET
Amx.SECURITY_LEVEL =
(
select
max_level
[code].........

I have intended the query to make it legible but it removes all spaces.

View 8 Replies View Related

SQL & PL/SQL :: Merging Two Schema In One?

Feb 5, 2012

Requirement of merging two schema's into one. I have a client in two diffrent location . intially we setup the application with two diffrent instances of database for them for a smooth opration as they were not having any connectivity between the branches. Now they are moving their both branches together as one organisation. My application database table structure is same in both places.

View 21 Replies View Related

SQL & PL/SQL :: Merging Text For Same ID

Aug 18, 2010

I understand you can do this using cursors, but i need it in plain SQL; so that I can make this a correlated sub-query to another table using the id column.

Here's the original table:

id subid text
-- ----- ----
1 1 red
1 2 blue
1 3 green
2 1 yellow
2 2 black
2 3 orange

result should be:

id text
-- ----
1 red,blue,green
2 yellow,black,orange

SQL to Create and populate the table:

CREATE TABLE testStringJoin(ID number, subid number,text varchar2(50));
INSERT INTO testStringJoin values(1,1,'red');
INSERT INTO testStringJoin values(1,2,'blue');
INSERT INTO testStringJoin values(1,3,'green');
INSERT INTO testStringJoin values(2,1,'yellow');
INSERT INTO testStringJoin values(2,2,'black');
INSERT INTO testStringJoin values(2,3,'orange');

View 4 Replies View Related

SQL & PL/SQL :: Merging Into Multiple Tables

Feb 12, 2012

Can I use MERGE statement to insert the same thing into multiple tables?

Like below--

Merge into tbl1
using tbl2
when matched then update...
when not matched then insert all.

View 6 Replies View Related

SQL & PL/SQL :: Merging All Subpartitions In One Partition?

Nov 21, 2012

way to delete my subpartitions by keeping my datas, and keep it into my partitions. In fact, i want to remove my subpartitions, and keep my table partitionning. I already remove my subpartition template, but i don't want to do an insert as select on a new table wich will be partitionned (without subpartitions). ALTER TABLE myTable SET SUBPARTITION TEMPLATE ();

View 6 Replies View Related

PL/SQL :: Merging Two Queries Into A Single

Mar 19, 2013

Below are two queries , in merging two queries into a single so that out put coveres out put from below two queries

create table t_look as
select 1 a, 2 b, 3 c from dual union all
select 1 a, 20 b, 3 c from dual union all
select 1 a, 21 b, 3 c from dual union all

[Code]....

View 12 Replies View Related

SQL & PL/SQL :: Compare Current Rows With Previous Rows

Apr 12, 2013

The Table having 3 columns

col0col1 , col2
P112
P112
P102
P113
P115

I want to retrieve the changes rows only.

Output like
col0col1 , col2
P112
P102
P113
P115

View 5 Replies View Related

Updating Table With Same Key?

Nov 19, 2008

I am using oracle 9i, and having trouble with updating a table.

I get ORA-00001(unique constraint) Error on executing the sql below; I know sql below is little strange( which use unique key in 'SET' statement) . but It worked on My Oracle Server. but it didn't on Client's.

why this error occurs or why this error did not occur on my PC,

[Update sql](key is CD and SDATE)
Update TBL1
set CD = 'A',

[Code]....

View 4 Replies View Related

Updating Using Subqueries

Oct 26, 2010

i was just working on one of my SQL assignments from my database management course, and thus far, this is the first that I just can't figure out. The question is:

Quote: Increase the credit limit of any customer who has any order that exceeds their credit limit. The new credit limit should be set to their maximim order amount plus $1,000. This must be done in 1 SQL statement

The bolded part is what I'm having trouble with.

What I have thus far:

UPDATE Customers
SET    CreditLimit = 1000 + (SELECT MAX(Amount) FROM Orders,  Customers WHERE Cust = CustNum)
WHERE CustNum IN (   
                   SELECT Cust
                   FROM Orders
                   WHERE Cust = CustNum
                   AND CreditLimit < Amount); 

So there's two tables that I'll be working with, Customers (the table I'm updating), and Orders (the table where the order amount is found). With the code I have so far, it does seem to be updating the correct tables at the very least, but not with the correct values. It's essentially updating the CreditLimit column with the new value of 1000 + the maximum amount in the order table, which is very close to what I want it to do, but I want it to be 1000 + the maximum amount for that specific customer.

CustNum is the primary key for the Customers table, and Cust is the foreign key that links each together.

(about the formatting, it looked much prettier in SQL Worksheet Plus)

View 8 Replies View Related

SQL & PL/SQL :: Updating CLOB Value?

Nov 9, 2011

create table top_uid(oldUID number,newUID number);

select * from top_uid;

OLDUID NEWUID
---------------------- ----------------------
1 1001
2 1002
3 1003
4 1004

create table topdUIDXML (uidinfo clob);

insert into topdUIDXML select '<filter name="test" topologyUID="1">' from dual;
insert into topdUIDXML select '<filter name="test2" topologyUID="2">' from dual;
insert into topdUIDXML select '<filter name="ftest" topologyUID="3">' from dual;
insert into topdUIDXML select '<filter name="qtest" topologyUID="4">' from dual;

select * from topdUIDXML

UIDINFO
---------------------------------------
<filter name="test" topologyUID="1">
<filter name="test" topologyUID="2">
<filter name="test" topologyUID="3">
<filter name="test" topologyUID="4">

the topdUIDXML table will contain the oldUID's in the clob XML. need to update the topologyUID in that topdUIDXML with the newUID from the top_uid.

View 5 Replies View Related

SQL & PL/SQL :: Updating Column In Every Row Except For The First

Jan 16, 2012

I am trying to find a way to update all of the rows in a table for a column EXCEPT for the very first row. I am not sure if this can be done while I enter my SET parameter or not. I have also thought about using an EXCEPTION in a stored procedure. For example, say I have the table listed below:

SQL> select * from example1;

CODE1 I_ID CODE2 J_ID NAME1 DATE1
----- -------------------- ----- -------------------- -------------------------------- ---------------
A 100 A 200 John 20111225
A 100 A 300 John 20111225
A 100 A 500 John 20111225
A 100 A 400 John 20111225
A 100 A 250 John 20111225
A 100 A 700 John 20111225
A 100 A 800 John 20111225
A 100 A 900 John 20111225
A 100 A 1000 John 20111225
A 100 A 1150 John 20111225
A 100 A 1275 John 20111225
A 100 A 3000 John 20111225

12 rows selected

I am wanting to update the table so that if there were more than 3 J_id's on the table for the same I_id then it will set all of the code1's and code2's to a C except for the very first one. Such as:

SQL> select * from example2;

CODE1 I_ID CODE2 J_ID NAME1 DATE1
----- -------------------- ----- -------------------- -------------------------------- ----------------
A 100 A 200 John 20111225
C 100 C 300 John 20111225
C 100 C 500 John 20111225
C 100 C 400 John 20111225
C 100 C 250 John 20111225
C 100 C 700 John 20111225
C 100 C 800 John 20111225
C 100 C 900 John 20111225
C 100 C 1000 John 20111225
C 100 C 1150 John 20111225
C 100 C 1275 John 20111225
C 100 C 3000 John 20111225

12 rows selected

I have done some searches and haven't seen any results.

View 12 Replies View Related

SQL & PL/SQL :: Updating CLOB

Apr 21, 2011

Ok, CLOB columns are such a hassle.

I have a variable in my script: v_field1 VARCHAR2(32000);

This is part of a cursor record:

v_mf_table IS TABLE OF mf_detail%ROWTYPE INDEX BY BINARY_INTEGER;
v_mf_record v_mf_table;

I use a FORALL to insert the data into a table:

FORALL x IN v_mf_record.FIRST .. v_mf_record.COUNT
INSERT INTO monthly_mf_snapshot VALUES v_mf_record(x);

BUT! v_field1 is > 4000 characters. Does this trash my changes of using FORALL? Do I need to deal with 4k chunks in an UPDATE instead?

View 30 Replies View Related

SQL & PL/SQL :: Updating On Select?

Jun 15, 2010

I have two tables,
CREATE TABLE repos
(
rep_key VARCHAR(10) NOT NULL,
base_term VARCHAR(100) NOT NULL,
blt_key INTEGER NOT NULL

[code]...

gloss table has the unique set of base_term as in repos. BLT_KEY will be primary_key in gloss and foreign key in repos.

Data in gloss table
BLT_KEY BASE_TERM

1 base1
2 base2
3 base3

Now, I need to update the BLT_KEY in gloss to matching entries in repos. Can I do that in a update on select statement? like,
UPDATE repos
SET blt_key = (SELECT gloss.blt_key
FROM repos,
gloss
WHERE repos.base_term = gloss.abase_term) This throws subquery returns more than one row.

And the end of update the repos table should look like,
REP_KEY BASE_TERM BLT_KEY

M001 base1 1
M002 base1 1
M003 base2 2
M004 base1 1

[code]....

Also I need a single query which can update on select as the no of records to be updated are more than 90000 in repos. So two step process would slow down the process

View 13 Replies View Related

PL/SQL :: Updating Records With Different IDs

Jun 6, 2012

Im looking for the posibility to update some records using new id with the column values with another id

example

the table contains these records:

id    gross      net

========================
7     0,1     0,0507749
8     0,2     0,1015499
9     0,5     0,2538748
10     0,83     0,4214
11     0,85     0,4315873

[Code]....

and I would like insert the same gross and net column values of ids 7 to 16 into columns with the ids 40 to 49 in the same order. therefore I would like to obtain the result that I describe below:

id    gross      net

========================
7     0,1     0,0507749
8     0,2     0,1015499
9     0,5     0,2538748
10     0,83     0,4214
11     0,85     0,4315873

[Code]....

View 4 Replies View Related

Updating Two Table Via SQL Loader

Jun 27, 2011

I am trying to write a control file that will read information from two INFILES and update two tables with the different information via SQL Loader. I am using Oracle 11g on Linux. i am not sure how to take the result from the first insert query and use it as input to the second insert query. Currently I have the following control file:

LOAD DATA
INFILE 'table1.dat'
INFILE 'table2.dat'
APPEND
INTO TABLE table1
WHEN tid='1'
FIELDS TERMINATED BY ","
OPTIONALLY ENCLOSED BY '"'
(pid "pid.nextval", p_fname, p_lname, tid)
[code]....

The pid in the second insert query should be taken from the pid generated in the first insert query. However, I do not know how to do this. Does SQL Loader run the first insert query for all entries in the table1.dat file and then the second insert query or will it do one record at a time? Here are my INFILES:

table1.dat
,John,Doe,1
,Joe,Smith,1

table2.dat
10,,abc,1,
11,,xyz,1,

The second field in table2.dat should be taken from the result of creating a record in the table1.dat. Is this something that can be achieved using SQL Loader? The first part of the control file is successful, I can see the table being updated with the contents on table1.dat, but the second query fails.

View 1 Replies View Related

Updating 15 Million Records

Jul 12, 2012

i'm using the below query to update a VOTER table with over 15million records but it's taking ages to finish. i am using 11gr2 on linux

the query:

MERGE INTO voter dst
USING (
SELECT voterid,
pollingstation || CASE
WHEN ROW_NUMBER () OVER ( PARTITION BY pollingstation
[code]........

View 6 Replies View Related

SQL & PL/SQL :: Updating View Using Instead Of Trigger

Jul 11, 2013

I have two different tables having similar structure but data is coming from different source.finally i want to update the view so that the it should affect the base table.

create table emp1 as
select empno,ename,job,deptno,sal from emp where deptno=10;
create table emp2 as
select empno,ename,job,deptno,sal from emp where deptno=20;
create view emp_view as select * from emp1 union all select * from emp2;
[code].......

View 13 Replies View Related

SQL & PL/SQL :: Not Updating All Records In 1st Commit

Nov 22, 2011

I have made a correlated update statement using rowid. Find my attachment. Its updating all columns which i wanted but issue is that its not updating in 1st commit.

Suppose 6 rows is to be updated, then in 1st commit its updating 1 record, then in 2nd commit its updating 2nd record and so on. And in Toad its showing 6 rows updated in 1st commit, then 5 rows updated in 2nd commit and 1 rows updated in last record. I want that all records to be updated in first commit only.

View 4 Replies View Related

SQL & PL/SQL :: Updating Nth Row And Adding Column

Aug 7, 2012

I had created a table which have 100s' of entries in it.

create table reg_user
(
USERNAME VARCHAR2(50),
PASSWORD VARCHAR2(20)
)
***

[Code]..

1. In this table, how to update the Nth row, how can I do it

2. Now I need to add USERID in this table,which will get value from 1 to max no. of rows. I do not want to drop the table and again re create it adding USERID or update each row manually. Is there any other way to add USERID and have IDs from 1 to max IDs.

View 30 Replies View Related

SQL & PL/SQL :: Trigger Updating Second Table With PK Value

Oct 20, 2011

Oracle 11.2 - The goal is to create a trigger on table and anytime an update, delete or insert is done on the table, write values to a second table. I have the trigger and it works except it is not loading my col1/PK values. I understand I need to do a new/old value. Col1 is my PK on Table that I want to load anytime there is an update/delete/insert on the table. How do I code the old/new variable?

My
CREATE OR REPLACE TRIGGER TRIGGER_NAME
AFTER INSERT OR UPDATE OR DELETE
ON TABLE_NAME
FOR EACH ROW
DECLARE
v_col1 TABLE_NAME.COLUMN%TYPE;
BEGIN
[code]...

View 5 Replies View Related

SQL & PL/SQL :: Updating A File With UTL_FILE

May 3, 2010

I have an application that creates files using the utl_file package. It works fine but one of the lines in the file should hold the number of bytes in the file (on a line formatted like 'FileSize: 2104'). Unfortunately this line is not the last line of the file and the lines that follow it are variable in length.

My approach therefore is as follows:
1. Write the 'FileSize: ' line during file creation.
2. Write the remaining lines of data to the file (but don't close it).
3. Use utl_fgetattr to find the file size.
4. Go back and find the 'FileSize' line I need to update, using get_line to read it into the buffer.
5. Append the filesize (plus the number of characters that the variable I use to store the filesize value) to the buffer string.
6. Write the line back to the file using put_line.

However I keep getting invalid file operation errors however I try to re-access the file...

PROCEDURE setUtlFileSize (pFileName IN VARCHAR2,
pFileHandle IN utl_file.file_type) IS
vbFileExists BOOLEAN;
viFileLen NUMBER;
viFileBlockSize NUMBER;
vsFileRecord VARCHAR2(2000);
[code].......

This gives me the following output.... (for two files)... currently I am using some generic exception handling just to show me the error.

File Length: 2106
File Position: 0
-29283,ORA-29283: invalid file operation
File Length: 497
File Position: 0
-29283,ORA-29283: invalid file operation
Process exited.

Two things here concern me: The File Position suggests that the current offset position is set to the start of the file... but since I had not closed or performed any other operation since the last put_line and fflush (which are used to add lines to the file) I had expected that the offset position would be the same as the file length?

Secondly: Even if the position had been reset to the start of the file I don't understand why the get_line gives me the oracle error.

View 1 Replies View Related

SQL & PL/SQL :: Updating Old Values In Trigger?

Apr 16, 2013

I am learning oracle trigger, i have one query.

Can i increment the old column value in trigger.

eg: :new.cid := :old.cid+1;

is this is correct.

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved