0

我想在 SQL Server Management Studio 中创建一个带有 with 子句的存储过程。

我想在 with 子句中使用 SQL 游标。

我的存储过程是:

ALTER PROCEDURE [dbo].[spGetAllCandidatesEmployeesByOrgId]            
   @ip_OrgId as int,             
   @Active_XML as Bit 

WITH q AS
(
  SELECT 
     'Candidate' AS [Entity]        
     ,viwProposals.ProposalId AS EntityID  
     ,viwProposals.LastModifiedOn AS LastModifiedOn 
  FROM 
     viwProposals 
  WHERE 
     OrgId=@ip_OrgId  AND IsHide = 0

  UNION

  SELECT 
    'Employee' AS [Entity]          
    ,viwEmployees.EmployeeId AS EntityID
    ,viwEmployees.LastModifiedOn AS LastModifiedOn
  FROM 
     viwEmployees 
  WHERE 
     OrgId=@ip_OrgId AND IsTerminate = 0
  )
  SELECT  
      q.*, 
      ROW_NUMBER() OVER (ORDER BY LastModifiedOn DESC) AS RowNo
  FROM    
      q              
  ORDER BY 
      LastModifiedOn DESC               
END

我想在q如何做到这一点上运行光标。

4

1 回答 1

1

试试这个——

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
于 2013-05-07T11:55:24.083 回答