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

  • Home
  • SEARCH
  • 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 4568154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:02:10+00:00 2026-05-21T19:02:10+00:00

Please be patient while I explain my scenario. I have following two tables: Items(id,

  • 0

Please be patient while I explain my scenario.
I have following two tables:

Items(id, plan, free)

— ‘id’ is identity column.

ItemsIncludes(MasterId, IncludeId)

There are two types of items – purchased and free (included). Regardless, both types of items are stored in Items table (obviously the bit field ‘free’ is set according to the type).
The ItemsIncludes table establishes a one-to-many relationship between a purchased item (MasterId) and items included as free with it (IncludeId).

Step 1 is to insert purchased items from cart (no problem).

Step 2 Is to insert free items for each purchased item inserted based on purchased item’s plan (which has nothing to do with the problem being faced).

Step 3 is to insert the IDs generated during this first 2 steps in their proper relationship into the ItemsIncludes table (problem).

The requirement is to capture free itemId being generated during insertion along with its purchased itemId. The output clause would have done this, if following was legal:

Step 2–

Declare @ItemsIncludesIntermediate table(MasterId int, IncludeId int)
Insert into Items
(PlanId, IsFree)
Output it.ID -- Illegal
, inserted.ID into @ItemsIncludesIntermediate
Select pli.IncludePlanId, 1
From Items it
Join PlansIncludes pli on it.PlanId = pli.MasterPlanId

The plan structure is also similar but is not relevant to the problem at hand therefore not described.

SQL Server doesn’t allow other than inserted.columns to be referred in output clause during insert; however, it is not the same in case of updates/delete, in which statements, referring to any identifier participating in FROM list is allowed. E.G.:

Note: I’ve not run the following example, but it is legal.

Delete Items
Output iti.MasterID, deleted.ID into @ItemsIncludesIntermediate
From items it
Join ItemsIncludes iti on it.ID = iti.IncludeId

In short, if I cannot do this with output clause, then how can I capture free itemId as it is being generated along with the purchased itemId for which it’s being generated?

I’m open for view and instead of trigger approach, if it is possible with that. The fundamental thing is that I don’t want to change the structure. E.G. I don’t want to remove ItemsIncludes table and have MasterItemId be included into Items table.

The problem looked simple at first, but now getting on my nerves

  • 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-21T19:02:11+00:00Added an answer on May 21, 2026 at 7:02 pm

    If you use SQL Server 2008 you can use merge instead of insert and get the mapping between the ID’s.

    Have a look at this question for more details on how to do it.

    Using merge..output to get mapping between source.id and target.id

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

Sidebar

Related Questions

This is my first question so please be patient :) Background: I'm implementing an
I'm new to .NET, so please be patient with me ;) On my Windows
Please explain to me why the very last echo statement is blank? I expect
Please be patient with my question, as this may be a bit longer. If
I am new in Java so please be patient. It is common to map
I'm new to sqlite so please be patient :) I've got a UITableView reading
please patient with my poor english. I'd bad concept of UIView, UIViewController, UITableView, UITableViewController
I will try to be specific if I can - please be patient, first
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual

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.