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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:26:17+00:00 2026-06-15T06:26:17+00:00

Inside a TSQL transaction I have an operation of inserting a record into a

  • 0

Inside a TSQL transaction I have an operation of inserting a record into a MyTable1 and an operation of updating a MyTable2 with a value of Identity column of the MyTable1 record, which is only to be inserted after the transaction is commited.
So, how to get this autogenerated value before it is actually inserted into the table?

Code just to illustrate the question:

CREATE TABLE MyTable1(
MyTable1Id int identity(1,1) primary key,
Field1 varchar(50)
) 

CREATE TABLE MyTable2(
MyTable2Id int identity(1,1) primary key,
MyTable1Id int
)


CREATE PROCEDURE MyProc(
   @MyVal1  [varchar](50)
)
AS
BEGIN TRAN
-- try to insert
INSERT INTO MyTable1(
   Field1
)
SELECT @MyVal1

IF @@ERROR <> 0
BEGIN
  ROLLBACK TRAN
END

-- update MyTable2
DECLARE @TheUnknownIdValue int
UPDATE MyTable2
SET    MyTable1Id = @TheUnknownIdValue -- how to get the value needed here?
WHERE ...

IF @@ERROR <> 0
BEGIN
  ROLLBACK TRAN
END

COMMIT TRAN 
  • 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-15T06:26:18+00:00Added an answer on June 15, 2026 at 6:26 am

    Use SCOPE_IDENTITY() just after the INSERT

    --
    DECLARE @newId INT 
    INSERT INTO MyTable1(
       Field1
    )
    SELECT @MyVal1
    --End of Insert here
    SELECT @newId = SCOPE_IDENTITY()
    --Rest of the procedure
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside my Controller i have function that runs after user clicks on item, which
Inside my .aspx I have some JSON code that looks like this: function someFunctionName()
inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into
Inside a form I have a button. What is the difference between when I
Inside my control, I have: ContextMenu = new ContextMenu(); ContextMenu.MenuItems.Add(new MenuItem(&Add Item, onAddSpeaker)); ContextMenu.MenuItems.Add(new
Can someone give me the tsql to find also the dates that lie inside
I have very long select query which i need to filter based on some
Inside a batch file on Windows I would like some variable to have the
Inside an SWT Table I have many TableItems. I want to move one of
Inside a function I have created, I have this loop that checks if the

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.