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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:07:52+00:00 2026-06-07T00:07:52+00:00

Using a populated Table Type as the source for a TSQL-Merge. I want to

  • 0

Using a populated Table Type as the source for a TSQL-Merge. I want to execute a select statement after the merge,
retrieving all columns/rows of the table type, but instead of the ‘-1’ value, I want the new
inserted id. I am not sure I can do this in a fully set-based manner, can I?

This is for a UI that is sending a pile of inserts into the DB, and needs back the same objects, but with each of their ID column values populated. There is no ‘common column’ for the SQL JOIN operation.

CREATE TYPE instype AS TABLE(
    instypeid [smallint] NOT NULL,
    instext [varchar](64) NOT NULL
)
Go
create table #desttable ( instypeid smallint identity(1,1) primary key , instext varchar(64) )
Go
declare @newids table ( idvalue smallint )
declare @thing1 instype
insert into @thing1 values ( -1 , 'zero' )
insert into @thing1 values ( -1 , 'one' )
    Merge #desttable desttbl
            Using @thing1  srctbl
            On desttbl.instypeid = srctbl.instypeid
            When Not Matched Then
                Insert ( instext )
                Values ( instext )
            Output inserted.instypeid Into @newids
        ;

/*
        Wanted shape of the result set
        instypeid   instext
        0           zero
        1           one

*/

Thanks.

  • 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-07T00:07:53+00:00Added an answer on June 7, 2026 at 12:07 am

    But you can get that result set by slightly modifying your current code:

    if object_id('tempdb.dbo.#desttable') is not null
        drop table #desttable 
    
    create table #desttable ( instypeid smallint identity(0,1) primary key
    , instext varchar(64) )
    Go
    declare @inserted table ( idvalue smallint, instext varchar(64) )
    declare @thing1 instype
    
    insert into @thing1 values ( -1 , 'zero' ), ( -1 , 'one' )
    
    Merge #desttable desttbl
            Using @thing1  srctbl
            On desttbl.instypeid = srctbl.instypeid
            When Not Matched Then
                Insert ( instext )
                Values ( instext )
            Output inserted.instypeid, inserted.instext Into @inserted
        ;
    
    SELECT  *
    FROM    @inserted
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I differentiate between elements inside a table that is populated using a
So I have a table Links of links, that was initially populated using Nokogiri.
I have a ListView that is populated using an XML file. However, I want
I have a select list which is being populated using the values from a
For my Visualization Table, i populated data from server side using PHP as guided
i want to create a table of all the h tags for a specific
I am using a listview to populate a table with 1000 rows. The DB
iv created a database with some tables and populated them using SQL server 2008,
In runtime I'm creating a DataTable and using nested for-loops to populate the table.
I've got a ListView that is populated using a custom BaseAdapter (MyBaseAdapter) : public

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.