Results 1 to 6 of 6

Thread: [RESOLVED] Insert a child record for every existing parent ID

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Resolved [RESOLVED] Insert a child record for every existing parent ID

    Hi,

    I have in the database tblUsers:

    UserID
    LastName
    FirstName
    etc.

    I now created another table tblUserSettings:

    UserSettingID
    UserID
    Setting1
    Setting2
    etc.

    So every user shall have some settings that I can use all over. e.g. I want to create 3 color themes, etc.

    The problem that I now have is that this newly created tblUserSettings is empty and I have a lot of existing UserID's. I want to insert default (child) data for every existing UserID in tblUsers.

    Meaning for every UserID in tblUsers insert default values to tblUserSettings.

    This is a one time problem because in the future when a new user is created in tblUsers then I shall insert a record with default values for that user in tblUserSettings.

    I guess I can do this manually, but it will a pain as it will take long time and maybe i can learn a quick way for future scenarios like this.

    Thank you

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Insert a child record for every existing parent ID

    Something like:

    insert into tblUserSettings(UserID,Setting1,Setting2,....)
    select distinct UserID, 'default1','default1',....)
    from tblUsers

    With the default values hardcoded like 'red','blue' or 1,2,3
    Please remember next time...elections matter!

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Insert a child record for every existing parent ID

    A slightly modified version from Tyson where in the event this needed to be ran more than once the following ensures no duplicates. Also note the setting table should have a primary key.

    Code:
    INSERT INTO tblUserSettings ( UserID )
                SELECT UserID
                FROM   dbo.tblUsers t1
                WHERE  t1.UserID NOT IN (   SELECT UserID
                                            FROM   dbo.tblUserSettings );
    Name:  2019.jpg
Views: 149
Size:  30.7 KB

    Defining SQL in code
    Code:
    Dim insertIntoStatement As String =
            <SQL>
            INSERT INTO tblUserSettings ( UserID )
                        SELECT UserID
                        FROM   dbo.tblUsers t1
                        WHERE  t1.UserID NOT IN (   SELECT UserID
                                                    FROM   dbo.tblUserSettings );
            </SQL>.Value

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Insert a child record for every existing parent ID

    I'd have gone with a left join
    Code:
    INSERT INTO tblUserSettings ( UserID, Setting1, Setting2, Setting3)
                SELECT U.UserID, 'S1', 'S2', 'S3'
                FROM   dbo.tblUsers U
                LEFT JOIN dbo.tblUserSettings US on U.UserID = US.UserID
                WHERE  US.UserID is null
    Just another way to skin the cat.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Insert a child record for every existing parent ID

    How do you all just KNOW these things!!!

    Thank you very much

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Insert a child record for every existing parent ID

    Quote Originally Posted by schoemr View Post
    How do you all just KNOW these things!!!

    Thank you very much
    The same way one gets to Carnegie Hall... Practice, practice, practice.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width