USE [AppServices]
GO
/****** Object: StoredProcedure [dbo].[Batch_GetNonTransferableMembers_p] Script Date: 07/06/2011 11:24:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[Batch_GetNonTransferableMembers_p]
@Batch_id AS INT
, @IsIncludeDetail AS BIT
AS
-- =============================================
-- Author: Richard.Chen
-- Create date: 2011/5/30
-- Description: ALTER PROCEDURE Batch_GetNonTransferableMembers_p for task 28652
-- Returns Non-Transferable member count of the specified batch by default, and return details as well if @IsIncludeDetail is true
-- =============================================
-- Save Transferable BatchItem into #temp table
SELECT b.BatchItem_id INTO #temp
FROM AppServices.dbo.BatchItem AS b WITH (NOLOCK)
INNER JOIN ET_Commerce.dbo.Subscriptions AS s WITH (NOLOCK) ON s.MemberId = b.Member_id
INNER JOIN ET_Commerce.dbo.Redemptions AS r WITH (NOLOCK) ON r.Code = s.PromoCode
INNER JOIN ET_Commerce.dbo.Licenses AS l WITH (NOLOCK) ON l.LicenseId = r.LicenseId
WHERE b.Batch_id = @Batch_id
AND b.IsDeleted = 0
AND b.Member_Id IS NOT NULL
AND s.IsActive = 1
AND l.IsTransferable = 1
-- Return NonTransferable member count
SELECT COUNT(1) FROM dbo.BatchItem WITH (NOLOCK)
WHERE Batch_id = @Batch_id
AND IsDeleted = 0
AND BatchItem_id NOT IN (SELECT BatchItem_id FROM #temp)
-- Return NonTransferable member details if @IsIncludeDetail is true
IF @IsIncludeDetail = 1
BEGIN
;WITH CTE AS
(
SELECT DISTINCT b.Member_id
, Email = CASE
WHEN m.Email IS NULL THEN b.Email COLLATE SQL_Latin1_General_CP1_CI_AS
ELSE m.Email
END
, r.Code AS LicenseCode
, cd.DivisionCode
, ROW_NUMBER() OVER (PARTITION BY m.MemberId ORDER BY s.SubscriptionId DESC) AS num
FROM AppServices.dbo.BatchItem AS b WITH (NOLOCK)
LEFT JOIN ET_Main.dbo.Members AS m WITH (NOLOCK) ON m.MemberId = b.Member_id
LEFT JOIN ET_Commerce.dbo.Subscriptions AS s WITH (NOLOCK) ON s.MemberId = b.Member_id
LEFT JOIN ET_Commerce.dbo.Redemptions AS r WITH (NOLOCK) ON r.Code = s.PromoCode COLLATE SQL_Latin1_General_CP1_CI_AS
LEFT JOIN ET_Commerce.dbo.Licenses AS l WITH (NOLOCK) ON l.LicenseId = r.LicenseId
LEFT JOIN ET_Corporate.dbo.CorporateMembers AS cm WITH (NOLOCK) ON cm.MemberId = b.Member_id
LEFT JOIN ET_Corporate.dbo.CorporateDivision AS cd WITH (NOLOCK) ON cd.DivisionId = cm.DivisionId
WHERE b.Batch_id = @Batch_id
AND b.IsDeleted = 0
AND b.BatchItem_id NOT IN (SELECT BatchItem_id FROM #temp)
)
SELECT Member_id, Email, LicenseCode, DivisionCode FROM CTE WHERE num = 1
END
RETURN
GO
GRANT EXECUTE ON [dbo].[Batch_GetNonTransferableMembers_p] TO [CommerceBaseRole] AS [dbo]
GO
/****** Object: StoredProcedure [dbo].[Batch_GetNonTransferableMembers_p] Script Date: 07/06/2011 11:24:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[Batch_GetNonTransferableMembers_p]
@Batch_id AS INT
, @IsIncludeDetail AS BIT
AS
-- =============================================
-- Author: Richard.Chen
-- Create date: 2011/5/30
-- Description: ALTER PROCEDURE Batch_GetNonTransferableMembers_p for task 28652
-- Returns Non-Transferable member count of the specified batch by default, and return details as well if @IsIncludeDetail is true
-- =============================================
-- Save Transferable BatchItem into #temp table
SELECT b.BatchItem_id INTO #temp
FROM AppServices.dbo.BatchItem AS b WITH (NOLOCK)
INNER JOIN ET_Commerce.dbo.Subscriptions AS s WITH (NOLOCK) ON s.MemberId = b.Member_id
INNER JOIN ET_Commerce.dbo.Redemptions AS r WITH (NOLOCK) ON r.Code = s.PromoCode
INNER JOIN ET_Commerce.dbo.Licenses AS l WITH (NOLOCK) ON l.LicenseId = r.LicenseId
WHERE b.Batch_id = @Batch_id
AND b.IsDeleted = 0
AND b.Member_Id IS NOT NULL
AND s.IsActive = 1
AND l.IsTransferable = 1
-- Return NonTransferable member count
SELECT COUNT(1) FROM dbo.BatchItem WITH (NOLOCK)
WHERE Batch_id = @Batch_id
AND IsDeleted = 0
AND BatchItem_id NOT IN (SELECT BatchItem_id FROM #temp)
-- Return NonTransferable member details if @IsIncludeDetail is true
IF @IsIncludeDetail = 1
BEGIN
;WITH CTE AS
(
SELECT DISTINCT b.Member_id
, Email = CASE
WHEN m.Email IS NULL THEN b.Email COLLATE SQL_Latin1_General_CP1_CI_AS
ELSE m.Email
END
, r.Code AS LicenseCode
, cd.DivisionCode
, ROW_NUMBER() OVER (PARTITION BY m.MemberId ORDER BY s.SubscriptionId DESC) AS num
FROM AppServices.dbo.BatchItem AS b WITH (NOLOCK)
LEFT JOIN ET_Main.dbo.Members AS m WITH (NOLOCK) ON m.MemberId = b.Member_id
LEFT JOIN ET_Commerce.dbo.Subscriptions AS s WITH (NOLOCK) ON s.MemberId = b.Member_id
LEFT JOIN ET_Commerce.dbo.Redemptions AS r WITH (NOLOCK) ON r.Code = s.PromoCode COLLATE SQL_Latin1_General_CP1_CI_AS
LEFT JOIN ET_Commerce.dbo.Licenses AS l WITH (NOLOCK) ON l.LicenseId = r.LicenseId
LEFT JOIN ET_Corporate.dbo.CorporateMembers AS cm WITH (NOLOCK) ON cm.MemberId = b.Member_id
LEFT JOIN ET_Corporate.dbo.CorporateDivision AS cd WITH (NOLOCK) ON cd.DivisionId = cm.DivisionId
WHERE b.Batch_id = @Batch_id
AND b.IsDeleted = 0
AND b.BatchItem_id NOT IN (SELECT BatchItem_id FROM #temp)
)
SELECT Member_id, Email, LicenseCode, DivisionCode FROM CTE WHERE num = 1
END
RETURN
GO
GRANT EXECUTE ON [dbo].[Batch_GetNonTransferableMembers_p] TO [CommerceBaseRole] AS [dbo]