SQL loader Help please [message #122950] |
Thu, 09 June 2005 06:05 |
Shania
Messages: 16 Registered: March 2005
|
Junior Member |
|
|
Our firm decided to change the department code as part of resturcturing. I have an excel sheet containing Old dept ID,dept prefix and dept suffix.New Dept code is obtained by concatening Dept_prefix and Dept_Suffix.
old_Dept_ID Dept_prefix Dept_Suffix
I005 110 I89078
I006 111 I90908
Now, I need to update couple of tables to reflect the change. Basically, I need to replace old dept code with new dept code.
I cannot do this manually as there are around 160 depts.
I am thinking of using SQL loader for this. Can some one suggest me how to do this using SQL loader or any better methods please?
Thanks so much
Shania
|
|
|
|
Re: SQL loader Help please [message #122977 is a reply to message #122955] |
Thu, 09 June 2005 07:26 |
Shania
Messages: 16 Registered: March 2005
|
Junior Member |
|
|
Thanks for quick reply.
I am quite new to Oracle 9i. I had a quick look at external tables, but I am quite confused on how to use loader with external tables and how to use an update based on old dept ID.
I would highly appreciate if you could provide me an example to use in my scenario. Thanks again.
|
|
|
|
Re: SQL loader Help please [message #123013 is a reply to message #123006] |
Thu, 09 June 2005 10:09 |
Shania
Messages: 16 Registered: March 2005
|
Junior Member |
|
|
Thanks a lot Mahesh for the links.
So, in this case, all I can do is use the SQL loader to load old and new dept codes to a staging table using SQL loader and then using PL/SQL update the dept codes in tables to replace old dept codes.
Am I right in my understanding ?
|
|
|
|
Re: SQL loader Help please [message #123181 is a reply to message #123016] |
Fri, 10 June 2005 07:30 |
Shania
Messages: 16 Registered: March 2005
|
Junior Member |
|
|
I have loaded new and old dept ids into staging table (DEPT_TEMP)using SQL loader.
I am trying to use Merge statement to upsert recs to existing dept table (dept_int).
While trying to execute MERGE stmt. I am getting following error.
ERROR at line 4:
ORA-00904: "C"."DEPT_ID": invalid identifier
Could you please suggest me where I might be wrong?
Thank you so much
CREATE TABLE DEPT_TEMP
( OLD_DEPT_ID VARCHAR2(300 BYTE),
DESCRIPTION VARCHAR2(300 BYTE),
NEW_DEPT_ID VARCHAR2(200 BYTE),
)
DESC DEPT_INT
DEPT_ID VARCHAR2(200 BYTE),
DESCRIPTION VARCHAR2(300 BYTE)
MERGE INTO DEPT_INT C
USING ( SELECT *
FROM DEPT_TEMP) S
ON ( C.DEPT_ID = S.OLD_DEPT_ID)
WHEN MATCHED THEN
UPDATE SET
C.DEPT_ID = S.NEW_DEPT_ID,
C.DESCRIPTION = S.DESCRIPTION
WHEN NOT MATCHED THEN
INSERT
( DEPT_ID
,DESCRIPTION
)
VALUES
( S.NEW_DEPT_ID
,S.DESCRIPTION
) ;
ERROR at line 4:
ORA-00904: "C"."DEPT_ID": invalid identifier
|
|
|
|
|
Re: SQL loader Help please [message #123231 is a reply to message #122950] |
Fri, 10 June 2005 11:30 |
smartin
Messages: 1803 Registered: March 2005 Location: Jacksonville, Florida
|
Senior Member |
|
|
Is this what you had in mind?
-- Note, this example assumes that there is enough room in the dept_code column
-- to hold the newer, larger, values for the dept_codes. If not, need to first
-- alter the data table to increase the size of the dept_code column. Also
-- note to be careful of primary and foreign key constraints (test this in dev).
MYDBA@ORCL >
MYDBA@ORCL > start update_codes;
MYDBA@ORCL >
MYDBA@ORCL > create table data
2 (
3 dept_code varchar2(20),
4 data varchar2(50)
5 );
Table created.
MYDBA@ORCL >
MYDBA@ORCL > insert into data
2 select mod(rownum,3)+1, rownum || 'blahblah' from all_objects where rownum <= 10;
10 rows created.
MYDBA@ORCL >
MYDBA@ORCL > commit;
Commit complete.
MYDBA@ORCL >
MYDBA@ORCL > select * from data;
DEPT_CODE DATA
-------------------- --------------------------------------------------
2 1blahblah
3 2blahblah
1 3blahblah
2 4blahblah
3 5blahblah
1 6blahblah
2 7blahblah
3 8blahblah
1 9blahblah
2 10blahblah
10 rows selected.
MYDBA@ORCL >
MYDBA@ORCL > create table temp_dept_codes
2 (
3 old_dept_code varchar2(10),
4 prefix varchar2(10),
5 suffix varchar2(10)
6 );
Table created.
MYDBA@ORCL >
MYDBA@ORCL > insert into temp_dept_codes values ('1', '100_', '111');
1 row created.
MYDBA@ORCL > insert into temp_dept_codes values ('2', '200_', '222');
1 row created.
MYDBA@ORCL > insert into temp_dept_codes values ('3', '300_', '333');
1 row created.
MYDBA@ORCL > commit;
Commit complete.
MYDBA@ORCL >
MYDBA@ORCL > select * from temp_dept_codes;
OLD_DEPT_C PREFIX SUFFIX
---------- ---------- ----------
1 100_ 111
2 200_ 222
3 300_ 333
3 rows selected.
MYDBA@ORCL >
MYDBA@ORCL > update data set dept_code =
2 ( select prefix || suffix from temp_dept_codes where old_dept_code = dept_code);
10 rows updated.
MYDBA@ORCL >
MYDBA@ORCL > commit;
Commit complete.
MYDBA@ORCL >
MYDBA@ORCL > select * from data;
DEPT_CODE DATA
-------------------- --------------------------------------------------
200_222 1blahblah
300_333 2blahblah
100_111 3blahblah
200_222 4blahblah
300_333 5blahblah
100_111 6blahblah
200_222 7blahblah
300_333 8blahblah
100_111 9blahblah
200_222 10blahblah
10 rows selected.
MYDBA@ORCL >
MYDBA@ORCL > drop table data;
Table dropped.
MYDBA@ORCL > drop table temp_dept_codes;
Table dropped.
MYDBA@ORCL >
MYDBA@ORCL > set echo off;
MYDBA@ORCL >
|
|
|
|