SQL SERVER stored procedure executes SQL statement stitching with output parameters

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.