Solved

URGENT: Inserting into two tables

Posted on 2004-10-12
8
188 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 177
Delphi application Soap connection 5 123
Correct Component for Shopping Cart. 2 113
Making delphi communicate with a c# service 16 112
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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