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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:36:24+00:00 2026-05-11T18:36:24+00:00

I have a stored procedure with multiple insert/select statements. Let’s say I’m using the

  • 0

I have a stored procedure with multiple insert/select statements. Let’s say I’m using the first insert to populate a “Manager” table. On insert, a ManagerId (incremented automatically) is added, but not referenced in the insert statement. I then wish to use the ManagerId from this table to insert a row into another table, where ManagerId is a foreign key. Sample code as follows..

USE [TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sptInsertNewManager]
    -- Add the parameters for the stored procedure here
    @FName varchar(50),
    @LName varchar(50),
    @EMail varchar(100),   
    @UserRoleID int,
    @LANUserID varchar(25), 
    @GroupID int
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
INSERT INTO [Manager]           
          ([FName], 
           [LName],
           [Email], 
           [UserRoleID],
           [LANUserID],          
           [ActiveFlag],
           [GroupID]
)
     VALUES
            (@FName
            ,@LName
            ,@EMail
            ,@UserRoleID
            ,@LANUserID
            ,1 
            ,@GroupID);

COMMIT

SELECT ManagerId FROM [Manager] AS newManager WHERE LANUserID = @LANUserID; 
        --also insert into Users table.
    INSERT INTO [dbo].[aspnet_Users] (
        [UserId],
        [UserName],
        [LoweredUserName],
        [ManagerId]
    )
        VALUES (
            NEWID(),
            @LANUserID,
            LOWER(@LANUserID),
            newManager)
END

This, obviously, does not work. This was my attempt at solving this. I’m fairly new to SQL, so any help with this problem would be greatly appreciated.

  • 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-11T18:36:24+00:00Added an answer on May 11, 2026 at 6:36 pm

    use scope_identity() after your insert to capture the most recent single identity value from within your current scope:

    DECLARE @ID     int
    
    
    INSERT ......
    SELECT @ID=scope_identity()
    

    use @ID wherever you need it

    note: SCOPE_IDENTITY() is preferred over the older @@IDENTITY because it gives the last Identity value in the current scope, which avoids issues from triggers that insert into log tables (with identities).

    However, if you need multiple identity values (inserting a set of rows), use OUTPUT and INTO:

    declare @test table (RowID  int identity(1,1) primary key not null, RowValue varchar(10) null)
    declare @OutputTable table (RowID int not null)
    
    insert into @test (RowValue)
        OUTPUT INSERTED.RowID
        INTO @OutputTable
        SELECT 'A'
        UNION SELECT 'B'
        UNION SELECT 'C'
        UNION SELECT 'D'
        UNION SELECT 'E'
    
    
    select * from @OutputTable
    

    the output:

    (5 row(s) affected)
    RowID
    -----------
    1
    2
    3
    4
    5
    
    (5 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 150k
  • Answers 150k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The <?eclipse version="3.5"?> entry in the plugin.xml has no affect… May 12, 2026 at 9:48 am
  • Editorial Team
    Editorial Team added an answer Looks like there is no such processors. · VFPv1 was… May 12, 2026 at 9:48 am
  • Editorial Team
    Editorial Team added an answer 1) Applications written in native code will run faster. Furthermore,… May 12, 2026 at 9:48 am

Related Questions

I have a stored procedure with multiple insert/select statements. Let's say I'm using the
So lets say I have a dataset based on: SELECT TopicLink.*, Topic.Name AS FromTopicName,
I want to generate some XML in a stored procedure based on data in
I'm using a SELECT statement in T-SQL on a table similar to this: SELECT
From an efficiency and best practices point of view, I appreciate everyones thoughts. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.