简体   繁体   English

SQL Server 存储过程将错误的值插入位列

[英]SQL Server stored procedure inserting wrong value into bit column

I have a SQL Server stored procedure as follows我有一个 SQL Server 存储过程如下

ALTER PROCEDURE [dbo].[AccessHistoryUpdate] 
    (@empPk int,
     @doorPk int)
AS
BEGIN
    SELECT ISNULL(@empPk, 0);
    SELECT ISNULL(@doorPk, 0);

    DECLARE @employeeRanking int;
    DECLARE @doorRanking int;

    SELECT
        SUM(e.SecurityDeviceId) AS emp
    FROM
        EmployeeSecurityDevice e
    WHERE
        e.EmployeeId = @empPk;

    SELECT
        SUM(d.SecurityDecivceId) AS door
    FROM
        DoorSecurityDevice d
    WHERE
        d.DoorId = @doorPk;
        
    IF (@employeeRanking >= @doorRanking)
    BEGIN
        INSERT INTO AccessHistory (AttemptDate, DoorId, EmployeeId, Result)
        VALUES(GETDATE(), @doorPk, @empPk, 1);
    END
    ELSE
    BEGIN
        INSERT INTO AccessHistory (AttemptDate, DoorId, EmployeeId, Result)
        VALUES (GETDATE(), @doorPk, @empPk, 0);
    END
END

The result column is a bit column and I wrote if employeeRanking is greater than door ranking, it should return 1. For some reason it's returning 0 (behaving the reverse of what I would have expected)结果列是一个位列,我写道,如果employeeRanking大于门排名,它应该返回1。出于某种原因,它返回0(与我预期的行为相反)

Can someone point me where I'm going wrong?有人可以指出我哪里出错了吗?

Thanks谢谢

You are missing the assignment of the variables, this should do:您缺少变量的分配,应该这样做:

    select 
        @employeeRanking = sum (e.SecurityDeviceId)
    from 
        EmployeeSecurityDevice e
    where 
        e.EmployeeId = @empPk;

    select 
        @doorRanking = sum(d.SecurityDecivceId)
    from 
        DoorSecurityDevice d
    where 
        d.DoorId = @doorPk;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM