Preparing and importing data into an existing database

I am maintaining a PHP application with a SQL Server backend. The database structure is something like this:

lot
===

lot_id (pk, identify)
lot_code

building
========

buildin_id (pk, identity)
lot_id (fk)

inspection
==========

inspection_id (pk, identify)
building_id (fk)
date
inspector
result

      

There are parcels and buildings already in the database and I need to import some checks. Key points:

  • This is a one-time bootstrap.
  • The data goes into an Excel file.
  • Excel data is not aware of autogenerated DB IDs: inspections must be linked to buildings via their lot_code

What are my options for downloading such data?

date         inspector     result     lot_code
==========   ===========   ========   ========
31/12/2009   John Smith    Pass       987654X
28/02/2010   Bill Jones    Fail       123456B

      

Update: how I finally did it

In case someone else has to complete a similar task, these are the steps required to finally load the data:

  • Prepare your Excel file: remove unnecessary columns, provide names for sheets and column headers, etc.

  • C SQL Server Import / Export Wizard

    (32-bit, 64-bit lacks this feature), load each sheet into a (new) database table. The wizard takes care of most of the dirty details, including creating the appropriate database structure.

  • Enter the database with your favorite client. To make the SQL coding easier, I have created some additional fields in the new tables.

  • Start a transaction.

    BEGIN TRANSACTION;

  • Update the auxiliary columns in the newly created tables:

    UPDATE excel_inspection$ SET building_id = bu.building_id FROM building bu INNER JOIN ....

  • Insert data into target tables:

    INSERT INTO inspection (...) SELECT ... FROM excel_inspection$ WHERE ....

  • Review the results and commit the transaction if everything is ok:

    COMMIT;

In my case, SQL Server was complaining about sort conflicts when connecting to new tables with existing ones. It was fixed by setting the appropriate collation on the new tables, but the method is different: in SQL Server 2005 I could just change the collation from the SQL Server Manager (click, click, save and execute), but in SQL Server 2008 I had to set the collation manually in the Wizard import (button "Edit SQL").

+2


a source to share


2 answers


1) get excel file to CSV.

2) import CSV file into storage table: SQL SERVER - import CSV file into SQL Server using bulk load - load comma delimited file into SQL Server



3) write a stored procedure / script where you declare local variables and loop through each row in the hold table, building the correct rows in the actual tables. Since this is a one-time download, there is no shame in the loop and you have complete control over all the logic.

0


a source


Your data will need to have natural primary keys in the data file. It looks like lot_code might be one, but I don't see it for the build table.

Also, you say that inspections refer to buildings by lot code, however, the relationship between table and table is between build and check.



If the data is modeled correctly, you can import it into temporary tables and then insert / update the target tables using natural keys.

0


a source







All Articles