Note: The defined SQL statement to execute must be of type nvarchar
CREATE PROC P_branchordersalesstatistics
@beginTime DateTime,--Start time
@endTime DateTime,--End time
@branchCode varchar (20),--store code
@NewMemberCount int out,--number of new members
@OrderCount int out,--order quantity
@SalesTotalPrice Decimal (18,2) out,--Sales
@ProfitTotalPrice Decimal (18,2) out--gross margin
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)
--order sales and order margin (gross margin = commodity sales price-product delivery price)
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 (SU M (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
SQL SERVER stored procedure executes SQL statement stitching with output parameters