如果是ELSE或使用案例或两者兼而有之

时间:2017-09-19 10:03:25

标签: sql-server sql-server-2008-r2

试图解决这个问题但不确定如何做到这一点。

我需要首先检查表格事件中有人值守条形码是否正确,如果是,它会更新。

如果没有,它需要检查什么不正确:

如果出席不正确,请选择@Err

如果条形码不正确,请选择@ Err2

我不确定这是否是最佳方法。 提前谢谢。

-- Returns scan details
    DECLARE @Err nvarchar(Max) = 'Already scanned'
    DECLARE @Err2 nvarchar(Max) = 'Barcode not found'

    IF (EXISTS(SELECT * FROM [Test].[dbo].[Events] WHERE Attended = '0' AND Barcode = @scan))
        BEGIN
            UPDATE [Test].[dbo].[Events] SET Attended = GETDATE() WHERE Barcode = @scan
            SELECT Title, Forename, Surname, Constituency, Category, Barcode, EventDate, EventName, Attended FROM [Test].[dbo].[Events] WHERE Barcode = @scan
            END
    ELSE
    BEGIN
    -- select @Err
    END

1 个答案:

答案 0 :(得分:0)

我认为你需要这样的东西:

-- Returns scan details
DECLARE @Err nvarchar(Max) = 'Already scanned'
DECLARE @Err2 nvarchar(Max) = 'Barcode not found'

IF (EXISTS(SELECT * FROM [Test].[dbo].[Events] WHERE Attended = '0' AND Barcode = @scan))
    BEGIN
        UPDATE [Test].[dbo].[Events] SET Attended = GETDATE() WHERE Barcode = @scan
        SELECT Title, Forename, Surname, Constituency, Category, Barcode, EventDate, EventName, Attended FROM [Test].[dbo].[Events] WHERE Barcode = @scan
    END
ELSE IF (EXISTS(SELECT * FROM [Test].[dbo].[Events] WHERE Barcode = @scan))
    BEGIN
        select @Err
    END
ELSE
    BEGIN
        SELECT @Err2
    END

问候。