Note that there are some explanatory texts on larger screens.

plurals
  1. POCapturing a relation between two rows while one of the two rows being inserted
    primarykey
    data
    text
    <p>Please be patient while I explain my scenario. I have following two tables:</p> <pre><code>Items(id, plan, free) </code></pre> <p>-- ‘id’ is identity column.</p> <pre><code>ItemsIncludes(MasterId, IncludeId) </code></pre> <p>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).</p> <p>Step 1 is to insert purchased items from cart (no problem).</p> <p>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).</p> <p>Step 3 is to insert the IDs generated during this first 2 steps in their proper relationship into the ItemsIncludes table (problem).</p> <p>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:</p> <p>Step 2--</p> <pre><code>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 </code></pre> <p>The plan structure is also similar but is not relevant to the problem at hand therefore not described.</p> <p>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.:</p> <p>Note: I’ve not run the following example, but it is legal.</p> <pre><code>Delete Items Output iti.MasterID, deleted.ID into @ItemsIncludesIntermediate From items it Join ItemsIncludes iti on it.ID = iti.IncludeId </code></pre> <p>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?</p> <p>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.</p> <p>The problem looked simple at first, but now getting on my nerves</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload