lern
lern

Reputation: 9

Migrate data from non Normalized database to a well Design database

I am migrating reads from a poorly design table to the newly Normalize one in sqlserver 2008.
My goal here is to get a count of total record in the Source table store in in a variable. The loop through each row and insert to the destination table. I have made numerous search on similar posts:

1: How do I insert a record from one table into another table? and Inserting into database by reading another

But was able to modify a code from SQL Examples
Now am get this error:

Msg 207, Level 16, State 1, Line 5   
Invalid column name 'Num_Row_in_table

Code:

DECLARE @i INT
DECLARE @Num_Row_in_table int

SET @i=1
WHILE(@i <= Num_Row_in_table)
BEGIN
 Insert into Destinationtable (FirstName,LastName,Photo,SN)
  (
 Select FirstName,LASTNAME,Photo,SN From MyDB.dbo.sourcetable
  )
 SET @i=@i+1
END

Upvotes: 0

Views: 58

Answers (1)

Pieter Geerkens
Pieter Geerkens

Reputation: 11893

Forget the cursor; just do this:

Insert into Destinationtable (FirstName,LastName,Photo,SN)
Select FirstName,LASTNAME,Photo,SN From MyDB.dbo.sourcetable

Upvotes: 1

Related Questions