Solved

URGENT: Inserting into two tables

Posted on 2004-10-12
8
177 Views
Last Modified: 2010-08-05
To all experts!

How do I write a query to insert into two tables? The problem is that I am importing a CSV Unit Table file which contains only the Unit_number, Unit_name and School_name. FYI I will be putting this query into my ADOQuery component.

Firstly,it will need to insert the unit_number and unit_name into the unit table. Then it will have to check if the school name exsists in school table. If it exsists [e.g.School of Marketing] then it will take its school_id [e.g.01] and insert into the Unit_Table_School_id. If it does not exsists [e.g.School of Mangement] then it will insert the school name into school table thus generating the sequential number(which is already in that oracle table) and taking that sequential number [e.g. 02] and put it in Unit_Table_School_id.

School Table
-School_id(PK) [e.g. 01]
-School_name [e.g.School of Marketing]

Unit Table
-Unit_id(PK) [e.g. 001]
-Unit_number [e.g. 123456]
-Unit_name [e.g. Business Communication 100]
-Description [e.g.How to yak and yak]
-School_id(fk) [e.g. 01]

PLEASEEEEE HEEELLLLPPPP!!!!!

Thank you!
Rodney
0
Comment
Question by:rodneylim
  • 4
  • 2
  • 2
8 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12289303
Hi rodneylim,
My opinion is that what you describe can be done using a stored procedure, because:
1). Check for the  School_name in the School Table;
2.1). If School_name does not exists insert it and get just inserted School_id;
2.2). If School_name exists get it's School_id;
3). Insert into the Unit Table ....
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12290161
or a trigger at the Unit Table... an After Insert trigger, that will perform the above checks?
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12290172
The reason I suggested a trigger is because, that way, you only insert into the Unit table (which means any other programme can do the same), and the updating of the School table is done behind the scene. So any third party software that writes to your database does not need to be informed of the need to either (i) call a stored procedure (ii) remember to write to 2 tables.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12290635
I think the only problem for the trigger usage is that it does not know about the School_name .... (I have been worked with InterBase triggers where the triggers know all the fields of the their Table)
But when you create a stored procedure for that you will declare as input parameters all the fields of the Unit Table plus School_name parameter. This way the stored procedure will work properly.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rodneylim
ID: 12299432
Hi guys!

Would I be able to use arrays(in Delphi) to read line by line and checking only once with the oracle database? it would be faster then stored procedures right? Imagine that I have thousands of data to be imported. It would take a long time to import if stored preocedures to be used as there would be constant checking and referring to the database right? If I hard code into Delphi, it would only check once. Do i make sense?

I am not very good with stored procedures. How do I actually write it in orcale and in Delphi?

Cheers!
Rodney
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 500 total points
ID: 12305855
Unfortunately I am not experienced in Oracle database, so I can post here an example about InterBase StoredProcedure, but I am not sure that it's syntax is useful for Oracle ....
> Would I be able to use arrays(in Delphi) to read line by line and checking only once with the oracle database?
I am not sure it is the best way to use arrays .... Basically databases are created for fast and safe SELECTing, INSERTing, UPDATEing and DELETEing of data .... I do not recommend usage of arrays because the database data is dynamic stuff ....
In this context it is better to use StoredProcedure or ADOQuery (Query):
When you need to know is there in the database Table SCHOOL with a name 'MATHEMATICAL SCHOOL' you can do:

  ADOQuerySchool.Active := False;
  ADOQuerySchool.SQL.Text :='' + ' SELECT * FROM SCHOOL' + '';
  ADOQuerySchool.Active := True;
  if ADOQuerySchool.Locate('SCHOOL_NAME', 'MATHEMATICAL SCHOOL', [loCaseInsensitive, loPartialKey] ) then
  begin
    // Insert multiple UNITS into the UNIT Table using the current SCHOOL_ID from ADOQuerySchool;
  end
  else
  begin
    // First insert a new SCHOOL into the SCHOOL table and then
    // insert multiple UNITS into the UNIT Table using the SCHOOL_ID of just created SCHOOL;
  end;

it is possible also:    

  Edit1.Text := 'MATHEMATICAL SCHOOL';
  ADOQuerySearch.Active := False;
  ADOQuerySearch.SQL.Text :=''
                                          + ' SELECT * FROM SCHOOL WHERE SCHOOL_NAME = '
                                          + QuotedStr(Edit1.Text)
                                          + '';
  ADOQuerySearch.Active := True;
  if (ADOQuerySearch.RecordCount=1) then
  begin
    // Insert multiple UNITS into the UNIT Table using the current SCHOOL_ID from ADOQuerySearch;
  end
  else
  begin
    // First insert a new SCHOOL into the SCHOOL table and then
    // insert multiple UNITS into the UNIT Table using the SCHOOL_ID of just created SCHOOL;
  end;

it is possible also usage of a STORED PROCEDURE biult in the database ....
0
 

Author Comment

by:rodneylim
ID: 12410727
Thanks esoftbg! Sorry for taking so long to reply! Needed to "trail & error" but it works! cheers!
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12410873
You are welcome !
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now