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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:57:46+00:00 2026-06-10T03:57:46+00:00

I know that in T-SQL (Server 2008 R2) I can use the ‘Output’ keyword

  • 0

I know that in T-SQL (Server 2008 R2) I can use the ‘Output’ keyword to get the Id of a row I just inserted. For example, I can do

insert into [Membership].[dbo].[User] (EmailAddress) 
output Inserted.UserId 
values('testUser1@test.com')

Is there any way of composing this into another insert? For example, lets say I want to add a new user and immediately add that user to a UserRole table which maps the UserId to a RoleId.

Basically, I would like to do something like below.

insert into UserRole (RoleId, UserId) 
values 
(
    1, 
    insert into [Membership].[dbo].[User] (EmailAddress) 
    output Inserted.UserId values('testUser1@test.com')
)

But I can’t seem to get this to work. I tried wrapping the internal insert in brackets () or using a select * from () etc.

What am I missing? Is this composition even possible?

Thanks for the help.

Regards,

  • 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-10T03:57:48+00:00Added an answer on June 10, 2026 at 3:57 am

    You would have to capture the output into a table variable:

    DECLARE @TempVar TABLE (UserID INT)
    
    insert into [Membership].[dbo].[User] (EmailAddress) 
    output Inserted.UserId INTO @TempVar(UserID)
    values('testUser1@test.com')
    

    and then in a second step do an insert from that temp table into the target table:

    INSERT INTO dbo.UserRole (RoleId, UserId) 
       SELECT 
          (yourRoleId), tv.UserID
       FROM @TempVar tv
    

    You could also direct the OUTPUT clause directly into the target table – that’ll work if you can e.g. use a fixed value for your RoleID:

    DECLARE @FixedRoleID INT = 42
    
    INSERT INTO [Membership].[dbo].[User] (EmailAddress) 
    OUTPUT @FixedRoleID, Inserted.UserId INTO dbo.UserRole(RoleId, UserId)
    VALUES ('testUser1@test.com')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that SQL server use memory cache for the all operations by loading
Does anyone know where I can get SQL server documentation pertaining to writing stored
I know that SQL Server does not have boolean data type and your best
I am using SQL Server for my web application. How will I know that
I have an application using LINQ-to-SQL with SQL Server CE 4. I know that
I know that in standard SQL you can do this: update top (100) table1
I know I can combine tables with sql queries, but that's not what I
I develop desktop-based WPF-application, that uses SQL Server 2008 R2 Database and ADO.NET Entity
I have to use MS SQL Server 2008 to store data of PHP CAKE
Here's the query that is being executed on SQL Server 2008: SELECT "dbo"."tblMainData"."recID" FROM

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.