Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
prakash.c
Starting Member
6 Posts |
Posted - 2008-06-30 : 03:29:48
|
| Hi All,Here is my requirementI have 2 DataBase DB1 and DB2I have to move data from DB1.Table1 to DB2.Table1And my DB1.Table1 Structure is as follows------------------------------------Id - Parent ID - Description------------------------------------1 - 0 - AAA2 - 0 - BBB3 - 1 - CCC4 - 2 - DDD5 - 3 - EEE------------------------------------And the condition is, I have Data already present in DB2.Table1 like------------------------------------Id - Parent ID - Description------------------------------------1 - 0 - AAX2 - 0 - BBX3 - 2 - CCX4 - 1 - DDX5 - 4 - EEX------------------------------------While moving Data from DB1.Table1 the record1 - 0 - AAAmust the place in DB2.Table2 as 6 - 0 - AAAAnd the correspondent ParentId of DB1.Table1 must be changed to 6as shown belowDB1.Table13 - 1 - CCCmust be changed to 3 - 6 - CCCHow to do this....?i think i'm clear with my issues, can anyone help me to solve this problem. Thanks in advance.Prakash.C |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-30 : 03:48:19
|
[code]DECLARE @db1 TABLE( ID int, ParentID int, Description varchar(3))INSERT INTO @db1SELECT 1 , 0 , 'AAA' UNION ALLSELECT 2 , 0 , 'BBB' UNION ALLSELECT 3 , 1 , 'CCC' UNION ALLSELECT 4 , 2 , 'DDD' UNION ALLSELECT 5 , 3 , 'EEE'DECLARE @db2 TABLE( ID int identity(1, 1), ParentID int, Description varchar(3))INSERT INTO @db2 (ParentID, Description)SELECT 0 , 'AAX' UNION ALLSELECT 0 , 'BBX' UNION ALLSELECT 2 , 'CCX' UNION ALLSELECT 1 , 'DDX' UNION ALLSELECT 4 , 'EEX'INSERT INTO @db2 (ParentID, Description)SELECT CASE WHEN ParentID <> 0 THEN NULL ELSE ParentID END, DescriptionFROM @db1 db1WHERE NOT EXISTS ( SELECT * FROM @db2 x WHERE x.Description = db1.Description )UPDATE db2SET ParentID = db2p.IDFROM @db2 db2 INNER JOIN @db1 db1 ON db2.Description = db1.Description INNER JOIN @db1 db1p ON db1.ParentID = db1p.ID INNER JOIN @db2 db2p ON db2p.Description = db1p.DescriptionWHERE db2.ParentID IS NULLSELECT *FROM @db2/*ID ParentID Description ----------- ----------- ----------- 1 0 AAX 2 0 BBX 3 2 CCX 4 1 DDX 5 4 EEX 6 0 AAA 7 0 BBB 8 6 CCC 9 7 DDD 10 8 EEE(10 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
prakash.c
Starting Member
6 Posts |
Posted - 2008-06-30 : 04:50:48
|
| Hi Khtan,Great, U'r almost correct, But it doesn't works if the Description is Same in the Both the tableLike in DB1.Table11 - 0 - AAAAnd in DB2.Table11 - 0 - AAAhow to make for this khtan. ThanksPrakash.C |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-30 : 05:16:29
|
then how do you identified if the AAA in DB1.Table1 is not existed in DB2.Table1 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
prakash.c
Starting Member
6 Posts |
Posted - 2008-06-30 : 05:21:30
|
| Hi KhtanDescription is independent for the tables, we can not match based on it. The thing is we have to append the records from DB1.Table1 to DB2.Table1, on the condition that Parent child relation for the DB1.Table1 records must not mis-match in the DB2.Table1.(Need to Protect the Parent - Child Relation)Thanks Prakash.C |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-06-30 : 09:03:27
|
[code]DECLARE @db1 TABLE( [ID] int, ParentID int, [Description] varchar(3))INSERT INTO @db1SELECT 1 , 0 , 'AAA' UNION ALLSELECT 2 , 0 , 'BBB' UNION ALLSELECT 3 , 1 , 'CCC' UNION ALLSELECT 4 , 2 , 'DDD' UNION ALLSELECT 5 , 3 , 'EEE'DECLARE @db2 TABLE( [ID] int identity(1, 1), ParentID int, [Description] varchar(3))INSERT INTO @db2 (ParentID, [Description])SELECT 0 , 'AAX' UNION ALLSELECT 0 , 'BBX' UNION ALLSELECT 2 , 'CCX' UNION ALLSELECT 1 , 'DDX' UNION ALLSELECT 4 , 'EEX'DECLARE @ids TABLE( [ID] int, [Description] varchar(3))INSERT INTO @db2 (ParentID, [Description])OUTPUT INSERTED.ID, INSERTED.[Description] INTO @ids SELECT CASE WHEN ParentID <> 0 THEN NULL ELSE ParentID END, [Description]FROM @db1 db1UPDATE db2SET ParentID = db2p.IDFROM @db2 db2 INNER JOIN @db1 db1 ON db2.[Description] = db1.[Description] INNER JOIN @db1 db1p ON db1.ParentID = db1p.[ID] INNER JOIN @ids db2p ON db2p.[Description] = db1p.[Description]WHERE db2.ParentID IS NULLSELECT *FROM @db2/*ID ParentID Description ----------- ----------- ----------- 1 0 AAX 2 0 BBX 3 2 CCX 4 1 DDX 5 4 EEX 6 0 AAA 7 0 BBB 8 6 CCC 9 7 DDD 10 8 EEE(10 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
prakash.c
Starting Member
6 Posts |
Posted - 2008-07-01 : 00:14:54
|
| Hi Khtan,thank u very for your help. I have done it in this way, check it weather it will do or notDeclare C1 Cursor for Select Id, ParentId, Description from Table1Declare @Id as BigIntDeclare @PId as BigIntDeclare @Desc as NVarchar(50)Declare @NewId as BigIntDeclare @Count as IntSelect @Count = ISNULL(MAX(Id),0) + 1 from Table2Open C1Fetch Next from C1 Into @Id, @PId, @DescWhile @@Fetch_Status = 0Begin Select @NewId = ISNULL(MAX(Id),0) + 1 from Table2 IF @PId IS NOT NULL Insert Into Table2 (Id, ParentId, [Description]) Values (@NewId, ((@Count - 1) + @PId), @Desc) ELSE Insert Into Table2 (Id, ParentId, [Description]) Values (@NewId, @PId, @Desc) Fetch Next from C1 Into @Id, @PId, @DescEndClose C1Deallocate C1 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-01 : 00:33:18
|
if my solution is working for you, it should be faster as it set based compare to loop based KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
prakash.c
Starting Member
6 Posts |
Posted - 2008-07-01 : 00:42:44
|
| Yes Khtan, U'r solution is also ok, but we cannot give chance for a condition that one or more description may be same in current table or other table.but with my solution, we can avoid those problemthanks Prakash.C |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-01 : 00:44:38
|
quote: Originally posted by prakash.c Yes Khtan, U'r solution is also ok, but we cannot give chance for a condition that one or more description may be same in current table or other table.but with my solution, we can avoid those problemthanks Prakash.C
the last solution i posted does not assumed that. Take a look KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-07-01 : 00:48:26
|
see this. I changed some of the data in db1 to be same as db2.
DECLARE @db1 TABLE( [ID] int, ParentID int, [Description] varchar(3))INSERT INTO @db1SELECT 1 , 0 , 'AAA' UNION ALLSELECT 2 , 0 , 'BBB' UNION ALLSELECT 3 , 1 , 'CCX' UNION ALLSELECT 4 , 2 , 'DDD' UNION ALLSELECT 5 , 3 , 'EEX'DECLARE @db2 TABLE( [ID] int identity(1, 1), ParentID int, [Description] varchar(3))INSERT INTO @db2 (ParentID, [Description])SELECT 0 , 'AAX' UNION ALLSELECT 0 , 'BBX' UNION ALLSELECT 2 , 'CCX' UNION ALLSELECT 1 , 'DDX' UNION ALLSELECT 4 , 'EEX'DECLARE @ids TABLE( [ID] int, [Description] varchar(3))INSERT INTO @db2 (ParentID, [Description])OUTPUT INSERTED.ID, INSERTED.[Description] INTO @ids SELECT CASE WHEN ParentID <> 0 THEN NULL ELSE ParentID END, [Description]FROM @db1 db1UPDATE db2SET ParentID = db2p.IDFROM @db2 db2 INNER JOIN @db1 db1 ON db2.[Description] = db1.[Description] INNER JOIN @db1 db1p ON db1.ParentID = db1p.[ID] INNER JOIN @ids db2p ON db2p.[Description] = db1p.[Description]WHERE db2.ParentID IS NULLSELECT *FROM @db2/*ID ParentID Description ----------- ----------- ----------- 1 0 AAX 2 0 BBX 3 2 CCX 4 1 DDX 5 4 EEX 6 0 AAA 7 0 BBB 8 6 CCX 9 7 DDD 10 8 EEX(10 row(s) affected)*/ KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|