간단한 저장 프로세스 생성 예

2166 단어 저장 프로세스
 
ALTER PROCEDURE  [dbo].[usp_syn_kmbs]
	 @batchid  int --     --    OUTPUT     
AS
BEGIN
   declare @username varchar --    
   declare @cellphone varchar
   declare @storecode	varchar
   declare @sid int
   declare cur cursor for --    
	SELECT  [SID],USERNAME, CELLPHONE,STORECODE  FROM  KMBS_SOURCE  WHERE  BATHCID=@batchid  
	
	open cur--    
	fetch next from cur into @sid,@username,@cellphone,@storecode  --                
	while(@@fetch_status=0) --             
	begin
	  declare @isin int
	  declare @autoid int
	   select @isin=isnull([dkr_id],0) from [KMBS_REGISTER] where [DKR_CELLPHONE]=@cellphone
	 if @isin=0 
	    begin
	  
	   INSERT INTO  [KMBS_REGISTER]
           ([DKR_SID]
           ,[DKR_NAME]
           ,[DKR_NICKNAME]
           ,[DKR_CELLPHONE]
           ,[DKR_STORECODE]
           ,[CREATEDATE]
           ,[UPDATEDATE]
           ,[DKR_ISNEW]
           ,[DKR_ISFINISH]
           ,[DKR_CALLER],BITCHID)
           values(@sid,@username,@username,@cellphone,@storecode,GETDATE(),GETDATE(),0,0,0,@batchid)
			 select @autoid= @@identity
			 select @isin=isnull(dre_id,0) from REGISTER where DRE_CELLPHONE=@cellphone
			  if @isin>0 
				  begin
					update [KMBS_REGISTER] set  DKR_DRE_ID=@isin where  DKR_ID=@autoid
			  end 
	 
	   end
	 else
	 begin 
	    update  KMBS_SOURCE set ISIN=@isin where [SID]=@sid 
	 end

	fetch next from cur into   @sid,@username,@cellphone,@storecode --                ,                      
	end
	close cur--    
	deallocate cur--    
	
   update KMBS_BATCH set 
    REPEATNUMBER=(select COUNT(*) from KMBS_SOURCE where BATCHID=@batchid and ISIN is not null)
   ,SUCCESSNUMBER=(select COUNT(*) from KMBS_REGISTER where BATCHID=@batchid)
   ,UPDATEDATE=GETDATE()
   ,TOTAL=(select COUNT(*) from KMBS_SOURCE where BATCHID=@batchid)
   ,[STATE]=2
   where BATCHID=@batchid
	
END
         ,           :
  1、varchar      
  2、if @isin=0      ,             null          set @isin = isnull(@isin,0)   .
  3、    select @isin=isnull([dkr_id],0)       ,            
             :set @isin=null           :        SET NOCOUNT ON;

좋은 웹페이지 즐겨찾기