I am working on an SQL Database
I have a Table Users
Id Name Age AddressId
----+------+------+-----------
Where AddressId is a foreign key to a table names Addresses
The Addresses Table:
Id Country State City ZipCode
----+---------+------+------+---------
I need an SQL Script, that loops through all the users and If the AddressId of that user is null, Create a new record in the Addresses Table with default Values and assign it to the user as a foreign key
Something like this in SQL script:
Foreach (User in Users)
If(user.AddressId==null)
INSERT INTO Addresses values('DefaultCountry', 'DefaultState', 'DefaultCity', 99999)
User.AddressId= the id of the newly created row
EDIT
It is a one-to-one relationship
thanks for any help
You can use
mergeandoutputto do this without a loop.SE-Data
If you only need to add one address and connect that one to all users that have none use this instead.