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 7625915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:15:19+00:00 2026-05-31T05:15:19+00:00

I have a simple database of the following form : Users{ UserID (PK) Username

  • 0

I have a simple database of the following form :

   Users{
    UserID (PK)
    Username
    Email
    FirstName
    SecondName
   }

   Groups{
    GroupID (PK)
    GroupName
   }

   Membership{
    UserID (FK)
    GroupID (FK)
   }

Membership is the “go-between” for the other two tables. When a user registers, their Username is inserted into the Users and Groups table (creating a group by themselves). UserID and GroupID should reference the corresponding columns in Users and Groups.

Is the best way of doing this simply with three INSERT INTO statements when a user registers, like so :

if (db.Execute("INSERT INTO Users(Username, Email, FirstName, SecondName) VALUES(@0,@1,@2,@3)", username, email, firstName, secondName) < 1
                || db.Execute("INSERT INTO Groups(GroupName) VALUES(@0)", username) < 1
                || db.Execute("INSERT INTO Membership(UserID, GroupID) VALUES(@0, @1)",?????????????) < 1)
            {
                <p class="error">Wasn't able to insert User record</p>//insert failed
            }else {
            Response.Redirect("Success.cshtml"); //success
            }

What should I put in the third INSERT INTO statement so that it will insert the userID and corresponding groupID correctly?

  • 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-05-31T05:15:20+00:00Added an answer on May 31, 2026 at 5:15 am

    This is database logic… put it in the database.

    CREATE PROCEDURE dbo.AddUserAndGroup
      @username   NVARCHAR(255),
      @email      VARCHAR(320),
      @firstname  NVARCHAR(32),
      @secondName NVARCHAR(32)
    AS
    BEGIN
      SET NOCOUNT ON;
    
      DECLARE @UserID INT, @GroupID INT;
    
      INSERT dbo.Users(Username, Email, FirstName, SecondName)
        SELECT @username, @email, @firstname, @secondname;
    
      SELECT @UserID = SCOPE_IDENTITY();
    
      -- could probably simply rest with OUTPUT, just keeping it simple/consistent
    
      INSERT dbo.Groups(GroupName) SELECT @username;
    
      SELECT @GroupID = SCOPE_IDENTITY();
    
      INSERT dbo.Membership(UserID, GroupID) SELECT @UserID, @GroupID;
    END
    GO
    

    Now you should be able to say something like this (pardon me if I don’t have your client syntax correct, but I’m trying to come close):

    if (db.Execute("EXEC dbo.AddUserAndGroup @0,@1,@2,@3;", 
        username, email, firstName, secondName) < 1
    {
      <p class="error">Wasn't able to insert User record</p>//insert failed
    }
    else
    {
      Response.Redirect("Success.cshtml"); //success
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple database with two tables. Users and Configurations. A user has
I have a simple form right now: <form action='<? echo $PHP_SELF;?>' method='POST'> Username:<input type='text'
I have a simple form that uploads an image to a database. Using a
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
I have a simple database table called Entries: class CreateEntries < ActiveRecord::Migration def self.up
I have a simple database with multiple tables. I can't figure out how to
I have a simple database named customer with a single table data.I want to
Ok, i have a simple database engine (its a proprietary product written in vb6)
I have completed a simple database for a project. Only 6tables. Of the 6,
I have a simple UserControl for database paging, that uses a controller to perform

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.