使用IF的存储过程

时间:2017-03-07 12:57:38

标签: sql sql-server-2008 if-statement stored-procedures

我需要一些帮助,我对SQL有点新手。我有一个存储过程,从另一个应用程序作为智能对象执行。更改之前的代码看起来像第1节中的代码。我根据第2节更改了代码,以便存储过程首先查看Sales ID类型实际上是3与存储过程的表成员资格中的成员资格数比较然后在members表中执行相同的操作。如果查询是3,其中包含会员编号,我希望它在成员表中使用到期日期而不是

部分

SELECT @ExpiryDate = DATEADD(YEAR,1,DATEADD(d,-1,DATEADD(m,0,@ ManagingDate)))

该woudl将364天添加到当前日期

SECTION1

USE [HotelStayWorkflow]
GO
/****** Object:  StoredProcedure [dbo].[CP_Create_Card]    Script Date: 3/7/2017 2:50:06 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[CP_Create_Card] 
    -- Add the parameters for the stored procedure here
 @membershipNumber VARCHAR(MAX),
 @CardId BIGINT = NULL OUTPUT,
 @minsToFirstDeadline int = 2880,--defailt 2 days
 @minsToSecondDeadline int = 5760 --default 4 days



AS
BEGIN
    DECLARE @CreatedDate DATETIME = GETDATE()
    DECLARE @ExpiryDate DATETIME
    DECLARE @FirstDeadine DATETIME
    DECLARE @SecondDeadline DATETIME

    BEGIN
                SELECT @ExpiryDate = DATEADD(YEAR, 1, DATEADD(d, -1, DATEADD(m, 0, @CreatedDate)))
    END


    SELECT @FirstDeadine = DATEADD(MINUTE,@minsToFirstDeadline,GETDATE())
    SELECT @SecondDeadline = DATEADD(MINUTE,@minsToSecondDeadline,GETDATE())
    --Create Card Linked to Membership below
    IF EXISTS(SELECT * FROM Cards WHERE MembershipNumber = @membershipNumber AND Enabled = 1)
    BEGIN
        UPDATE Cards SET Enabled = 0 WHERE MembershipNumber = @membershipNumber AND Enabled = 1
    END

    INSERT INTO Cards (CardStatusId, MembershipNumber, PrintedDate, Created, Modified, ExpiryDate,FirstDeadline,SecondDeadline, Enabled) 
    VALUES(1, @membershipNumber, @CreatedDate, @CreatedDate, @CreatedDate, @ExpiryDate,@FirstDeadine,@SecondDeadline, 1)    
    SET @CardId = SCOPE_IDENTITY()

            -- Return Card Id


END

第2节

USE [HotelStayWorkflow]
GO
/****** Object:  StoredProcedure [dbo].[CP_Create_Card]    Script Date: 3/7/2017 1:08:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[CP_Create_Card] 
    -- Add the parameters for the stored procedure here
 @membershipNumber VARCHAR(MAX),
 @CardId BIGINT = NULL OUTPUT,
 @minsToFirstDeadline int = 2880,--defailt 2 days
 @minsToSecondDeadline int = 5760 --default 4 days



AS
BEGIN
    DECLARE @CreatedDate DATETIME = GETDATE()
    DECLARE @ExpiryDate DATETIME
    DECLARE @FirstDeadine DATETIME
    DECLARE @SecondDeadline DATETIME

    BEGIN
            IF EXIST (FROM Memberships WHERE MembershipNumber = @MembershipNumber AND SalesTypeId = 3 AND Enabled =1)
            AND 
            IF EXIST (FROM Members WHERE MembershipNumber = @MembershipNumber AND Enabled =1)
            SELECT @ExpiryDate = dbo.Members.MembershipExpiry
        OR              

            SELECT @ExpiryDate = DATEADD(YEAR, 1, DATEADD(d, -1, DATEADD(m, 0, @CreatedDate)))
    END


    SELECT @FirstDeadine = DATEADD(MINUTE,@minsToFirstDeadline,GETDATE())
    SELECT @SecondDeadline = DATEADD(MINUTE,@minsToSecondDeadline,GETDATE())
    --Create Card Linked to Membership below
    IF EXISTS(SELECT * FROM Cards WHERE MembershipNumber = @membershipNumber AND Enabled = 1)
    BEGIN
        UPDATE Cards SET Enabled = 0 WHERE MembershipNumber = @membershipNumber AND Enabled = 1
    END

    INSERT INTO Cards (CardStatusId, MembershipNumber, PrintedDate, Created, Modified, ExpiryDate,FirstDeadline,SecondDeadline, Enabled) 
    VALUES(1, @membershipNumber, @CreatedDate, @CreatedDate, @CreatedDate, @ExpiryDate,@FirstDeadine,@SecondDeadline, 1)    
    SET @CardId = SCOPE_IDENTITY()

            -- Return Card Id


END

2 个答案:

答案 0 :(得分:0)

请尝试这样做,因为您没有包含所有列,因此很难完成此答案。

   SELECT @ExpiryDate = M.MembershipExpiry From Memberships M WHERE M.MembershipNumber = @MembershipNumber AND M.SalesTypeId = 3 AND M.[Enabled] =1

        IF @ExpiryDate IS NULL
        BEGIN
           SELECT @ExpiryDate = DATEADD(YEAR, 1, DATEADD(d, -1, DATEADD(m, 0, @CreatedDate)))
        end

答案 1 :(得分:0)

将代码更改为

BEGIN     SELECT @ExpiryDate = MembershipExpiry来自会员M WHERE M.MembershipNumber = @MembershipNumber
    成员资格MS WHERE M.MembershipNumber = @MembershipNumber AND SalesTypeId = 3 AND MS。[Enabled] = 1

    IF @ExpiryDate IS NULL
            SELECT @ExpiryDate = DATEADD(YEAR, 1, DATEADD(d, -1, DATEADD(m, 0, @CreatedDate)))
END