--临时表

 

IF OBJECT_ID('tempdb..#Entry') is not null

 

BEGIN

 

  DROP TABLE #Entry

 

 

 

END

 

-----------------------------------------------------

 

 

 

--创建触发器

 

 ALTER TRIGGER [dbo].[icstockbill_ins]

 

   ON  [dbo].[ICStockBill]

 

   AFTER insert

 

AS 

 

BEGIN

 

SET NOCOUNT ON;

 

    declare  @ftrantype int,@Finterid int 

 

declare @text varchar(255)

 

declare @fbillerid int

 

    select @ftrantype=ftrantype,@Finterid=finterid   from inserted

 

 

   if @ftrantype=1  

 

      begin

 

         exec [采购申请单执行信息更新] @Finterid

 

          

 

 

 

      end 

 

 

 

 

 

      end

 

--------------------------------------------------------------

 

 

 

--创建存储过程

 

 

 

ALTER PROC lj_bom_wanjue @fitemid int ,@flevel int

 

AS

 

begin

 

DECLARE CURSOR FOR 

 

SELECT FITEMID,FQTY  INTO #tmp FROM dbo.BZX_BOMEntry  WHERE FID = 

 

(SELECT MAX(fid) FROM dbo.BZX_BOM WHERE FITEMID = @fitemid)

 

SELECT *FROM #tmp

 

DROP TABLE #tmp

 

END

 

 

 

 

 

EXEC   lj_bom_wanjue 78940, 1

 

----------------------------------------------------

 

--游标

 

 CREATE  table #t(fitemid INT)

 

 DECLARE @fitemid INT

 

 DECLARE lj_test CURSOR FOR

 

 SELECT TOP 50 FItemID FROM dbo.t_ICItem 

 

 OPEN lj_test 

 

 FETCH NEXT FROM lj_test INTO @fitemid

 

while (@@fetch_status=0)

 

 BEGIN

 

 INSERT INTO #t(fitemid) VALUES (@fitemid)

 

  --PRINT @fitemid

 

  FETCH NEXT FROM lj_test INTO @fitemid

 

 END

 

 CLOSE lj_test

 

 DEALLOCATE lj_test

 

 SELECT *FROM #t

 

DROP TABLE  #t

 

 

 

------------------------------------------------------------

 

--BOM嵌套

 

  declare   @lev   int,@liaohao   varchar(30)   

 

  select   @MID='2',@levs=1   

 

  declare   @t   table(物料ID号   varchar(30),序号   int,物料父ID号   varchar(30),levs   int)   

 

  insert   @t   select   物料ID号 ,序号,物料父ID号,@levs   from   BOM   where   物料ID号=@lMID      

 

  while   @@rowcount<>0   

 

  begin   

 

        set   @levs=@levs+1   

 

        insert   @t   select  物料ID号,序号,物料父ID号,@levs  from   BOM     

 

        where   物料父ID号   in(select    物料ID号   from   @t   where   levs=@levs-1)   

 

  end   

 

    

 

  select   *   from   @t   order   by   levs 

 

 

 

 

 

 

 

---------------------------------------------------------------

 

 

 

--临时表

 

insert into #bzxbom(fmbitemid,fitemid,fqty,fyt,fth,fisty,fgx,fnote,flev,fismm,fpz ) select @fcpitmeid,@fitemid,@fqty,@fyt,@fth,@fisty,@fyt,@FNOTE,cast(@fmb as varchar(255)),@fismm,@fpz 

 

      if exists(select 1 from BZX_BOM where FITEMID = @fitemid )

 

 

 

 

 

-------------------------------------------------------------

 

--存储过程调用存储过程

 

 

 

 exec GetICMaxNum 'icstockbill' ,@fqoutinterid output ,1,@fbiller

 

 exec LJ_GetBillNo  29,@fqoutbillno output

 

 

 

 select @fqoutinterid as finterid,IDENTITY(int,1,1) as fentryid,'0' as fbrno,a.FItemID,a.FBatchNo,a.FQty,a.FStockID,b.FUnitID,a.FQty as Fauxqty,'PLM调拨库存' as fnote,a.FStockPlaceID,14036 as FPlanMode

 

 into #lj_qout 

 

 from ICInventory a inner join t_ICItem b on a.FItemID =b.FItemID 

 

 where b.FNumber = @foldnumber and b.FDeleted =0 and a.FQty >0

 

 

 

 INSERT INTO ICStockBillEntry (FInterID,FEntryID,FBrNo,FItemID,FBatchNo,FQty,FDCStockID,FUnitID,Fauxqty,Fnote,FDCSPID,FPlanMode)  

 

 SELECT * from #lj_qout

 

 

 

 drop table #lj_qout

 

 

 

 INSERT INTO ICStockBill(FInterID,FBillNo,FBrNo,FTranType,FCancellation,FStatus,FUpStockWhenSave,FROB,Fdate,FDeptID,Fuse,FCheckDate,FFManagerID,FSManagerID,FBillerID,FHeadSelfB0941 ) 

 

 SELECT @fqoutinterid,@fqoutbillno,'0',29,0,0,0,1,convert(varchar(10),GETDATE(),120),65522,'',Null,10147,10147,16740,@fplmwldh

 

 

 

 EXEC p_UpdateBillRelateData 29,@fqoutinterid,'ICStockBill','ICStockBillEntry' 

 

 

 

 

 

 

 

 ----------------------------------------------------------

 

 

 

--存储过程返回值 

 

ALTER proc [dbo].[LJ_GetBillNo]

 

@ClassType  VARCHAR(50),

 

@RBillNo       VARCHAR(50)   OUTPUT

 

AS

 

BEGIN

 

DECLARE @BillNo varchar(50) 

 

DECLARE @FullBillNo varchar(50) 

 

DECLARE @BillID int 

 

DECLARE @TmpID INT 

 

DECLARE @BillIDLen int 

 

DECLARE @NOBillStr varchar(5000) 

 

DECLARE @BillStr varchar(5000) 

 

DECLARE @SCRBillNO varchar(500) 

 

 

 

SET @FullBillNo = ''

 

set  @BillNo=''

 

--取单据编号1

 

SELECT @BillNo = a.fprojectval 

 

   FROM t_BillCodeRule a

 

   LEFT OUTER JOIN t_option e ON a.fprojectid = e.fprojectid AND a.fformatindex = e.fid

 

   LEFT OUTER JOIN t_checkproject b ON a.fbilltype = b.fbilltypeid AND a.fprojectval = b.ffield

 

WHERE a.fbilltypeid = @ClassType 

 

      AND a.fprojectID = 1

 

 

 

--取单据编号2

 

SELECT @BillIDLen = a.flength,

 

               @BillID = a.fprojectval  

 

   FROM t_BillCodeRule a

 

   LEFT OUTER JOIN t_option e ON a.fprojectid = e.fprojectid AND a.fformatindex = e.fid

 

   LEFT OUTER JOIN  t_checkproject b ON a.fbilltype = b.fbilltypeid AND a.fprojectval = b.ffield

 

WHERE a.fbilltypeid = @ClassType 

 

      AND a.fprojectID = 3

 

 

 

--设置返回值

 

SET @FullBillNo = (@BillNo + REPLICATE('0' , @BillIDLen - LEN(CAST(@BillID AS VARCHAR(50)))) + CAST(@BillID AS VARCHAR(50)))

 

 

 

BEGIN TRAN

 

 

 

-- 更新流水号  

 

SET @TmpID = (SELECT FID 

 

                              FROM t_BillCodeRule WITH(READUNCOMMITTED) 

 

                           WHERE fbilltypeid = @ClassType 

 

                                 AND fprojectid = 3)

 

 

 

UPDATE t_BillCodeRule 

 

       SET fprojectval = fprojectval + 1 ,

 

               flength = CASE WHEN (flength - LEN(fprojectval + 1))  >= 0

 

                                         THEN flength 

 

                                          ELSE LEN(fprojectval + 1)

 

                               END 

 

WHERE FID = @TmpID 

 

 

 

UPDATE ICBillNo 

 

       SET FCurNo = (SELECT TOP 1 ISNULL(fprojectval - 1 , 1) 

 

                                   FROM t_BillCodeRule 

 

                                WHERE fprojectid = 3 

 

                                      AND fbilltypeid = @ClassType) 

 

WHERE fbillid = @ClassType

 

 

 

-- 更新注释流水号

 

SET @BillID = @BillID + 1

 

UPDATE ICBillNo 

 

       SET FDesc = @BillNo + '+' + REPLICATE('0' , @BillIDLen - LEN(CAST(@BillID AS VARCHAR(50)))) + CAST(@BillID  AS VARCHAR(50))

 

WHERE FBillID = @ClassType

 

  

 

IF @@error = 0

 

  BEGIN

 

          SELECT @RBillNo = @FullBillNo   

 

          COMMIT

 

  END

 

 ELSE

 

      BEGIN

 

               ROLLBACK

 

               SELECT @RBillNo = ''

 

      END

 

RETURN

 

 

 

END

 

posted on 2018-07-16 16:06  RogerLu  阅读(517)  评论(0编辑  收藏  举报