SQL SERVER 存储过程执行带输出参数的SQL语句拼接

注意:定义的要执行的SQL语句必须是nvarchar类型

CREATE PROC P_BranchOrderSalesStatistics
@beginTime DateTime, --开始时间
@endTime DateTime, --结束时间
@branchCode varchar(20), --门店编码
@NewMemberCount int out, -- 新会员数量
@OrderCount int out, --订单数量
@SalesTotalPrice decimal(18,2) out, --销售额
@ProfitTotalPrice decimal(18,2) out --毛利额
AS
BEGIN

DECLARE @sqlfilter nvarchar(max)
DECLARE @sqlmain nvarchar(max)

 

DECLARE @orderSaleAmount decimal(18,2)
DECLARE @rechargeSaleAmount decimal(18,2)
DECLARE @consumeSaleAmount decimal(18,2)

DECLARE @orderProfitAmount decimal(18,2)
DECLARE @rechargeProfitAmount decimal(18,2)
DECLARE @consumeProfitAmount decimal(18,2)

--订单销售额和订单毛利(毛利=商品销售价-商品配送价)
SET @sqlmain = ' '
SET @sqlfilter = ' 1=1'
SET @sqlfilter = @sqlfilter + ' AND SaleDate >= '''+CAST(@beginTime AS nvarchar(50))+''''
SET @sqlfilter = @sqlfilter + ' AND SaleDate <='''+CAST(@endTime AS nvarchar(50))+''''

IF(@branchCode <> '')
SET @sqlfilter = @sqlfilter + ' AND BranchNo = '''+ @branchCode+''''

SET @sqlmain =@sqlmain + 'SELECT @orderSaleAmount = ISNULL(SUM(OrderSalePrice),0), @orderProfitAmount = ISNULL(SUM(OrderProfitPrice),0) FROM V_Reprot_GoodsOrderStatistics WHERE '
PRINT (@sqlmain + @sqlfilter)
SET @sqlmain = @sqlmain+@sqlfilter
exec sp_executesql @sqlmain,N'@orderSaleAmount decimal(18,2) output, @orderProfitAmount decimal(18,2) output ', @orderSaleAmount output,@orderProfitAmount output

END

 

posted @ 2018-04-19 14:15  剑舞江南  阅读(1191)  评论(0编辑  收藏  举报