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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:20:48+00:00 2026-05-28T05:20:48+00:00

I am using SQL Server 2008 to create a procedure. I am using the

  • 0

I am using SQL Server 2008 to create a procedure.

I am using the following sql statement to insert into a audit table

 insert into  Listuser
 (
   UserID,
   ListID,
   AuditCreated
  )
   select
   UserID,
   ListID,
    GETDATE()
 from  ListUser where Surname='surname'

I am using scope_identity() to get the identity column from the listuser table and insert the identity column to another logs table

If the select statement contains more than 1 value, how to get the identity value of both columns and insert into logs table?

Thanjs

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

    If you need to capture multiple identity values being inserted, I’d use the OUTPUT clause:

    DECLARE @TableOfIdentities TABLE (IdentValue INT)
    
    INSERT INTO dbo.Listuser(UserID, ListID, AuditCreated) 
      OUTPUT Inserted.ID INTO @TableOfIdentities(IdentValue)
      SELECT
          UserID, ListID, GETDATE()
      FROM 
          dbo.ListUser 
      WHERE
          Surname = 'surname'
    

    This will insert all rows into your ListUser table, and it will output all identities generated by this INSERT into the @TableOfIdentities table variable

    Read more about the OUTPUT clause on MSDN

    These values are inserted into the table variable, and you can select them out from that table variable after the insert, and do whatever you need to do with them:

    SELECT * FROM @TableOfIdentities
    

    Update: the use of the table variable here is just as an example – of course you can also output the data into a “normal” table in SQL Server – and you can also output multiple values for each inserted row, if you need that – so you can have something like:

    INSERT INTO dbo.Listuser(UserID, ListID, AuditCreated) 
      OUTPUT Inserted.ID, Inserted.UserID, Inserted.SurName, GETDATE() 
           INTO AuditTable(IdentValue, UserID, Surname, InsertDate)
      SELECT
          UserID, ListID, GETDATE()
      FROM 
          dbo.ListUser 
      WHERE
          Surname = 'surname'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this sp: using sql server 2008 create procedure SelectTopCounts @Id bigint =
I'm using SQL Server 2008. Say I create a temporary table like this one:
I'm using Microsoft SQL Server 2008 Management Studio to create a relational schema by
Using SQL Server 2008, is there a way to allow inserts to a table
I am using SQL Server 2008 Enterprise. I am wondering whether this stored procedure
Using SQL Server 2008 I created a Stored Procedure called MyStoreProc and it runs
I need to create a sql stored procedure (Sql Server 2008 - T-SQL) which
hi i am using SQL Server 2008, want to create a view to list
I have a stored procedure mentioned as below. I'm using SQL Server 2008 R2.
I am using SQL Server 2008 Enterprise. I have created a very simple test

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.