我有两个非常相似的表,并且在运行以下查询时遇到此错误:
在包含顶级UNION,INTERSECT或EXCEPT运算符的语句中不允许进行变量赋值
请告诉我如何解决?
SELECT @EID = [EId]
FROM [HR].[E]
WHERE @LName = ltrim(rtrim([LName]))
UNION
SELECT @EID = [EId]
FROM [HR].[EArchive]
WHERE @LName = ltrim(rtrim([LName]))
答案 0 :(得分:1)
在不了解您正在做的其他事情或担心多次点击等情况下,查询可能是:
SELECT @EID = [EId] from (
select Eid
FROM [HR].[E]
WHERE @LName = ltrim(rtrim([LName]))
UNION
SELECT [EId]
FROM [HR].[EArchive]
WHERE @LName = ltrim(rtrim([LName]))
) q