1 USE [ChinaMDM_0603] 2 GO 3 /****** Object: StoredProcedure [dbo].[Import_11_WorkFlow_4n_Items_CustomerCompCodes] Script Date: 2016/6/7 13:49:12 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 ALTER procedure [dbo].[Import_11_WorkFlow_4n_Items_CustomerCompCodes] 9 --参数(目前不用参数) 10 --@parameter nvarchar(100), 11 --@headerid int 12 as 13 begin 14 set xact_abort on 15 begin tran 16 --变量 17 --CustomerCompCodes 表 18 declare @CustomerHeaderid int; 19 declare @CompanyCodeid nvarchar(50); 20 declare @ProductClasses nvarchar (100); 21 declare @IsISO bit; 22 declare @IsGMP bit; 23 declare @IsGsp bit; 24 declare @IsGMPGSPOther bit; 25 declare @GMPGSPOtherValue nvarchar(500); 26 declare @count int; 27 declare @CustomerCompCodeId int; 28 --声明游标 29 declare _cur cursor for 30 --查询必要数据 31 select 32 h.id as Headerid, 33 cp.id as CompanyCodeid, 34 temp.[ProductClasses], 35 temp.IsISO, 36 temp.[IsGMP], 37 temp.[IsGSP] , 38 temp.[IsGMPGSPOther], 39 temp.[GMPGSPOtherValue] 40 from temp_CustomerCompCodes_M temp 41 join CustomerHeaders h on temp.CustomerHeaderCode=h.code 42 left join CompanyCodes cp on temp.CompanyCodes=cp.code 43 --打开游标 44 open _cur 45 --将游标所在行的column值赋给对应参数 46 fetch next from _cur into @CustomerHeaderid,@CompanyCodeid,@ProductClasses,@IsISO,@IsGMP,@IsGSP,@IsGMPGSPOther,@GMPGSPOtherValue 47 --判断游标状态@@fetch_status值的改变是通过fetch next from实现的 “FETCH NEXT FROM Cursor” 48 --0 FETCH 语句成功 -1 FETCH 语句失败或此行不在结果集中 -2 被提取的行不存在 49 while(@@FETCH_STATUS=0) 50 begin 51 select @count = COUNT(1) from CustomerCompCodes where CustomerHeaderId=@CustomerHeaderId and CompanyCodeId=@CompanyCodeid 52 if ( @count > 0) 53 begin 54 select @CustomerCompCodeId=id from CustomerCompCodes where CustomerHeaderId=@CustomerHeaderid and CompanyCodeId=@CompanyCodeid 55 update CustomerCompCodes 56 set CustomerHeaderId=@CustomerHeaderid,ProductClasses=@ProductClasses,IsISO=@IsISO,IsGMP=@IsGMP,IsGSP=@IsGsp,IsGMPGSPOther=@IsGMPGSPOther,GMPGSPOtherValue=@GMPGSPOtherValue 57 where id=@CustomerCompCodeId 58 end 59 else 60 begin 61 --将参数值插入表VendorCompCodes 62 insert into CustomerCompCodes (CustomerHeaderId,CompanyCodeid,ProductClasses,IsISO,IsGMP,IsGSP,IsGMPGSPOther,GMPGSPOtherValue,IsActive) values (@CustomerHeaderid,@CompanyCodeid,@ProductClasses,@IsISO,@IsGMP,@IsGSP,@IsGMPGSPOther,@GMPGSPOtherValue,1) 63 end 64 --循环下一行游标并将游标所在行的column值赋给对应参数 65 fetch next from _cur into @CustomerHeaderid,@CompanyCodeid,@ProductClasses,@IsISO,@IsGMP,@IsGSP,@IsGMPGSPOther,@GMPGSPOtherValue 66 end 67 --关闭游标 68 close _cur 69 --释放游标所占资源 70 deallocate _cur 71 72 --判断循环过程中是否有错,有则撤回 73 if @@error<>0 74 begin 75 rollback tran 76 end 77 --无措则提交 78 commit tran 79 set xact_abort off 80 end