4

The short version is I'm trying to map from a flat table to a new set of tables with a stored procedure.

The long version: I want to SELECT records from an existing table, and then for each record INSERT into a new set of tables (most columns will go into one table, but some will go to others and be related back to this new table).

I'm a little new to stored procedures and T-SQL. I haven't been able to find anything particularly clear on this subject.

It would appear I want to something along the lines of

INSERT INTO [dbo].[MyNewTable] (col1, col2, col3)
    SELECT
        OldCol1, OldCol2, OldCol3 
    FROM 
        [dbo].[MyOldTable] 

But I'm uncertain how to get that to save related records since I'm splitting it into multiple tables. I'll also need to manipulate some of the data from the old columns before it will fit into the new columns.

Thanks

Example data

MyOldTable

Id  | Year | Make | Model   | Customer Name
572 | 2001 | Ford | Focus   | Bobby Smith
782 | 2015 | Ford | Mustang | Bobby Smith

Into (with no worries about duplicate customers or retaining old Ids):

MyNewCarTable

Id | Year | Make | Model
1  | 2001 | Ford | Focus 
2  | 2015 | Ford | Mustang

MyNewCustomerTable

Id | FirstName | LastName | CarId
1  | Bobby     | Smith    | 1
2  | Bobby     | Smith    | 2
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Randy Hall
  • 7,716
  • 16
  • 73
  • 151

3 Answers3

2

I would say you have your OldTable Id to preserve in new table till you process data.

I assume you create an Identity column Id on your MyNewCarTable

INSERT INTO MyNewCarTable (OldId, Year, Make, Model)
SELECT Id, Year, Make, Model FROM MyOldTable

Then, join the new table and above table to insert into your second table. I assume your MyNewCustomerTable also has Id column with Identity enabled.

INSERT INTO MyNewCustomerTable (CustomerName, CarId)
SELECT CustomerName, new.Id 
FROM MyOldTable old
JOIN MyNewCarTable new ON old.Id = new.OldId

Note: I have not applied Split of Customer Name to First Name and Last Name as I was unsure about existing data.

If you don't want your OldId in MyNewCarTable, you can DELETE it

ALTER TABLE MyNewCarTable DROP COLUMN OldId
techspider
  • 3,370
  • 13
  • 37
  • 61
1

You are missing a step in your normalization. You do not need to duplicate your customer information per vehicle. You need three tables for 4th Normal form. This will reduce storage size and more importantly allow an update to the customer data to take place in one location.

Customer CustomerID FirstName LastName

Car CarID Make Model Year

CustomerCar CustomerCarID CarID CustomerID DatePurchaed

This way you can have multiple owners per car, multiple cars per owner and only one record needs to be updated per car and or customer...4th Normal Form.

Ross Bush
  • 14,648
  • 2
  • 32
  • 55
-1

If I am reading this correctly, you want to take each row from table 1, and create a new record into table A using some of that row data, and then data from the same original row into Table B, Table C but referencing back to Table A again?

If that's the case, you will create TableA with an Identity and make thats the PK.

Insert the required column data into that table and use the @IDENTITY to retrieve the last identity value, then you will insert the remaining data from the original table into the other tables, TableB, TableC, etc. and use the identity you retrieved from TableA as the FK in the other tables.

By Example:

Table 1 has columns col1, col2, col3, col4, col5 Table A has TabAID, col1, col2 Table B has TabBID, TabAID, col3 TableC has TabCID, TabAID, col4

When the first row is read, the values for col1 & col2 are inserted into TableA. The Identity is captured from that row inserted, and then value for col3 AND the identity are entered into TableB, and then value for col4 AND the identity are entered into TableC.

This is a standard data migration technique for normalizing data.

Hope this assists,

LogicalMan
  • 384
  • 2
  • 6