Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8022721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:20:58+00:00 2026-06-04T22:20:58+00:00

I am working on an SQL Database I have a Table Users Id Name

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:21:01+00:00Added an answer on June 4, 2026 at 10:21 pm

    You can use merge and output to do this without a loop.

    declare @IDs table(UserId int, AddressId int);
    
    merge Addresses as T
    using (select Id, 'DefaultCountry', 'DefaultState', 'DefaultCity', 99999
           from Users
           where AddressID is null) as S (UserId, Country, State, City, ZipCode)
    on 0 = 1
    when not matched then
      insert(Country, State, City, ZipCode) 
        values (S.Country, S.State, S.City, S.ZipCode)
    output S.UserId, inserted.Id into @IDs;
    
    update Users
    set AddressId = IDs.AddressID
    from @IDs as IDs
    where Users.Id = IDs.UserId;
    

    SE-Data

    If you only need to add one address and connect that one to all users that have none use this instead.

    declare @AddressId int;
    
    insert into Addresses(Country, State, City, ZipCode)
      values('DefaultCountry', 'DefaultState', 'DefaultCity', 99999);
    
    set @AddressId = scope_identity();
    
    update Users
    set AddressId = @AddressId
    where AddressId is null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL database table with User info - ID, Name, Address, City,
I am working on PHP and database MySQL. I have two tables in SQL
I have a database working in my local sql server 2005 express edition. I
I'm currently working with MS SQL 2005, and have a table that has 17
I have a simple database with a Persons table UserId, Name,DOB The table contains
I am working on an Excel application that queries a SQL database. The queries
I'm working on a SQL Server 2008 database. This database has two tables. Book
I am working with SQL Server and I want to set the Database password
I'm working on an ASP (Classic) application using MS SQL as database. In the
I'm working with an old sql server 2000 database, mixing some of it's information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.