Home Forums SQL Server 2008 SQL Server 2008 - General how to write a stored procedure to check if the members membership has expired RE: how to write a stored procedure to check if the members membership has expired

  • Here's an example of a procedure that takes an email address as input and outputs 0/1 if the user is expired. Of course, you can update the user's IsExpired column within the procedure itself or use this output to do that or whatever you need to do with the status info.

    IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL

    DROP TABLE #TempTable

    CREATE TABLE #TempTable (

    [ID] INT IDENTITY(1,1) NOT NULL,

    [Email] NVARCHAR(50) NULL,

    [ExDate] DATE NULL,

    [isExpired] BIT NULL,

    PRIMARY KEY (ID),

    UNIQUE (ID))

    INSERT INTO #TempTable

    SELECT 'test1@aol.com','2012-11-30',0

    UNION

    SELECT 'test2@aol.com','2012-12-31',0

    UNION

    SELECT 'test3@aol.com','2013-01-31',0

    UNION

    SELECT 'test4@aol.com','2013-02-28',0

    UNION

    SELECT 'test5@aol.com','2012-10-31',1

    CREATE PROCEDURE dbo.GetExpirationValue

    @UserEmail NVARCHAR(50)

    ,@IsExpired BIT OUTPUT

    AS

    BEGIN

    SET NOCOUNT ON

    SELECT

    @IsExpired =

    (CASE

    WHEN DATEDIFF(DAY,GETDATE(),ExDate) < 0 THEN 1

    ELSE 0

    END)

    FROM

    #TempTable

    WHERE

    Email = @UserEmail

    SELECT

    @UserEmail AS UserEmail

    ,@IsExpired AS IsExpired

    END

    /* Run the procedure */

    DECLARE @UserExpired BIT

    EXEC dbo.GetExpirationValue

    @UserEmail = 'test4@aol.com'

    ,@IsExpired = @UserExpired OUTPUT

    EXEC dbo.GetExpirationValue

    @UserEmail = 'test5@aol.com'

    ,@IsExpired = @UserExpired OUTPUT

    The output:

    UserEmailIsExpired

    test4@aol.com0

    UserEmailIsExpired

    test5@aol.com1