Home » RDBMS Server » Server Utilities » SQL loader Help please
SQL loader Help please [message #122950] Thu, 09 June 2005 06:05 Go to next message
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 #122955 is a reply to message #122950] Thu, 09 June 2005 06:14 Go to previous messageGo to next message
himang
Messages: 282
Registered: March 2005
Location: Bangalore
Senior Member

One way is to load this data into Temporary table through SQL*Loader and issue update statements based on that table.

You can also make use of External Tables (if using Oracle9i) to perform your task.

Regards,
Himanshu
Re: SQL loader Help please [message #122977 is a reply to message #122955] Thu, 09 June 2005 07:26 Go to previous messageGo to next message
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 #123006 is a reply to message #122977] Thu, 09 June 2005 09:45 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
sqlldr cannot do the update.
so, load the data as-is-from-excel-sheet into oracle database (into a staging table).
then by sql methods you can update the original table (with the values from the staging table).
>>but I am quite confused on how to use loader with external tables
yes you are.
sqlldr is an utility to load the external data (in flat files/ text files) into an oracle table.
This link to documentation will help you to understand how sql*loader works.
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96652/toc.htm

External tables is an functionality in oracle where you can consider the Flat/text file in OS as an oracle table~.
Jared Still has written a good article ( a very favourite of mine)
on using external tables.
http://www.dbazine.com/oracle/or-articles/still1
Re: SQL loader Help please [message #123013 is a reply to message #123006] Thu, 09 June 2005 10:09 Go to previous messageGo to next message
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 #123016 is a reply to message #123013] Thu, 09 June 2005 10:15 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
yes. you got it. That is the only method i know (within oracle means. Once my perl savvy developer wrote a parsing script that updates the text file to-be-loaded. So he fixed the textfile with proper updates and then he loaded the textfile into oracle table).
Re: SQL loader Help please [message #123181 is a reply to message #123016] Fri, 10 June 2005 07:30 Go to previous messageGo to next message
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 #123212 is a reply to message #123181] Fri, 10 June 2005 09:59 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
intended behaviour.

http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_915a.htm#2080942

You cannot update a column that is referenced in the ON condition clause.
Re: SQL loader Help please [message #123214 is a reply to message #123212] Fri, 10 June 2005 10:05 Go to previous messageGo to next message
Shania
Messages: 16
Registered: March 2005
Junior Member
Can you please suggest me an alternative here in that case ?

Thanks.
Re: SQL loader Help please [message #123231 is a reply to message #122950] Fri, 10 June 2005 11:30 Go to previous messageGo to next message
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 >

Re: SQL loader Help please [message #123404 is a reply to message #123231] Mon, 13 June 2005 05:28 Go to previous message
Shania
Messages: 16
Registered: March 2005
Junior Member
Thanks a lot Scot!!!
Previous Topic: Are containst exported ?
Next Topic: ORA-01917
Goto Forum:
  


Current Time: Wed Jul 03 08:31:59 CDT 2024