Solved

copy row with all children

Posted on 2013-12-24
18
31 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 66
SSRS 2013 - Overlapping reports 2 21
TSQL - How to declare table name 26 29
ms sql + get number in list out of total 7 28
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

776 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