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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:37:10+00:00 2026-05-23T09:37:10+00:00

Assume we have hierarchy which consists from several objects, like this: And I want

  • 0

Assume we have hierarchy which consists from several objects, like this:

enter image description here

And I want to copy this tree (and maybe somehow change those objects).

A simple method to do this is just iterate between objects and create it one-by-one. But performance here is very poor. In addition, i do not like loops 😉

So question – is it possible to do with set-based logic?

  • 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-23T09:37:11+00:00Added an answer on May 23, 2026 at 9:37 am

    Using the technique described in this question, you could do something like this:

    DECLARE @CartId int, @NewCartId int;
    DECLARE @CartItems TABLE (OldId int, NewId int);
    
    SET @CartId = ...;
    
    INSERT INTO Shopping_Cart (
      Session_Id_Str,
      Name,
      Program_User_Id,
      Shopping_Cart_Status_Code,
      Placed_Order_Id)
    SELECT
      Shopping_Cart_Id,
      Session_Id_Str,
      Name,
      Program_User_Id,
      Shopping_Cart_Status_Code,
      Placed_Order_Id
    FROM Shopping_Cart
    WHERE Shopping_Cart_Id = @CartId;
    SET @NewCartId = SCOPE_IDENTITY();
    
    WITH src AS (
      SELECT
        Shopping_Cart_Item_Id,
        @NewCartId AS Shopping_Cart_Id,
        Base_Product_Id,
        Product_Category_Id,
        Shop_Id,
        Currency_Code
      FROM Shopping_Cart_Item
      WHERE Shopping_Cart_Id = @CartId
    )
    MERGE Shopping_Cart_Item AS tgt
    USING src
    ON 0 = 1
    WHEN NOT MATCHED THEN
      INSERT (
        Shopping_Cart_Id,
        Base_Product_Id,
        Product_Category_Id,
        Shop_Id,
        Currency_Code)
      VALUES (
        src.Shopping_Cart_Id,
        src.Base_Product_Id,
        src.Product_Category_Id,
        src.Shop_Id,
        src.Currency_Code)
    OUTPUT
      src.Shopping_Cart_Item_Id,
      inserted.Shopping_Cart_Item_Id
    INTO @CartItems (OldId, NewId);
    
    WITH src AS (
      SELECT
        s.Shopping_Cart_Item_Detail_Id,
        map.NewId AS Shopping_Cart_Item_Id,
        s.Display_Order_Number,
        s.Product_Id,
        s.Product_Variant_Id
      FROM Shopping_Cart_Item_Detail s
        INNER JOIN @CartItems map ON s.Shopping_Cart_Item_Id = map.OldId
    )
    MERGE Shopping_Cart_Item_Detail AS tgt
    USING src
    ON 0 = 1
    WHEN NOT MATCHED THEN
      INSERT (
        Shopping_Cart_Item_Id,
        Display_Order_Number,
        Product_Id,
        Product_Variant_Id)
      VALUES (
        src.Shopping_Cart_Item_Id,
        src.Display_Order_Number,
        src.Product_Id,
        src.Product_Variant_Id);
    

    It is assumed that you only want to copy one ‘tree’ at a time.

    All this should probably be done in a single transaction.

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

Sidebar

Related Questions

Assume you have some objects which have several fields they can be compared by:
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Assume we have a method like this: public IEnumerable<T> FirstMethod() { var entities =
I have an image file in my project. The hierarchy looks like this: I'm
Say I have a schema that represents a fixed-depth hierarchy like this: CREATE TABLE
I have a hierarchy of objects that all implement INotifyPropertyChanged. I also have a
Assume I have the following example hierarchy: US Michigan Detroit Grand Rapids Lansing Minnesota
I am having a scenario where there are several subclasses which have got similar
I have set up several Views each containing a UIPickerView each of which contains
Assume I have the following class-hierarchy in C++: class AbstractBaseClass { public: // Note:

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.