将数据从非规范化数据库迁移到井设计数据库

时间:2014-10-28 10:25:02

标签: sql-server sql-server-2008

我正在将设计不佳的表中的读取迁移到sqlserver 2008中的新Normalize中 我的目标是在变量中获取源表存储中的总记录数。循环遍历每一行并插入目标表。我在类似帖子上进行了大量搜索:

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

但能够修改SQL Examples的代码 现在我得到这个错误:

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

代码:

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

1 个答案:

答案 0 :(得分:1)

忘记光标;这样做:

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