Solved

copy row with all children

Posted on 2013-12-24
18
32 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 40

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 40

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 40

Assisted Solution

by:Sharath
Sharath earned 100 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 40

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 40

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 40

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

840 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