SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--exec insert01 1,1-----WZX,2003-03-17
ALTER PROCEDURE DBO.INSERT01(@USERID INTEGER,@docno varchar(10), @RETURN INT OUTPUT) AS
BEGIN
declare @ERROR integer, @ERRMSG varchar(255)
DECLARE @PROP INTEGER
DECLARE @PLUID INTEGER, @PLUNO VARCHAR(20), @QTY numeric(12,4), @RDOCNO CHAR(12), @COMID INTEGER, @DOCDAT smalldatetime, @STKID INTEGER,
@CTYP INT,@TMPDOCNO CHAR(10), @CSPRC numeric(11,4), @PRODDAT smalldatetime,@ISTMP BIT
select @RETURN = 0
---------------------
----检查是否有无供应商的单据
if exists(select 1 from TMPPLU where RDOCNO=@DOCNO AND ISNULL(COMNO,'') NOT IN (SELECT COMNO FROM BASCOMMAIN))
begin
raiserror('您所要导入的进货单供应商不存在a',16,1) with seterror
return
end
----检查是否有无仓位的单据
if exists(select 1 from TMPPLU where RDOCNO=@DOCNO AND ISNULL(STKNO,'') NOT IN (SELECT STKNO FROM BASSTKDEF))
begin
raiserror('您所要导入的进货单仓位不存在',16,1) with seterror
return
end
----检查是否有单据号重复的单据wzx-2003-02-12
if exists(select 1 from TMPPLU where RDOCNO=@DOCNO AND RDOCNO in (select docno from doc01bf))
begin
raiserror('您所要导入的进货单单据号已存在',16,1) with seterror
return
end
if exists(SELECT 1 FROM TMPPLU WHERE rDOCNO=@DOCNO AND RDOCNO IN (SELECT LHDOCNO FROM DOC01BF))
begin
raiserror('您所要导入的进货单单据号已存在',16,1) with seterror
return
end
-------------------------------
BEGIN TRAN
DECLARE TMP_BF CURSOR FOR
SELECT STKID=MAX(C.ID), CTYP=MAX(A.CTYP), RDOCNO=MAX(A.RDOCNO), COMID=MAX(B.COMID), DOCDAT=MAX(A.DOCDAT),ISTMP=ISTMP
FROM TMPPLU A,BASCOMMAIN B,BASSTKDEF C
WHERE A.STKNO=C.STKNO AND A.COMNO=B.COMNO AND A.DOCNO=@DOCNO
group by DOCNO,ISTMP
OPEN TMP_BF
FETCH NEXT FROM TMP_BF
INTO @STKID,@CTYP, @RDOCNO, @COMID, @DOCDAT,@ISTMP
WHILE @@FETCH_STATUS = 0
begin
-----插入主表 WZX,2004-10-26 增加进货单号
INSERT INTO DOC01BF(DOCNO, LHDOCNO, CTYP, INVNO, COMID, STKID, DOCDAT,ISTMP)
VALUES (@RDOCNO, @RDOCNO,@CTYP,'', @COMID, @STKID,@DOCDAT,@ISTMP)
select @ERROR = @@ERROR
if @ERROR <> 0
begin
rollback tran
select @ERRMSG = dbo.GetErrorMSG(@ERROR)
raiserror('在插入进货单主表出错.原始错误号码是[%i],原始错误信息是[%s].', 16, 1, @ERROR, @ERRMSG) with seterror
return
END
-----插入扩展表
INSERT INTO DOC01EXT(DOCNO, RECPSN, RECDAT)
VALUES ( @RDOCNO, @USERID,GETDATE())
select @ERROR = @@ERROR
if @ERROR <> 0
begin
rollback tran
select @ERRMSG = dbo.GetErrorMSG(@ERROR)
raiserror('在插入进货单扩展表出错.原始错误号码是[%i],原始错误信息是[%s].', 16, 1, @ERROR, @ERRMSG) with seterror
return
END
-----插入明细表
INSERT INTO DOC01DT(DOCNO, PLUID, ADDTX, PACTPRC, PKQTY, CASQTY, PCSQTY, QTY, CSPRC, SLPRC, CSAMT, SLAMT, PRODDAT, VALIDDAY, SNO)
SELECT A.RDOCNO,A.PLUID, B.ADDTX, C.CSPRC, D.PKQTY, 0, A.QTY, A.QTY,A.CSPRC, B.SLPRC, A.QTY*A.CSPRC,
A.QTY*B.SLPRC,
PRODDAT, 1000, 1
FROM TMPPLU A,BASPLUMAIN D,BASPLUPRC B
LEFT OUTER JOIN (SELECT COMID=A.COMID, PLUID=B.PLUID, CSPRC=B.CSPRC
FROM DOC70BF A,DOC70DT B
WHERE A.DOCNO=B.DOCNO AND COMID=@COMID
) C
ON B.PLUID=C.PLUID
WHERE A.PLUID=B.PLUID AND A.PLUID=D.PLUID AND A.RDOCNO=@RDOCNO
ORDER BY A.SNO
select @ERROR = @@ERROR
if @ERROR <> 0
begin
rollback tran
select @ERRMSG = dbo.GetErrorMSG(@ERROR)
raiserror('在插入进货单明细表出错.原始错误号码是[%i],原始错误信息是[%s].', 16, 1, @ERROR, @ERRMSG) with seterror
return
END
FETCH NEXT FROM TMP_BF
INTO @STKID,@CTYP, @RDOCNO, @COMID, @DOCDAT,@ISTMP
end
CLOSE TMP_BF
DEALLOCATE TMP_BF
COMMIT TRAN
SELECT @RETURN = 1
SELECT 1
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO