Moving Data Between Tables
-
I think you have to do this in two parts. If you have a natural key on the table, it's easiest to use that. Something like (just a draft, may contain several mistakes):
INSERT INTO TargetTable (Title)
SELECT Title
FROM SourceTableUPDATE TargetTable
SET ParentID = (SELECT tt1.Id
FROM TargetTable tt1, SourceTable st1, SourceTable st2
WHERE targettable.Title = st1.Title
AND st2.Id = st1.ParentId
AND st2.Title = tt1.Title)The need to optimize rises from a bad design. My articles[^]
Thank you, seems a very good idea. I will try it :rose:
-
Thank you, seems a very good idea. I will try it :rose:
-
I have got a table say something like this:
ID VARCHAR(30),
ParentID VARCHAR(30),
TITLE NVARCHAR(100)As it seems, this table has a parent-child relation with itself. the ID column is not auto-generated and is a combination of ASCII characters and numbers with length of 30. Now, I want to move all the data contained in the above table to the following table:
ID SMALLINT
ParentID INT
TITLE NVARCHAR(100)In this table, ID is an auto-generated integer Identity column starting from 1. What is the best way for converting the ID columns while moving them to the new table? Thanks in advance for any help :)
Shouldn't ID and ParentID have the same type? Are the Titles unique?
-
Shouldn't ID and ParentID have the same type? Are the Titles unique?
ID and ParentID are of the same type (both varchar in source table and INT in destination table) Titles may not necessarily be unique. Therefor, I might have a title like 'business' whose parentId is NULL and have another title like 'business' whose parentId is 'POVAD78Y6CSDD8F76NEL876D'. The grand parent of all items is an item whose parentId is NULL (a root element). Could you suggest a good solution please? :)
-
ID and ParentID are of the same type (both varchar in source table and INT in destination table) Titles may not necessarily be unique. Therefor, I might have a title like 'business' whose parentId is NULL and have another title like 'business' whose parentId is 'POVAD78Y6CSDD8F76NEL876D'. The grand parent of all items is an item whose parentId is NULL (a root element). Could you suggest a good solution please? :)
Maysam Mahfouzi wrote:
INT in destination table
That's not what this says: ID SMALLINT ParentID INT
Maysam Mahfouzi wrote:
Titles may not necessarily be unique
That would seem to eliminate the other suggestion.
Maysam Mahfouzi wrote:
Could you suggest a good solution please?
Maybe... Perhaps you could add the existing varchar columns to the new table temporarily, copy the data over, then set the ParentID values based on the old mappings within the new table, and then remove the varchar IDs.
-
Maysam Mahfouzi wrote:
INT in destination table
That's not what this says: ID SMALLINT ParentID INT
Maysam Mahfouzi wrote:
Titles may not necessarily be unique
That would seem to eliminate the other suggestion.
Maysam Mahfouzi wrote:
Could you suggest a good solution please?
Maybe... Perhaps you could add the existing varchar columns to the new table temporarily, copy the data over, then set the ParentID values based on the old mappings within the new table, and then remove the varchar IDs.
PIEBALDconsult wrote:
That's not what this says: ID SMALLINT ParentID INT
I'm sorry think of those both as SMALLINT.
PIEBALDconsult wrote:
That would seem to eliminate the other suggestion.
You are right, at that time I didn't know titles are not unique. I just found some duplicate titles. I can't rely on old VARCHAR values to calculate new ID and ParentIDs in SMALLINT. How is it possible to move a string like 'LSFDKJSDLFKSJDLJSLDK' to a SMALLINT column?
-
PIEBALDconsult wrote:
That's not what this says: ID SMALLINT ParentID INT
I'm sorry think of those both as SMALLINT.
PIEBALDconsult wrote:
That would seem to eliminate the other suggestion.
You are right, at that time I didn't know titles are not unique. I just found some duplicate titles. I can't rely on old VARCHAR values to calculate new ID and ParentIDs in SMALLINT. How is it possible to move a string like 'LSFDKJSDLFKSJDLJSLDK' to a SMALLINT column?
Maysam Mahfouzi wrote:
I can't rely on old VARCHAR values to calculate new ID
No, you're using auto-increment for the new IDs, right?
ID,ParentId,OldID,OldParentId,Description
(or whatever it is)INSERT INTO NewTable (OldID,OldParentId,Description) SELECT * FROM OldTable
(add an ORDER BY clause if desired) And you get all new IDs. You also have the original mappings. For any row with non-null OldParentId, match it to the row with that OldID to get the (new) ID to put in the (new) ParentID. Something like this:UPDATE NewTable
SET ParentId=B.ID
FROM NewTable A
INNER JOIN NewTable B
ON A.OldParentID=B.OldID(I think I'll go test that now.) Then remove the OldID and OldParentID columns (if desired).
-
Maysam Mahfouzi wrote:
I can't rely on old VARCHAR values to calculate new ID
No, you're using auto-increment for the new IDs, right?
ID,ParentId,OldID,OldParentId,Description
(or whatever it is)INSERT INTO NewTable (OldID,OldParentId,Description) SELECT * FROM OldTable
(add an ORDER BY clause if desired) And you get all new IDs. You also have the original mappings. For any row with non-null OldParentId, match it to the row with that OldID to get the (new) ID to put in the (new) ParentID. Something like this:UPDATE NewTable
SET ParentId=B.ID
FROM NewTable A
INNER JOIN NewTable B
ON A.OldParentID=B.OldID(I think I'll go test that now.) Then remove the OldID and OldParentID columns (if desired).
So I think I've got to add OldID and OldParentID columns to the destination table, move data the way you've suggested and then remove OldID and OldParentID columns. This solution seems quite perfect and beautiful and I'm going to test it as soon as I have access to database. I'm also thinking of a way to move data without using those two temporary columns in target table. I really really appreciate your help and time PIEBALDconsult :) :rose:
_
-
So I think I've got to add OldID and OldParentID columns to the destination table, move data the way you've suggested and then remove OldID and OldParentID columns. This solution seems quite perfect and beautiful and I'm going to test it as soon as I have access to database. I'm also thinking of a way to move data without using those two temporary columns in target table. I really really appreciate your help and time PIEBALDconsult :) :rose:
_
Just got around to testing it:
INSERT INTO NewTable (OldID,OldParentID,Description) SELECT * FROM OldTable
SELECT * FROM NewTableUPDATE A
SET ParentID=B.ID
FROM NewTable A
INNER JOIN NewTable B
ON A.OldParentID=B.OldIDSELECT * FROM NewTable
It worked on the small amount of data I created to test it:
1 NULL 1 NULL Section 1
2 NULL 2 NULL Section 2
3 1 1.a 1 Section 1.a
4 1 1.b 1 Section 1.b
5 2 2.a 2 Section 2.a
6 2 2.b 2 Section 2.b
7 3 1.a.i 1.a Section 1.a.i
8 3 1.a.ii 1.a Section 1.a.ii -
Just got around to testing it:
INSERT INTO NewTable (OldID,OldParentID,Description) SELECT * FROM OldTable
SELECT * FROM NewTableUPDATE A
SET ParentID=B.ID
FROM NewTable A
INNER JOIN NewTable B
ON A.OldParentID=B.OldIDSELECT * FROM NewTable
It worked on the small amount of data I created to test it:
1 NULL 1 NULL Section 1
2 NULL 2 NULL Section 2
3 1 1.a 1 Section 1.a
4 1 1.b 1 Section 1.b
5 2 2.a 2 Section 2.a
6 2 2.b 2 Section 2.b
7 3 1.a.i 1.a Section 1.a.i
8 3 1.a.ii 1.a Section 1.a.iiThank you again, I tested it and it worked for me too :) :rose: