Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

copy row with all children

Posted on 2013-12-24
18
Medium Priority
?
39 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 2
18 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

715 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