?
Solved

URGENT: Inserting into two tables

Posted on 2004-10-12
8
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

770 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