Not exactly sure what your mean but this should at least point you in the right direction
Begin Try
Begin Transaction
Update Users
Set Users.address = Address.address, create_date = GetDate()
From Addresses
Inner Join Users On Addresses.userid = Users.userid
Commit Transaction
End Try
Begin Catch
Rollback Transaction
End Catch
It should be something like this. There are a couple of ways of doing the problem so have fun with it and hopefully this helped. For testing it write two Select * From Users
statements one before and one after. Also change Commit Transaction
to Rollback Transaction
so you don't have to worry about making a mistake.
Just reread question yea you can't do that in one shot just replace the Update
statement with
Insert Into Addresses (address_id, create_date, location, user_id)
Values ('@ddr355_1d', GetDate(), '1234theLocation', 123478)
and you will have to do that for each one but should be easy with only 16 entries in the User table. You might want to look into writing a Stored Procedure if you plan on adding more to the table. Something kind of like this
Create Procedure [dbo].[AddressesInsertData]
(
@Address Int,
@Location varchar(100),
@UserId Int
)
As
Begin Try
Begin Transaction
Insert Into Addresses (address_id, create_date, location, user_id)
Values (@Address, GetDate(), @Location, @UserId)
Commit Transaction
End Try
Begin Catch
Rollback Transaction
End Catch
Basic structure of a stored procedure. I would add an if not exists
in there that would update instead of insert but this should be plenty to get you started. Hopefully these examples should clear up somethings for you and help you out.