返回底部sql错误的特定行

时间:2015-06-23 23:45:35

标签: sql-server

CREATE PROC [dbo].[RptUsageReport] 
  @branchId    INT=NULL, 
  @strDate     DATETIME=NULL, 
  @endDate     DATETIME=NULL, 
  @customerCat INT 
AS 
  BEGIN 
    SELECT   bd.bandcharge_desc, 
             bd.rate, 
             Sum(bd.qty)    AS qty, 
             Sum(bd.amount) AS amount 
    FROM     bill_details bd 
    WHERE    bd.bandcharge_desc LIKE '%days%' 
    AND      bd.bill_gen_id IN 
             ( 
                    SELECT bg.id 
                    FROM   billing_gen bg 
                    JOIN   customer_registration cr 
                    ON     bg.account_number = cr.account_number 
                    JOIN   reading r 
                    ON     r.id = bg.reading_id 
                    WHERE  cr.customer_category_id = @customerCat 
                    AND    cr.branch_id=@branchId 
                    AND    (( 
                                         @strDate IS NULL) 
                           OR     Cast(Floor(Cast(r.enddate AS FLOAT)) AS DATETIME) >= Cast(Floor(Cast(@strDate AS FLOAT)) AS DATETIME))
                    AND    (( 
                                         @endDate IS NULL) 
                           OR     Cast(Floor(Cast(r.enddate AS FLOAT)) AS DATETIME) <= Cast(Floor(Cast(@endDate AS FLOAT)) AS DATETIME)))
    GROUP BY bd.bandcharge_desc, 
             bd.rate 
    ORDER BY 
             CASE 
                      WHEN bd.bandcharge_desc = 'Usage above 300 units for 30 days' THEN '1' 
                      ELSE bd.bandcharge_desc 
             END ASC;

我有上面的存储过程,我希望在结果集的顶部放置一个特定的行,但是当我运行查询时它会给我一个错误 - 语法不正确&#39;;&#39; < / p>

语法错误在哪里?

1 个答案:

答案 0 :(得分:2)

您需要另一个END来结束创建过程语句。分号是意料之外的,因为您的END是CASE语句。

    CREATE PROC [dbo].[RptUsageReport] 
      @branchId    INT=NULL, 
      @strDate     DATETIME=NULL, 
      @endDate     DATETIME=NULL, 
      @customerCat INT 
    AS 
      BEGIN 
        SELECT   bd.bandcharge_desc, 
                 bd.rate, 
                 Sum(bd.qty)    AS qty, 
                 Sum(bd.amount) AS amount 
        FROM     bill_details bd 
        WHERE    bd.bandcharge_desc LIKE '%days%' 
        AND      bd.bill_gen_id IN 
                 ( 
                        SELECT bg.id 
                        FROM   billing_gen bg 
                        JOIN   customer_registration cr 
                        ON     bg.account_number = cr.account_number 
                        JOIN   reading r 
                        ON     r.id = bg.reading_id 
                        WHERE  cr.customer_category_id = @customerCat 
                        AND    cr.branch_id=@branchId 
                        AND    (( 
                                             @strDate IS NULL) 
                               OR     Cast(Floor(Cast(r.enddate AS FLOAT)) AS DATETIME) >= Cast(Floor(Cast(@strDate AS FLOAT)) AS DATETIME))
                        AND    (( 
                                             @endDate IS NULL) 
                               OR     Cast(Floor(Cast(r.enddate AS FLOAT)) AS DATETIME) <= Cast(Floor(Cast(@endDate AS FLOAT)) AS DATETIME)))
        GROUP BY bd.bandcharge_desc, 
                 bd.rate 
        ORDER BY 
                 CASE 
                          WHEN bd.bandcharge_desc = 'Usage above 300 units for 30 days' THEN '1' 
                          ELSE bd.bandcharge_desc 
                 END ASC
    END;