码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-04-19 14:56:43      阅读:493      评论:0      收藏:0      [点我收藏+]

标签:ber   存储过程   输出参数   roc   销售额   serve   sql   销售   var   

注意:定义的要执行的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

 

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

标签:ber   存储过程   输出参数   roc   销售额   serve   sql   销售   var   

原文地址:https://www.cnblogs.com/morpheusliu/p/8882443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!