注意:定义的要执行的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 [email protected] + ‘SELECT @orderSaleAmount = ISNULL(SUM(OrderSalePrice),0), @orderProfitAmount = ISNULL(SUM(OrderProfitPrice),0) FROM V_Reprot_GoodsOrderStatistics WHERE ‘
PRINT (@sqlmain + @sqlfilter)
SET @sqlmain = @[email protected]
exec sp_executesql @sqlmain,N‘@orderSaleAmount decimal(18,2) output, @orderProfitAmount decimal(18,2) output ‘, @orderSaleAmount output,@orderProfitAmount output
END
原文地址:https://www.cnblogs.com/morpheusliu/p/8882443.html