gpt4 book ai didi

sql-server-2008 - T-SQL if 语句错误

转载 作者:行者123 更新时间:2023-12-03 23:38:07 24 4
gpt4 key购买 nike

我正在尝试执行以下 SQL 2008 代码,它说“=”和“else”附近有问题……我不明白代码有什么问题

ALTER PROCEDURE dbo.LoginEmp @username NVARCHAR(10),
@password NVARCHAR(10),
@confirm INT output,
@emp_name NVARCHAR(50) output,
@emp_id BIGINT output
AS
IF EXISTS (SELECT @emp_id = emp_id,
@emp_name = emp_name_ara
FROM Employee
WHERE ( emp_username LIKE @username
AND emp_password LIKE @password ))
BEGIN
SET @confirm=1

INSERT INTO EmployeeLog
(log_emp_id,
log_act_id,
log_date,
log_data)
VALUES (@emp_id,
1,
GETDATE(),
-1)
END
ELSE
BEGIN
SET @confirm=0
END

RETURN

最佳答案

而不是尝试分配给 EXISTS 中的输出参数做作业然后检查 @@rowcount查看是否找到任何匹配的行。

ALTER PROCEDURE dbo.LoginEmp @username NVARCHAR(10),
@password NVARCHAR(10),
@confirm INT output,
@emp_name NVARCHAR(50) output,
@emp_id BIGINT output
AS
SELECT @emp_id = emp_id,
@emp_name = emp_name_ara
FROM Employee
WHERE ( emp_username = @username
AND emp_password = @password )

IF @@ROWCOUNT = 1
BEGIN
SET @confirm=1

INSERT INTO EmployeeLog
(log_emp_id,
log_act_id,
log_date,
log_data)
VALUES (@emp_id,
1,
GETDATE(),
-1)
END
ELSE
BEGIN
SET @confirm=0
END

关于sql-server-2008 - T-SQL if 语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com