日期:2014-05-18  浏览次数:20500 次

急急急!怎么执行这个存储过程啊!

select SaleDay,sum(orderMoney)as OrderMoney,sum(OrderQty) as OrderQty,sum(RtnCash) as RtnCash,sum(RtnCount) as RtnCount from
(
select convert(date,CreateTime) as SaleDay,sum(shouldpay) as OrderMoney,count(orderId) as OrderQty,0 as RtnCash,0 AS RtnCount 
from dbo.Sales_Order--订单销售表
where status>=3 and CreateTime>convert(date,'{@BeginDate}') and CreateTime<convert(date,dateadd(day,1,'{@EndDate}'))and saleBy='{@saleBy}'
group by convert(date,CreateTime)
union all
select convert(date,CreateTime) as SaleDay,0 as OrderMoney,0 as OrderQty,SUM(RtnCash) as RtnCash,count(*) as RtnCount 
FROM dbo.Sales_RtnOrder--订单退换表
where status>=1 and CreateTime>convert(date,'{@BeginDate}') and CreateTime<convert(date,dateadd(day,1,'{@EndDate}'))
group by convert(date,CreateTime)
)a group by SaleDay--销售天数


我想 在数据库执行一下查询(目前这个脚本)




------解决方案--------------------
SQL code

CREATE PROCEDURE YourPROCname
    (
      @BeginDate VARCHAR(30) ,
      @EndDate VARCHAR(30) ,
      @saleBy INT
    )
AS 
    BEGIN
        SELECT  SaleDay ,
                SUM(orderMoney) AS OrderMoney ,
                SUM(OrderQty) AS OrderQty ,
                SUM(RtnCash) AS RtnCash ,
                SUM(RtnCount) AS RtnCount
        FROM    ( SELECT    CONVERT(DATE, CreateTime) AS SaleDay ,
                            SUM(shouldpay) AS OrderMoney ,
                            COUNT(orderId) AS OrderQty ,
                            0 AS RtnCash ,
                            0 AS RtnCount
                  FROM      dbo.Sales_Order--订单销售表
                  WHERE     status >= 3
                            AND CreateTime > CONVERT(DATE, @BeginDate)
                            AND CreateTime < CONVERT(DATE, DATEADD(day, 1,
                                                              @EndDate))
                            AND saleBy = @saleBy
                  GROUP BY  CONVERT(DATE, CreateTime)
                  UNION ALL
                  SELECT    CONVERT(DATE, CreateTime) AS SaleDay ,
                            0 AS OrderMoney ,
                            0 AS OrderQty ,
                            SUM(RtnCash) AS RtnCash ,
                            COUNT(*) AS RtnCount
                  FROM      dbo.Sales_RtnOrder--订单退换表
                  WHERE     status >= 1
                            AND CreateTime > CONVERT(DATE, @BeginDate)
                            AND CreateTime < CONVERT(DATE, DATEADD(day, 1,
                                                              @EndDate))
                  GROUP BY  CONVERT(DATE, CreateTime)
                ) a
        GROUP BY SaleDay--销售天数
    END


--执行存储过程
EXEC YourPROCname '2010-02-01','2011-02-01',45