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

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更合适。
但是,您可以完全删除该条件语句以及局部变量:

CREATE PROC [dbo].[GetUserEULASelection] (@username VARCHAR(150))
AS
BEGIN

    SELECT COALESCE(a.NAME, '0') 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;

END

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

10-11 05:21