Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copy row with all children

Posted on 2013-12-24
18
Medium Priority
?
40 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
17 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 1600 total points
ID: 39738865
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
ID: 39738924
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
ID: 39738986
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 41

Expert Comment

by:Sharath
ID: 39740441
What is your SQL Server version?
0
 

Author Comment

by:ethar1
ID: 39740445
2008
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39740461
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
ID: 39740519
I change it but I got wrong result I got :
I got
where I should got
I should got
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39740545
Can you post some sample data from your table with expected result?
0
 

Author Comment

by:ethar1
ID: 39740627
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 41

Assisted Solution

by:Sharath
Sharath earned 400 total points
ID: 39740769
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
ID: 39740881
did u read my question?
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39741002
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
ID: 39763013
the code posted not works as required ..
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39764394
Provide some sample data and expected result
0
 

Author Comment

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

Expert Comment

by:Sharath
ID: 39766821
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
ID: 39767192
Thanks,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

877 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