试试这个——
ALTER PROCEDURE [dbo].[spGetAllCandidatesEmployeesByOrgId]
@ip_OrgId INT,
@Active_XML BIT
AS BEGIN
DECLARE
@Entity VARCHAR(10)
, @EntityID BIGINT
, @LastModifiedOn DATETIME
, @RowNum BIGINT
DECLARE cursor_name CURSOR FAST_FORWARD READ_ONLY FOR
SELECT
q.Entity
, q.EntityID
, q.LastModifiedOn
, RowNo = ROW_NUMBER() OVER (ORDER BY q.LastModifiedOn DESC)
FROM (
SELECT
'Candidate' AS [Entity]
, v.ProposalId AS EntityID
, v.LastModifiedOn AS LastModifiedOn
FROM dbo.viwProposals v
WHERE OrgId=@ip_OrgId AND IsHide = 0
UNION
SELECT
'Employee'
, v.EmployeeId
, v.LastModifiedOn
FROM dbo.viwEmployees v
WHERE OrgId=@ip_OrgId AND IsTerminate = 0
) q
ORDER BY LastModifiedOn DESC
OPEN cursor_name
FETCH NEXT FROM cursor_name INTO
@Entity
, @EntityID
, @LastModifiedOn
, @RowNum
WHILE @@FETCH_STATUS = 0 BEGIN
--
FETCH NEXT FROM cursor_name INTO
@Entity
, @EntityID
, @LastModifiedOn
, @RowNum
END
CLOSE cursor_name;
DEALLOCATE cursor_name;
END
或者试试这个 -
ALTER PROCEDURE [dbo].[spGetAllCandidatesEmployeesByOrgId]
@ip_OrgId INT,
@Active_XML BIT
AS BEGIN
DECLARE
@Entity VARCHAR(10)
, @EntityID BIGINT
, @LastModifiedOn DATETIME
, @RowNum BIGINT
DECLARE cursor_name CURSOR FAST_FORWARD READ_ONLY FOR
WITH cte AS
(
SELECT
'Candidate' AS [Entity]
, v.ProposalId AS EntityID
, v.LastModifiedOn AS LastModifiedOn
FROM dbo.viwProposals v
WHERE OrgId=@ip_OrgId AND IsHide = 0
UNION
SELECT
'Employee'
, v.EmployeeId
, v.LastModifiedOn
FROM dbo.viwEmployees v
WHERE OrgId=@ip_OrgId AND IsTerminate = 0
)
SELECT
q.Entity
, q.EntityID
, q.LastModifiedOn
, RowNo = ROW_NUMBER() OVER (ORDER BY q.LastModifiedOn DESC)
FROM cte q
ORDER BY LastModifiedOn DESC
OPEN cursor_name
FETCH NEXT FROM cursor_name INTO
@Entity
, @EntityID
, @LastModifiedOn
, @RowNum
WHILE @@FETCH_STATUS = 0 BEGIN
--
FETCH NEXT FROM cursor_name INTO
@Entity
, @EntityID
, @LastModifiedOn
, @RowNum
END
CLOSE cursor_name;
DEALLOCATE cursor_name;
END