Solved

URGENT: Inserting into two tables

Posted on 2004-10-12
8
171 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
Comment Utility
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
Comment Utility
or a trigger at the Unit Table... an After Insert trigger, that will perform the above checks?
0
 
LVL 14

Expert Comment

by:DragonSlayer
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:rodneylim
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks esoftbg! Sorry for taking so long to reply! Needed to "trail & error" but it works! cheers!
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
You are welcome !
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

12 Experts available now in Live!

Get 1:1 Help Now