SQL 服务器返回一个值并检查是否存在于同一个 select(一个 select 操作)
SQL Server returning a value and checking if exists on the same select (one select operation)
我和员工有一个 table。 table 有一个 BIT 类型的列 (FlagActive),表示员工当前是活跃 (1) 还是非活跃 (0)。
例如,为了简化场景,table 将是:
Id | EmployeeName | FlagActive
我有一个存储过程来检查 table 中是否存在雇员。如果存在,它会做一些事情。否则它会做其他事情。所以我这样做:
DECLARE @IsActive BIT
SELECT @IsActive = FlagActive FROM Employees WHERE Id = @IdEmployee
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
如果员工始终存在于 table 上,则效果很好,但如果员工不存在于 table 上,则满足 @IsActive = 0 并执行 ELSE 正文。这是不正确的:如果员工不存在,则不应执行任何操作。
那我试着做下面的事情:
IF EXISTS(SELECT TOP 1 * FROM Employees WHERE Id = @IdEmployee)
BEGIN
DECLARE @IsActive BIT
SELECT @IsActive = FlagActive FROM Employees WHERE Id = @IdEmployee
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
END
这种方法的问题是我需要做 2 个操作:第一个 select 检查员工是否存在,第二个(一旦我知道员工存在于 table)检查员工是否存在活跃或不活跃。
我想在 on select 操作中做这两个操作(相同 select)并避免做两个 select 操作。
有什么想法吗?
尝试改变你的其他方式:
ELSE IF @IsActive = 0 AND @IsActive IS NOT NULL
BEGIN
-- Employee is inactive: Do some stuff
END
--You could change your IF Exists to just a select Active where employeeId = Id ,
--if it is NULL they don't exist, else select the active value.
DECLARE @isActive BIT
--Instead of IF EXISTS, just set the BIT value to whatever is returned,
--If NULL they don't exist
SET @isActive = SELECT TOP 1 [FlagActive] FROM Employees WHERE Id = @IdEmployee
IF @isActive IS NOT NULL
BEGIN
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
END
我和员工有一个 table。 table 有一个 BIT 类型的列 (FlagActive),表示员工当前是活跃 (1) 还是非活跃 (0)。
例如,为了简化场景,table 将是:
Id | EmployeeName | FlagActive
我有一个存储过程来检查 table 中是否存在雇员。如果存在,它会做一些事情。否则它会做其他事情。所以我这样做:
DECLARE @IsActive BIT
SELECT @IsActive = FlagActive FROM Employees WHERE Id = @IdEmployee
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
如果员工始终存在于 table 上,则效果很好,但如果员工不存在于 table 上,则满足 @IsActive = 0 并执行 ELSE 正文。这是不正确的:如果员工不存在,则不应执行任何操作。
那我试着做下面的事情:
IF EXISTS(SELECT TOP 1 * FROM Employees WHERE Id = @IdEmployee)
BEGIN
DECLARE @IsActive BIT
SELECT @IsActive = FlagActive FROM Employees WHERE Id = @IdEmployee
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
END
这种方法的问题是我需要做 2 个操作:第一个 select 检查员工是否存在,第二个(一旦我知道员工存在于 table)检查员工是否存在活跃或不活跃。
我想在 on select 操作中做这两个操作(相同 select)并避免做两个 select 操作。
有什么想法吗?
尝试改变你的其他方式:
ELSE IF @IsActive = 0 AND @IsActive IS NOT NULL
BEGIN
-- Employee is inactive: Do some stuff
END
--You could change your IF Exists to just a select Active where employeeId = Id ,
--if it is NULL they don't exist, else select the active value.
DECLARE @isActive BIT
--Instead of IF EXISTS, just set the BIT value to whatever is returned,
--If NULL they don't exist
SET @isActive = SELECT TOP 1 [FlagActive] FROM Employees WHERE Id = @IdEmployee
IF @isActive IS NOT NULL
BEGIN
IF @IsActive = 1
BEGIN
-- Employee is active: Do some stuff
END
ELSE
BEGIN
-- Employee is inactive: Do some stuff
END
END