dbo.GetDiscountReport.StoredProcedure.sql
6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
USE [AIADatabaseV5]
GO
/****** Object: StoredProcedure [dbo].[GetDiscountReport] Script Date: 2/1/2018 12:15:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- ====================================================
-- Author: Magic Software
-- Create date: 23-Dec-2009
-- Description: To get the report of discounts on the basis of given parameters
-- ====================================================
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetDiscountReport]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[GetDiscountReport]
GO
CREATE PROCEDURE [dbo].[GetDiscountReport]
-- Add the parameters for the stored procedure here
@sStartDate VARCHAR(20) = '', @sEndDate VARCHAR(20) = '', @intDiscountID INT,
@sAccoutNumber VARCHAR(16)=''
AS
BEGIN
IF 1=0 BEGIN
SET FMTONLY OFF
END
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @dtStartDate DATETIME, @dtEndDate DATETIME
-- convert the datatype of startdate & enddate parameter to datetime
SELECT @dtStartDate = CONVERT(DATETIME,@sStartDate)
SELECT @dtEndDate = DATEADD(ms,-3,DATEADD(DAY,1,CONVERT(DATETIME,@sEndDate)))
IF LEN(@sAccoutNumber) > 0
BEGIN
SELECT Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate,
CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate,
(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
FROM Discount INNER JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
INNER JOIN License ON License.Id = DiscountToLicense.LicenseId
WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END)
AND License.AccountNumber = @sAccoutNumber
GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive
END
ELSE
BEGIN
SELECT Discount.DiscountCode, Discount.Percentage, CONVERT(VARCHAR(10),Discount.StartDate,101) as StartDate,
CONVERT(VARCHAR(10),Discount.EndDate,101) as EndDate,
(CASE Discount.IsActive WHEN 1 THEN 'Active' ELSE 'Inactive' END) AS DiscountStatus,
COUNT(DiscountToLicense.LicenseId) AS TotalLicenses
FROM Discount LEFT JOIN DiscountToLicense ON Discount.Id = DiscountToLicense.DiscountId
WHERE Discount.StartDate >= (CASE WHEN LEN(@sStartDate) > 0 THEN @dtStartDate ELSE Discount.StartDate END)
AND Discount.EndDate <= (CASE WHEN LEN(@sEndDate) > 0 THEN @dtEndDate ELSE Discount.EndDate END)
AND Discount.Id = (CASE WHEN @intDiscountID > 0 THEN @intDiscountID ELSE Discount.Id END)
GROUP BY Discount.DiscountCode, Discount.Percentage, Discount.StartDate, Discount.EndDate, Discount.IsActive
END
END
GO