Sql 使用循环将一表的数据拆分为多表

--此次的功能是将一个以前设计的用户表数据拆分为多个表分别储存 

--以前表为账户表zhanghu 拆分为 用户基本信息Account  ,雇主信息ProfessionInfo ,个人信息PersonalInformation ,其他信息TradingInformation

--因为要各表之间有外键引用,所以又插入的先后,表都使用自动增列所以插入数据后需要得到所有返回的ID插入到主表中


//首先将你要拆分的表复制一个零时表,只需要账户表的唯一建ID列和一个自己做的排序ID列即可(因为账户表的ID可能不是连续的,这样我下面的循环就不好控制)

select id as departmentNo,ROW_NUMBER() OVER(ORDER BY id) AS rowNumber into #depTemp--建立临时表

from [zhanghu]

//然后开始循环 临时表的所有行 ,首先设置循环变量。临时表总行数
declare @max int--获得最大的rowNumber
select @max=max(rownumber)
from #depTemp


declare @rowNo int
set @rowNo=1


declare @tbid int  --储存账户表的唯一建


declare @TradingInformationId int  --新表的ID
declare @PersonalInformationId int  --新表的ID
declare @ProfessionInfoId int  --新表的ID
declare @AccountId int  --新表的ID


while @rowNo<=@max  --对每一个rowNumber进行循环操作
begin
--这儿对每一行要进行的操作的代码

select @tbid = departmentNo from  #depTemp where rowNumber = @rowNo  --得到循环遍历要操作表唯一键

--得到账户表唯一键后开始做插入工作,因为以前的数据很杂乱,所以使用isnull函数来替换一些为Null 的数据来保证插入一定成功。

--因为这里我只会操作一次,而且对数据比较清楚,所有没有做事务,如果严谨的可以做事务,如果下面的一条插入失败则回滚,所有成功则提交之类的。

--开始插入数据


--其他信息
insert into [TradingInformation](LeverTransaction_Often, LeverTransaction_Date, FuturesOptions_Often, FuturesOptions_Date, StockShare_Often, StockShare_Date,
 IsHaveCertificate, CertificateInfo, Preferential_Code )(select isnull(ganggan1,'无'), isnull(ganggan2,'无'), isnull(qihuo1,''), isnull(qihuo2,'无'), isnull(gupiao1,'无'), isnull(gupiao2,'无'),isnull(zizhi,'无'),isnull(xiangqing,'无'),isnull(youhuiquan,'无') from [zhanghu] where id = @tbid );
 select @TradingInformationId = @@identity
--个人信息
insert into [PersonalInformation] (Country, Province, City, LiveAddress, PhoneNumber, HomePhone, Birthday, Nationality, CredentialsFromCountry, CredentialsTypeId, CredentialsNumber,
 Explanation )(select isnull(country,'无'),isnull(sheng,'无'),isnull(shi,'无'),isnull(adress,'无'),isnull(tel,'无'), isnull(phone,'无') ,CAST([year] AS VARCHAR(10)) + '-' + CAST([month] AS VARCHAR(10))  + '-' + CAST([day] AS VARCHAR(10)) as P, 
 isnull(guoji,'无') ,isnull(qianfa,'无'),isnull([card],'无'),isnull(cardid,'无'),'' from [zhanghu]  where id = @tbid );select @PersonalInformationId = @@identity
--雇主信息
insert into [ProfessionInfo](PersonalInformationId, ProfessionType, IsReceiveEnglandControlWork, HirerName, HirerCountry, HirerAddress, HirerCity, HirerProvince,
 ZipCode, NatureOfBusiness, Post, BEFOREYearRevenue, DepositAndInvestment, FastIntoCapitalMoney, SourceOfFundsID, OtherSource)(select @PersonalInformationId,'其他','否',isnull(guzhu,'无'),isnull(gzguojia,'无') ,isnull(gzjiedao,'无'),
 isnull(gzzhen,'无'),isnull(gzsheng,'无'),isnull(youbian,'无'),isnull(gzxingzhi,'无'),isnull(zhiwei,'无'),isnull(shuiqian,'无'),isnull(touzi,'无'),isnull(rujin,0),isnull(laiyuan,'无') ,isnull(laiyuan2,'无') from [zhanghu] where id = @tbid);select @ProfessionInfoId = @@identity
--用户信息
insert into Account ( TradingInformationId, AccountName,AccountPassword, Nickname, Country, CH_Surname, EN_Surname, CH_Name, EN_Name, AppellationId, Email, Agents, PWQuestionId, PWAnswer,
 CurrencyTypeId, AccountType, AccountNatureType, InformationId, Explanation ,IsDisable)(select @TradingInformationId,'无',isnull([password],'无'),isnull(cw,'无'),isnull(gj,'无'),isnull(cname1,'无'),isnull(lname1,'无'),isnull(cname2,'无'),isnull(lname2,'无'),0,isnull(email,'无'),
  isnull(dailishang,'无'),1,isnull(s,'无'),1,'个人','自然联权共有人',@ProfessionInfoId,'',0 from [zhanghu] as A where A.id = @tbid );select @AccountId = @@identity

--所有插入成功后删除零时表中的这一行,因为如果没有一次成功的话则可以再次执行这一段,不过要做判断:是否取到账户表唯一建,没有则跳过有则执行,也可以把零时表---的ID再做一次排序让其从1开始再逐行加1,然后在执行上面的知道所有的成功。

--就是把这个再执行一遍:账户表改零时表

--(select id as departmentNo,ROW_NUMBER() OVER(ORDER BY id) AS rowNumber into #NewdepTemp--建立临时表

--from #depTemp)

delete  from #depTemp where rowNumber = @rowNo 

//循环变量每次递增

set @rowNo=@rowNo+1
end


select * from #depTemp //查看有哪些没有成功,以便修改

drop table #depTemp--清除临时表
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九幽之君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值