gpt4 book ai didi

mysql - T-SQL 存储过程 CASE 问题

转载 作者:行者123 更新时间:2023-11-29 01:16:35 26 4
gpt4 key购买 nike

你能告诉我我做错了什么吗?

CREATE PROC [dbo].[GetUserEULASelection] (@username VARCHAR(150))
AS
BEGIN
DECLARE @result VARCHAR(250);

SELECT @result = a.NAME
FROM licmn_license_users_accounts_c ua
JOIN accounts a ON a.id = ua.licmn_license_users_accountsaccounts_ida
JOIN licmn_license_users lu ON lu.id = licmn_license_users_accountslicmn_license_users_idb
WHERE lu.username = @username;

CASE
WHEN @result IS NOT NULL
THEN SELECT @result AS 'name'
END
CASE
WHEN @result IS NULL
THEN SELECT '0' AS 'name'
END
END

我是 T-SQL 的新手(我正在将 MySQL 过程移植到 MSSQL)

问题是case语句导致编译错误。

最佳答案

问题是您错误地使用了 CASE。在这种情况下,您将它与 IF 混为一谈

所以你应该正确地做:

IF @result IS NOT NULL
SELECT @result AS name
ELSE
SELECT '0' AS name

如果你想使用 CASE,你应该在查询中使用它:

 SELECT CASE 
WHEN a.NAME IS NULL THEN '0'
ELSE a.NAME
END as Name
FROM licmn_license_users_accounts_c ua
JOIN accounts a ON a.id = ua.licmn_license_users_accountsaccounts_ida
JOIN licmn_license_users lu ON lu.id = licmn_license_users_accountslicmn_license_users_idb
WHERE lu.username = @username;

关于mysql - T-SQL 存储过程 CASE 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25888755/

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