如何在 SQL 服务器存储过程中使用 if else
How to use if else in SQL Server stored procedure
存储过程:
CREATE PROCEDURE validate
@blockID VARCHAR(255)
AS
BEGIN
IF @blockID = (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT 'exists'
IF @blockID != (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT 'does not exist'
END
当输入错误的第一个blockID时,第二个if条件不起作用。
您可能想使用 EXISTS
。
CREATE PROCEDURE validate
@blockID VARCHAR(255)
AS
BEGIN
IF EXISTS (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT'exists'
ELSE
PRINT'not exists'
END
存储过程:
CREATE PROCEDURE validate
@blockID VARCHAR(255)
AS
BEGIN
IF @blockID = (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT 'exists'
IF @blockID != (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT 'does not exist'
END
当输入错误的第一个blockID时,第二个if条件不起作用。
您可能想使用 EXISTS
。
CREATE PROCEDURE validate
@blockID VARCHAR(255)
AS
BEGIN
IF EXISTS (SELECT blockID FROM block WHERE blockID = @blockID)
PRINT'exists'
ELSE
PRINT'not exists'
END