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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:21:26+00:00 2026-05-23T15:21:26+00:00

How do i populate the column colNEW in table #tt with the value from

  • 0

How do i populate the column colNEW in table #tt with the value from column col5 table @t2 in my INSERT statement ?
Can my current expression be reused, or do i have to use merge ?

I am using mssql server 10.

DECLARE @t1 TABLE (id INT IDENTITY(1,1), col1 INT, col2 INT)
DECLARE @t2 TABLE (col3 INT, col4 INT, col5 INT)

INSERT @t2 VALUES (1,2,3);INSERT @t2 VALUES (2,3,4)

CREATE TABLE #tt (id INT, col3 INT, col4 INT, colNEW int)

INSERT #tt (id, col3, col4)
SELECT *
FROM
  ( 
INSERT INTO @t1(col1,col2)  
OUTPUT Inserted.id, Inserted.col1,Inserted.col2
SELECT col3, col4  
FROM @t2 
  ) t

I hope someone can help.

  • 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-23T15:21:27+00:00Added an answer on May 23, 2026 at 3:21 pm

    You’ll have to use merge:

    DECLARE @t1 TABLE (id INT IDENTITY(1,1), col1 INT, col2 INT)
    DECLARE @t2 TABLE (col3 INT, col4 INT, col5 INT)
    
    INSERT @t2 VALUES (1,2,3);INSERT @t2 VALUES (2,3,4)
    
    CREATE TABLE #tt (id INT, col3 INT, col4 INT, colNEW int)
    
    INSERT #tt (id, col3, col4,colNew)
    SELECT *
    FROM
      ( 
    MERGE INTO @t1 t1
    using @t2 t2 on 1=0
    when not matched then INSERT (col1,col2)  
    VALUES(t2.col3, t2.col4  )
    OUTPUT Inserted.id, Inserted.col1,Inserted.col2,t2.col5
      ) t
    
    select * from #tt
    

    This is a slight misuse of MERGE – I’m only using it because it allows other tables to be referenced in the OUTPUT clause – rather than (for insert) just the inserted table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a BULK INSERT statement to populate a large (17-million-row) table
When we use sqlldr to populate an NCLOB column with a text value from
I have a table that includes a column for the user to select from
I have a listview with 2 columns. I can easily populate the first column
I want to populate a drop down menu with a column from a MySQL
I'm trying to populate a new column in a table with data residing in
i need to populate a table column which takes one of 30 different string
I have an application where two column could be populated from a number of
Using SQL2k5, I have a staging table that contains columns that will populate numerous
I'm trying to populate an array from a dataset with only a specific column

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.