gpt4 book ai didi

sql - IF EXISTS语句中的UNION无法正常工作

转载 作者:行者123 更新时间:2023-12-04 18:16:32 26 4
gpt4 key购买 nike

SELECT A, B, C FROM TUser

UNION

IF EXISTS(SELECT dataUserId FROM TUserData WHERE DataId = @dataId AND UserId = @userId)
BEGIN
SELECT @dataUserId = dataUserId FROM TUserData WHERE DataId = @dataId AND UserId = @userId

SELECT A, B, C FROM TUser WHERE UserId = dataUserId
END

最佳答案

您可以像这样转换它:

SELECT @dataUserId  = dataUserId FROM TUserData
WHERE DataId = @dataId AND UserId = @userId

IF (@dataUserId IS NOT NULL)
BEGIN
SELECT A, B, C FROM TUser
UNION
SELECT A, B, C FROM TUser WHERE UserId = @dataUserId
END
ELSE
SELECT A, B, C FROM TUser

关于sql - IF EXISTS语句中的UNION无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2415314/

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