Q:

Insert Into...Select - get and use autoincrement ID

I have a table which contains all information about a player. What I'd like to do is take this information and split the data into different tables. For example:
Table PlayerData has all the information. Table Address holds information about the player's residence and table Info holds information such as Name, date of birth and the Address ID (pointing to the Address table).
I can use an INSERT INTO...SELECT to copy data across. However, my issue comes in doing this sequentially such that the correct ID outputted from the Address table is inserted into the Info table otherwise there would be a mix up between which address belongs to which player. How can I get the identity created for an Address insert and use that in the subsequent Staff insert?
Speed is not a priority as this is only done once to initialise the database, the integrity is crucial.
Thanks

A:

If you are learning how to do this, then learn the right way:  the OUTPUT clause (documented here).
This allows you to put the results into a temporary table (usually a table variable).  An example is:
DECLARE @ids TABLE (AddressId int);

INSERT INTO ADDRESS( . . .)
    OUTPUT inserted.AddressId INTO @ids
    VALUES ( . . . );

INSERT INTO info( . . ., AddressId)
    SELECT . . . , i.AddressId
    FROM @ids i;

A:

You should really be using foreign keys to reference a main table. Now I've kept this simple but if you can follow this then you will be able to edit it for your needs.
Ok, let's make some sample data. This is an equivalent of your current table;
CREATE TABLE PlayerData (PlayerID int, PlayerName varchar (20), DateOfBirth date, Address1 varchar(20), Address2 varchar(20))
INSERT INTO PlayerData (PlayerID, PlayerName, DateOfBirth, Address1, Address2)
VALUES
(1,'Mike Hunt','1980-01-01','Mike Street','Hunt Town')
,(2,'Harry Dong','1970-02-02','Harry Street','Dong Town')
,(3,'Hugh Gass','1960-03-03','Hugh Street','Gass Town')
,(4,'Neil Down','1950-04-04','Neil Street','Down Town')
,(5,'Seymore Butts','1940-05-05','Seymore Street','Butts Town')

I'm going to create one table that holds a unique list of my player id numbers, this is where I would put a little further information that doesn't fit into the other tables. For this example I've just got the one field;
CREATE TABLE PlayerNum (PlayerID int PRIMARY KEY CLUSTERED)

I'm now going to make my new AddressData table. Notice it's got it's own identity field but also has a PlayerID that will reference the PlayerNum table;
CREATE TABLE AddressData (AddressID int identity(10,1) PRIMARY KEY CLUSTERED, PlayerID int, Address1 varchar(20), Address2 varchar(20), FOREIGN KEY (PlayerID) REFERENCES PlayerNum(PlayerID))

I'm going to do the same for the table that will contain my player's personal info;
CREATE TABLE PlayerPersonalInfo (InfoID int identity(50,1) PRIMARY KEY CLUSTERED, PlayerID int, PlayerName varchar(20), DateOfBirth date, FOREIGN KEY (PlayerID) REFERENCES PlayerNum(PlayerID)) 

So I've now got my new 3 tables that are empty and one table with data to insert into them.
Let's first populate our PlayerNum table, this needs to be first because of the foreign key constraints on the other tables;
INSERT INTO PlayerNum  (PlayerID)
SELECT PlayerID 
FROM PlayerData

Now I've done that, let's insert our data into AddressData. Notice I'm not inserting data into the AddressID field as it's an identity field. It will start from 10 and increment by 1 as per the table definition;
INSERT INTO AddressData (PlayerID, Address1, Address2)
SELECT PlayerID, Address1, Address2
FROM PlayerData

I'm going to do the same with my PlayerPersonalInfo data. The identity for this table will start from 50 and increment by 1;
INSERT INTO PlayerPersonalInfo (PlayerID, PlayerName, DateOfBirth)
SELECT PlayerID, PlayerName, DateOfBirth
FROM PlayerData

You can now get rid of the PlayerData table if you're confident you don't need it.
DROP TABLE PlayerData

You'll now have 3 tables;
PlayerNum
PlayerID
1
2
3
4
5

AddressData
AddressID   PlayerID    Address1        Address2
10          1           Mike Street     Hunt Town
11          2           Harry Street    Dong Town
12          3           Hugh Street     Gass Town
13          4           Neil Street     Down Town
14          5           Seymore Street  Butts Town

PlayerPersonalInfo
InfoID  PlayerID    PlayerName      DateOfBirth
50      1           Mike Hunt       1980-01-01
51      2           Harry Dong      1970-02-02
52      3           Hugh Gass       1960-03-03
53      4           Neil Down       1950-04-04
54      5           Seymore Butts   1940-05-05

Notice that the PlayerID in the final two tables can now be linked to PlayerNum in order to retrieve your data.
As we're using foreign keys, you cannot have a player with information in AddressData or PlayerPersonalInfo without a corresponding entry in PlayerNum

