sqlserver--存储过程里面调用‘另外的存储过程’




CREATE PROCEDURE [dbo].[proc_CreateSoldOutStock]
	@FBillNo varchar(50),				-- 已售出库单编码
	@FInterID int  output,	    		-- 已售id
	@Errors varchar(50)		output				-- 错误
AS
BEGIN
	declare @FMultiCheckStatus varchar(20)   --//审核流程状态
	declare @FComboBox varchar(20)   --//是否回购
	declare @RKJS_YWType  varchar(20)   --//业务类型
	declare @JSMX_HTH varchar(50)		--合同号
	declare @FDept  int			--部门ID
	declare @FDept_number  varchar(50)  --部门
	declare @RKJS_GYSBH varchar(50)		--客户代码
	declare @RKJS_GYSBH_id  int			--客户ID
	declare @RKJS_XSRQ datetime			--销售日期
	declare @FBase8 int  --币种ID
	declare @RKJS_BZ varchar(20) --RKJS_BZ  币种
	declare @FText6 varchar(10)   --期间
	declare @FEntry int   --行号
	declare @RKJS_Empt_ID int  --业务员ID
	declare @RKJS_Empt varchar(20)   --业务员名称
begin try
	begin tran	
	set  @Errors=''

if exists(select * from t_BOS200000002 where FBillNo =@FBillNo)
	begin
		set @Errors='单据编号为:'+@FBillNo+'---k3已经存在'
		return
	end
if exists(select * from  t_esb_SoldOutStock where  RKJS_DJBH=@FBillNo and IsRead=0)
  begin
	--获取已售出库的ID
		declare @p2 int
		exec GetICMaxNum 't_BOS200000002',@p2 output
		set @FInterID=@p2
  

		
		select @RKJS_YWType=RKJS_YWType,@JSMX_HTH=JSMX_HTH,@RKJS_GYSBH=RKJS_GYSBH,@RKJS_XSRQ=RKJS_XSRQ,
			@RKJS_BZ=RKJS_BZ,@RKJS_Empt=RKJS_Empt from  t_esb_SoldOutStock where  RKJS_DJBH=@FBillNo and IsRead=0
		
		set @FMultiCheckStatus='2'   --//审核流程状态
		set @FComboBox='否'		  --//是否回购
		--公司    默认进出口 page1JsonObj.put("FText", 0);   
		--部门 根据合同号,截取部门
		select @FDept_number=substring(@JSMX_HTH,6,2)+'.'+substring(@JSMX_HTH,8,2)+'.'+substring(@JSMX_HTH,10,3)
		if exists(select top 1 FItemID from t_Department   where  FNumber =@FDept_number)
			begin
				 set @FDept=(select top 1 FItemID from t_Department   where  FNumber =@FDept_number)
			end
		--else
		--	begin
		--		set @FDept=0
		--	end
		--客户
		if exists(select top 1 FItemID from t_Item   where FItemClassID=1 and  FNumber =@RKJS_GYSBH)
			begin
				 set @RKJS_GYSBH_id=(select top 1 FItemID from t_Item   where FItemClassID=1 and  FNumber =@RKJS_GYSBH)
			end
		else
			begin
				set  @Errors ='客户代码为:'+@RKJS_GYSBH+'---k3不存在'
				rollback tran   --回滚
				return
			end
		--期间   GETDATE()截取---》数据库存在Bug
		--select @FText6=cast(cast(substring(cast(isnull(@RKJS_XSRQ,GETDATE())as varchar),6,2) as int)as varchar)
		select @FText6=cast(cast(DATENAME(MONTH,@RKJS_XSRQ) as int)as varchar)
		
		select @FBase8=FCurrencyID from t_Currency where FNumber=@RKJS_BZ  --币种
		
		declare @FDecimal2 decimal(23, 6) --汇率 RKJS_HLv    根据币种,获取汇率,如果是人名币,汇率是1
		if(@RKJS_BZ='CNY')
			begin
				set @FDecimal2=1
			end
		else
			begin
				set @FDecimal2=(select  top 1 FExchangeRate from t_Currency where  FNumber=@RKJS_BZ)
			end	 
		--业务员
		select @RKJS_Empt_ID=FID from t_Emp where FName=@RKJS_Empt
		

		INSERT INTO t_BOS200000002(FID,FClassTypeID,FBillNo,FMultiCheckStatus,FComboBox,FComboBox1,FText,FBase,FBase1,FBase4,FDate,FBiller,FInteger2,FUser,FDate1,FText6,FText12,FBase8,FBase9,FDecimal2) 
			Values
			(@FInterID,291790021,@FBillNo,@FMultiCheckStatus,@FComboBox,@RKJS_YWType,'0',@FDept,@RKJS_GYSBH_id,0,@RKJS_XSRQ,16394,0,16394,@RKJS_XSRQ,@FText6,'',@FBase8,ISNULL(@RKJS_Empt_ID,0),@FDecimal2)

	
		 -----------------------------------------------------------------------------------------
	
		declare @JSMX_WLBH_ID int       -- 物料ID
		declare @JSMX_WLBH_Name varchar(50)-- 物料代码名称
		declare @JSMX_BatchNo varchar(50)	 --VIN/桥序列/批号
		declare @JSMX_SL 	decimal(23, 6)		--数量
		declare @JSMX_JSHJ  decimal(23, 6)		--含税单价  JSMX_JSHJ
		declare @JSMX_BGDH  varchar(50)			--报关单号
		declare @JSMX_BGDWLMC  varchar(50)			--报关单物料名称(报关名称)
		declare @JSMX_YSRQ varchar(500)   --应收日期 	2020-10-11*2020-10-23*2020-10-31
		declare @JSMX_SKJE  varchar(500)    --收款金额 	100*200*300
		declare @JSMX_SKJE_BenWeiBi  varchar(500)    --收款金额本位币 	100*200*300
		declare @JSMX_HTH_ID int        -- 合同号ID
		declare @FBase3 int				-- 	// 单位 FBase3 "辆"
		declare @FDecimal  decimal(8, 2)		--税率
		declare @FPrice1  decimal(23, 6)		--不含税单价
		declare @FAmount3  decimal(23, 6)		--不含税金额
		declare @FAmount5  decimal(23, 6)		--不含税金额本位币
		declare @FAmount  decimal(23, 6)		--含税金额  JSMX_JSHJ 价税合计原币=含税单价*数量 FAmount
		declare @FAmount6  decimal(23, 6)		--含税金额本位币  税合计本位币=含税单价*数量*汇率 @FAmount6
		declare @FAmount1  decimal(23, 6)		--税额=计税合计-金额 @FAmount1
		declare @FAmount7  decimal(23, 6)		--税额本位币=(计税合计-金额)*汇率 @FAmount7
		
		set @JSMX_HTH=''
		set @FEntry=1
  declare cursor_entry cursor for
		select JSMX_WLBH_Name,JSMX_BatchNo,JSMX_SL,JSMX_JSHJ,JSMX_HTH,JSMX_BGDH,JSMX_BGDWLMC,JSMX_YSRQ,JSMX_SKJE from t_esb_SoldOutStock where IsRead=0 and RKJS_DJBH=@FBillNo
	open cursor_entry -- 打开游标
  fetch next from cursor_entry into @JSMX_WLBH_Name,@JSMX_BatchNo,@JSMX_SL,@JSMX_JSHJ,@JSMX_HTH,@JSMX_BGDH,@JSMX_BGDWLMC,@JSMX_YSRQ,@JSMX_SKJE
  while @@fetch_status=0 -- 提取成功,进行下一条数据的提取操作
   BEGIN
			
			--产品代码-FBase2
			set @JSMX_WLBH_ID=0
			if exists(select top 1 FNumber from t_ICItem where  FName=@JSMX_WLBH_Name)
				begin
					set @JSMX_WLBH_ID=(select top 1 FItemID from t_ICItem where FName=@JSMX_WLBH_Name)
				end
			else
				begin
					set  @Errors =@Errors+'物料名称为:'+@JSMX_WLBH_Name+'---k3不存在'
					rollback tran   --回滚
					return
				end
			--// 合同号/合同小号 JSMX_HTH FBase7
			set @JSMX_HTH_ID=0
			if exists(select * from t_Item   where FItemClassID=3007 and FName =@JSMX_HTH)
				begin
					set @JSMX_HTH_ID=(select top 1 FItemID from t_Item   where FItemClassID=3007 and FName =@JSMX_HTH)
				end
			else
				begin
					set  @Errors =@Errors+'合同号名称为:'+@JSMX_HTH+'---k3不存在'
					rollback tran   --回滚
					return
				end
			--	// 单位 FBase3 "辆"
			
			SELECT top 1 @FBase3=FItemID FROM T_MeasureUnit   where FName ='辆'
			--收款金额原币	FText15  JSMX_SKJE     rs7.getString("JSMX_SKJE")
			--收款金额本币	FText14  如果汇率=1   ==JSMX_SKJE
			--						  如果汇率<>1
			set  @JSMX_SKJE_BenWeiBi=0
			if (@RKJS_BZ='CNY')  --汇率=1
				begin
					set @JSMX_SKJE_BenWeiBi=@JSMX_SKJE
				end
			else 
				begin
    
				CREATE TABLE #JSMX_SKJE_BenWeiBi(
					JSMX_SKJE_BenWeiBi    varchar(555)
				)


					set @JSMX_SKJE_BenWeiBi=''
					select @FDecimal2               --收款原币金额  汇率		返回本币金额
					
					INSERT #JSMX_SKJE_BenWeiBi	exec  proc_c_SoldOut_Update_Money  @JSMX_SKJE,@FDecimal2,@JSMX_SKJE_BenWeiBi
					SELECT @JSMX_SKJE_BenWeiBi=#JSMX_SKJE_BenWeiBi.JSMX_SKJE_BenWeiBi FROM #JSMX_SKJE_BenWeiBi;
					DROP TABLE #JSMX_SKJE_BenWeiBi; 
					print @JSMX_SKJE_BenWeiBi
				end
		--// 税率  使用合同号区分 "FDecimal"
		--//SQD内销 ---》‘0.13’   SQZ外销 (   如果SQZ外销 ‘币种’=‘CNY’  )
		if(CHARINDEX('SQD',@JSMX_HTH)=1)  --存在
			begin
				set @FDecimal=0.13
			end
		else
			begin
				set @FDecimal=0
			end
		--// "FPrice1"  不含税单价=含税单价/1.13或者不含税单价=含税单价/(1+税率)
			set @FPrice1=0			
			set @FPrice1=@JSMX_JSHJ/(1+@FDecimal)
		--	// 不含税金额      不含税金额原币=不含税单价*数量 FAmount3
			set @FAmount3=0
			set @FAmount3=(@JSMX_JSHJ/(1+@FDecimal))*@JSMX_SL
		--不含税金额本位币 =不含税金额原币*汇率
			set @FAmount5=0
			set @FAmount5=@FAmount3*@FDecimal2
		--// 含税金额  JSMX_JSHJ 价税合计原币=含税单价*数量 FAmount
			set @FAmount=0
			set @FAmount= @JSMX_JSHJ*@JSMX_SL
		--// 含税金额本位币  税合计本位币=含税单价*数量*汇率
			set @FAmount6=0
			set @FAmount6=@JSMX_JSHJ*@JSMX_SL*@FDecimal2			
		--// 税额=计税合计-金额
			set @FAmount1=0	
			set @FAmount7=0		
			if(@FDecimal=0.13)
				begin
					set @FAmount1=@FAmount-@FAmount3
					set @FAmount7=@FAmount1		--税额本位币
				end
			else
				begin
					set @FAmount1=0
					set @FAmount7=@FAmount1
				end
			
			
			
			
		INSERT INTO t_BOS200000002Entry2
			(FID,FIndex,FBase2,FText2,FText4,
			 FText7,FBase7,FBase3,FQty,FText15,
			 FText14,FDecimal,FPrice1,FPrice,FAmount3,
			 FAmount5,FAmount,FAmount6,FAmount1,FAmount7,
			 FInteger3,FBase6,FText3,FText9,FText5,
			 FText8,FID_SRC,FEntryID_SRC,FBillNo_SRC,FClassID_SRC,
			 FText10,FText1,FText11,FBase5,FDecimal1,
			 FText13) 
		Values
			(@FInterID,@FEntry,@JSMX_WLBH_ID,@JSMX_BatchNo,@JSMX_BGDH,
			 @JSMX_BGDWLMC,@JSMX_HTH_ID,@FBase3,@JSMX_SL,@JSMX_SKJE,
			 @JSMX_SKJE_BenWeiBi,@FDecimal,@FPrice1,@JSMX_JSHJ,@FAmount3,
			 @FAmount5,@FAmount,@FAmount6,@FAmount1,@FAmount7,
			 0,0,'','','',
			 '',0,0,'',0,
			 '','','',0,0,
			 @JSMX_YSRQ)

     set @FEntry=@FEntry+1
     			
   fetch next from cursor_entry into @JSMX_WLBH_Name,@JSMX_BatchNo,@JSMX_SL,@JSMX_JSHJ,@JSMX_HTH,@JSMX_BGDH,@JSMX_BGDWLMC,@JSMX_YSRQ,@JSMX_SKJE
		
   END
  close cursor_entry -- 关闭游标
  deallocate cursor_entry -- 释放游标
------------------------------------------------------------------------------
	
  end
  
	commit tran
	end try
	begin catch
		set @Errors= ERROR_MESSAGE()  --错误的完整信息
		IF @@TRANCOUNT > 0		--判断有没有事务
			BEGIN
				rollback tran
			end
	end catch
		
		select @FInterID					
		select @Errors
END












           

                  获取存储过程最后select返回的结果集。SELECT 数据集返回值

                  因为select返回的结果是一个表。所以返回的结果需要用一个表接收。使用临时表接收。

                 被调用的存储过程最后是这样:返回了一个空标题的字段。

                思路:基本思路是先创建一个临时表,通过INSERT ... EXEC ...语句将存储过程的返回结果保存到临时表中,接下来就可以像处理普通数据表那样对待这个临时表了。

            修改:用临时表接收返回结果集,把临时表的相关字段赋值给变量。

sqlserver--存储过程里面调用‘另外的存储过程’_第1张图片

                           

         

你可能感兴趣的:(sqlserver改错,sqlserver)