Solved

copy row with all children

Posted on 2013-12-24
18
27 Views
Last Modified: 2016-02-14
Dear all,
I Have the following table:
Products:
ProductID int,  ParentProductID int

I need to design a SP to [copy] a given ProductID and its all children ( and sub children ) to be a child of another ProductID

thanks
0
Comment
Question by:ethar1
  • 9
  • 6
  • 2
18 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 400 total points
Comment Utility
This is air code as I don't have anything to work with but I believe the basic logic is sound.  Let me know if you have any problems.

CREATE PROCEDURE CopyChildProducts
(
   @FomProductID INT,
   @ToProductID INT
)
AS

DECLARE @CurrProductID INT
   @NewProductID INT;

DECLARE crsProduct CURSOR FOR
SELECT ProductID
FROM Products
WHERE ParentProductID = @FromProductID;

OPEN crsProduct;
FETCH NEXT FROM crsProduct INTO @CurrProductID;

WHILE @@FETCH_STATUS = 0
BEGIN
   INSERT INTO Products (ParentProductID)
   VALUES (@ToProductID)

   SET @NewProductID = SCOPE_IDENTITY()

   EXECUTE CopyChildProducts @FromProductID = @CurrProductID,
      @ToProductID = @NewProductID

   FETCH NEXT FROM crsProduct INTO @CurrProductID;
END

CLOSE crsProduct
DEALLOCATE crsProduct
0
 

Author Comment

by:ethar1
Comment Utility
thanks,
this is exactly what is want,
but  
    -did not copy the @FromProductID, its copy the children only
    -I have some cols to  copy (say x, y, z)
0
 

Author Comment

by:ethar1
Comment Utility
I modify the SP to be
ALTER PROCEDURE [dbo].[CopyChildProducts] 
	-- Add the parameters for the stored procedure here
	  @FromProductID INT,   @ToProductID INT
AS
BEGIN


DECLARE @CurrProductID INT,   @NewProductID INT;

DECLARE crsProduct CURSOR FOR
	SELECT ProductID
	FROM Products
	WHERE ParentProductID = @FromProductID;

OPEN crsProduct;
FETCH NEXT FROM crsProduct INTO @CurrProductID;

WHILE @@FETCH_STATUS = 0
BEGIN
   INSERT INTO Products (ParentProductID ,[ProductNameEN)   
   
      select @ToProductID, ProductNameEN]
           From Products where ProductID= @FromProductID 

   SET @NewProductID = SCOPE_IDENTITY()

   EXECUTE CopyChildProducts  @CurrProductID,  @NewProductID

   FETCH NEXT FROM crsProduct INTO @CurrProductID;
END

CLOSE crsProduct
DEALLOCATE crsProduct 
END

Open in new window

but I have some problems , it copy first level only and put them inside each other
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
What is your SQL Server version?
0
 

Author Comment

by:ethar1
Comment Utility
2008
0
 
LVL 34

Expert Comment

by:Brian Crowe
Comment Utility
I believe there is a syntax issue in the INSERT clause, should be...

      INSERT INTO Products (ParentProductID ,ProductNameEN)  
      SELECT @ToProductID, ProductNameEN
      FROM Products where ProductID= @CurrProductID
0
 

Author Comment

by:ethar1
Comment Utility
I change it but I got wrong result I got :
I got
where I should got
I should got
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Can you post some sample data from your table with expected result?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ethar1
Comment Utility
I have those product structure:
structured data
I just need to make a copy of the product 229 and its children structured products
0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 100 total points
Comment Utility
You can try this. You can assign whatever ParentProductID you want in the below code.
DECLARE @ParentProductID INT
SELECT @ParentProductID = 564
;WITH cte AS(
SELECT ProductID, ParentProductID,ParentProductID TopLevelParent
  FROM Test
 WHERE ParentProductID = @ParentProductID
 UNION ALL
SELECT c.ProductID, c.ParentProductID, p.TopLevelParent 
  FROM Test c
  JOIN cte p
    ON c.ParentProductID = p.ProductID
)
SELECT ProductID, TopLevelParent FROM cte 

Open in new window


http://sqlfiddle.com/#!3/94b35/5
0
 

Author Comment

by:ethar1
Comment Utility
did u read my question?
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
You want an SP? If the code posted works for you, It can be easily converted into an SP. Let me know.
0
 

Author Comment

by:ethar1
Comment Utility
the code posted not works as required ..
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
Provide some sample data and expected result
0
 

Author Comment

by:ethar1
Comment Utility
ohhh GOD, please I don't have time for you , if you cant understand the question  please ignore it..
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
alright. I am here to help and gain knowledge by helping. If my posts won't help you, you can obviously look for more experts comments.
 
The more information, detailed explanation you can provide, the faster you can get an answer.

When you said the code posted not works as required, you need to explain what is required and where the current script is lagging. If you cannot explain, its tough to answer and I can ignore your question right here.

Finally, I am here as a volunteer and don't get dollars for answering the questions. Time is important for everyone and I answer the questions in my leisure time only.
0
 

Author Comment

by:ethar1
Comment Utility
Thanks,
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now