Solved

URGENT: Inserting into two tables

Posted on 2004-10-12
8
182 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

16 Experts available now in Live!

Get 1:1 Help Now