oracle代码
select PERSONID
into v_personID
from HAIL_ESS_USERS_EXTENDED_V
where EID = p_eid
and IS_ACTIVE = 'Y'
and rownum = 1;
sql server code
set @v_PersonId = (select PersonId
from viwSSAppsEmpMasterExtended
where EID = @p_EID
and IsEmployeeActive = 'Y'
and @@rowcount=1)
这是正确的还是可能需要进行一些更改......?
答案 0 :(得分:1)
@@ROWCOUNT
不能像这样工作,@@ROWCOUNT
将返回受前一个语句影响的行数,因此在以下示例中:
SELECT A = 1 UNION ALL SELECT 1;
SELECT *
FROM (select A = 1 union all select 1) AS t
WHERE @@ROWCOUNT = 1;
因为第一个语句返回两行,所以第二个返回none(因为@@ROWCOUNT
= 2,而不是1)。如果您只想获得第一个结果,则需要使用TOP
:
set @v_PersonId = (select top 1 PersonId
from viwSSAppsEmpMasterExtended
where EID = @p_EID
and IsEmployeeActive = 'Y')
但是,如果没有ORDER BY
条款,TOP
几乎毫无意义,您可以使用相同的数据运行此查询10并获得10个不同的人。
答案 1 :(得分:0)
在sql server查询中使用top
而不是@@rowcount
:
set @v_PersonId = (select top 1 PersonId
from viwSSAppsEmpMasterExtended
where EID = @p_EID
and IsEmployeeActive = 'Y')