首页 > 代码库 > 存储过程 将数据插入到临时表,再根据条件判断 插入到不同的表

存储过程 将数据插入到临时表,再根据条件判断 插入到不同的表

技术分享
 1 ALTER PROCEDURE [dbo].[Proc_PX_InportScore]
 2 --@IdentityNum nvarchar(50),
 3 @PlanType nvarchar(50),--培训类别Guid
 4 @BanQGuid nvarchar(50) --班期Guid
 5 as
 6 begin
 7 
 8 
 9 
10      --导入成绩
11      insert into pk_user(RowGuid,Name,IdentityNum,Age,Sex,DanWeiName,TitleCode,MobilePhone,IsEnable,[Status])
12      (--插入到人员表   条件:临时表里的人员不在人员表中  判断依据 IdentityNum
13      select LOWER(NEWID()),Name,IdentityNum,Age,(case Sex when  then 0 else 1 end),DanWeiName,ZhiWu,MobilePhone,1,2  from PX_InportScore
14      WHERE not exists (select pk_user.IdentityNum from pk_user WHERE  PX_InportScore.IdentityNum=pk_user.IdentityNum)
15      )
16 
17      insert into PX_BaoM(RowGuid,Name,UserGuid,LoginID,DanWeiName,PXPrograms,Note)
18      (--插入到报名表    条件:从临时表里插入成功到人员表中的人  并且这些人不存在报名表中  条件:人员表的RowGuid   报名表的UserGuid
19             select LOWER(NEWID()), a.Name,b.RowGuid,a.IdentityNum,a.DanWeiName,03,Note
20             from PX_InportScore a 
21             join pk_user b on a.IdentityNum=b.IdentityNum
22             where b.rowguid not in (select UserGuid from PX_BaoM)
23 
24 
25      )
26 
27      insert into PX_BaoMDetail(RowGuid,ItemGuid,ItemName,IsDel,ClassGuid,ParentGuid,IsConfirm)
28      (--插入到报名子表   条件:插入成功到报名表里的人  并且这些人不在子表中       子表的ParentGuid  报名表的 RowGuid
29       select LOWER(NEWID()),@PlanType,‘‘ ,0,@BanQGuid, c.RowGuid,0
30             from PX_InportScore a 
31             join pk_user b on a.IdentityNum=b.IdentityNum
32             join PX_BaoM c on  b.rowguid =c.UserGuid
33             where c.RowGuid not in (select ParentGuid  from PX_BaoMDetail)
34 
35      )
36 
37 
38     --成功标记
39     update b set b.flag=1    
40     from PX_BaoMDetail a,PX_InportScore b where a.ClassGuid=@BanQGuid
41     
42      declare @successc int 
43      declare @failc int 
44 
45      select @successc= count(1)  from PX_InportScore where  Flag=1
46      select @failc= count(1)  from PX_InportScore where  isnull(flag,0)<>1
47              
48      select @successc as successc,@failc as failc
49 
50 
51 
52 
53 end
54 GO
View Code

 

存储过程 将数据插入到临时表,再根据条件判断 插入到不同的表