使用A位(bool)检入Sql查询

时间:2014-12-12 18:58:32

标签: sql-server tsql

我需要检查某人是否病了,不幸的是,数据存储在2个单独的表中。如果它们在这里,我需要执行路径A,如果它们不在这里我需要执行路径B.这就是我在想的但是它在编译错误后产生编译错误。有人可以提供指导吗?

Create Table #Uno (Id Int Identity, employee varchar(50))
Select empName
From employeeDatabase
WHERE empStatus = 'Active'


Declare @outSick bit, @empName varchar(50), @N int, @MAXID int

Set @N = 1
SELECT @MAXID = Max(ID)
FROM @Uno
WHILE (@N<=MAXID)
BEGIN
--Second Table that stores this data
Select @outSick = callIn
FROM callinDatabase
WHERE empName = @empName

--Returns Yes or True
If @outSick = 1
    Set @N = @N + 1
Else
    --If not out sick Do this sql statement
    Set @N = @N + 1
End

2 个答案:

答案 0 :(得分:0)

Create Table #Uno (Id Int Identity, employee varchar(50))
Select empName
From employeeDatabase
WHERE empStatus = 'Active'


Declare @outSick bit, @empName varchar(50), @N int, @MAXID int

Set @N = 1
SELECT @MAXID = Max(ID)
FROM #Uno                 <----- HERE
WHILE (@N<=@MAXID)        <------- HERE
BEGIN
--Second Table that stores this data
Select @outSick = callIn
FROM callinDatabase
WHERE empName = @empName

--Returns Yes or True
If @outSick = 1
    Set @N = @N + 1
Else
    --If not out sick Do this sql statement
    Set @N = @N + 1
End                      <-------------- Here

答案 1 :(得分:0)

为什么不能在一个查询中执行此操作,如下所示?

select 
    employeeDatabase.empName,
    case
        when callinDatabase.callin = '1' then 'Out Sick'
        else 'Not Out Sick'
    end as OutSick
from employeeDatabase left join callinDatabase
    on employeeDatabase.empName = callinDatabase.empName
where employeeDatabase.empStatus = 'active'