Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:13:46+00:00 2026-06-10T00:13:46+00:00

How can I write the following sp without the cursor?. More over its not

  • 0

How can I write the following sp without the cursor?. More over its not giving me the desired output. I didn’t write this, I am trying to interpret what is wrong with this.

ALTER PROCEDURE [dbo].[AccreditationExpiryCheck]
AS
BEGIN
    SET NOCOUNT ON;

    declare @taskTypeId int = 19 -- Accreditations, automated
    declare @firstActionTypeId int = 23 -- Accreditation expiring
    declare @nextActionTypeId int = 3 -- Call company

    declare @companyId int
    declare @accreditationId int
    declare @comment nvarchar(max) = N' accreditation for this company has expired.'

    -- find all companies and accreditations expiring
    declare companies cursor local forward_only read_only for 
        select c.Company_Id, a.Accred_ID
        from COMPANY c
            inner join MEMBERSHIP m on c.Company_ID = m.Company_ID
            inner join ACCREDITATION a on c.Company_ID = a.Company_ID
        where
            -- Accreditation expired yesterday
            cast(a.Accred_ExpDate as DATE) = cast(DATEADD(DAY, -1, GETDATE()) as DATE)
            and m.IsMember_Ind = 1
            and (c.HQ_ID IS NULL OR c.HQ_ID = c.Company_ID)  -- FB4640: this isn't a 'team' co (with an HQ)
            -- and there is no action of this type created within 1 day
            -- of the expiry date
            and not exists (
                select * from TaskAction ta where
                    ta.FirstActionTypeId = @firstActionTypeId and
                    ta.TaskTypeId = @taskTypeId and
                    ta.TaskCreatedOn BETWEEN a.Accred_ExpDate AND DATEADD(DAY, 1, a.Accred_ExpDate) and
                    ta.EntityId = c.Company_ID and 
                    ta.EntityTypeId = 1 )

    open companies

    fetch next from companies into @companyId, @accreditationId

    declare @title nvarchar(max) = 
        (select AccredType_Name from ACCREDITATION_TYPE at 
        inner join ACCREDITATION a on at.AccredType_ID = a.AccredType_ID
        where a.Accred_ID = @accreditationId)

    declare @comment2 nvarchar(max) = isnull(@title, '') + ' accreditation for this company has expired.'
    while @@FETCH_STATUS = 0
    begin
        exec CreateSystemTask 
            @taskTypeId, 
            @firstActionTypeId,
            @nextActionTypeId,
            @companyid,
            @comment2,
            @title

        fetch next from companies into @companyId,@accreditationId
    end

    close companies
    deallocate companies
END

The following select statement from the above sp gives me the correct dataset, but the cursor which loops through gives me a different output.

select c.Company_Id, a.Accred_ID
        from COMPANY c
            inner join MEMBERSHIP m on c.Company_ID = m.Company_ID
            inner join ACCREDITATION a on c.Company_ID = a.Company_ID
        where
            -- Accreditation expired yesterday
            cast(a.Accred_ExpDate as DATE) = cast(DATEADD(DAY, -1, GETDATE()) as DATE)
            and m.IsMember_Ind = 1
            and (c.HQ_ID IS NULL OR c.HQ_ID = c.Company_ID)  -- FB4640: this isn't a 'team' co (with an HQ)
            -- and there is no action of this type created within 1 day
            -- of the expiry date
            and not exists (
                select * from TaskAction ta where
                    ta.FirstActionTypeId = @firstActionTypeId and
                    ta.TaskTypeId = @taskTypeId and
                    ta.TaskCreatedOn BETWEEN a.Accred_ExpDate AND DATEADD(DAY, 1, a.Accred_ExpDate) and
                    ta.EntityId = c.Company_ID and 
                    ta.EntityTypeId = 1 )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:13:48+00:00Added an answer on June 10, 2026 at 12:13 am
    ALTER PROCEDURE [dbo].[AccreditationExpiryCheck] 
    AS 
    BEGIN 
        SET NOCOUNT ON; 
    
        declare @taskTypeId int = 19 -- Accreditations, automated 
        declare @firstActionTypeId int = 23 -- Accreditation expiring 
        declare @nextActionTypeId int = 3 -- Call company 
    
        declare @companyId int 
        declare @accreditationId int 
        declare @comment nvarchar(max) = N' accreditation for this company has expired.'
    
        -- find all companies and accreditations expiring 
        select ROW_NUMBER() OVER(ORDER BY c.Company_Id, a.Accred_ID) as [RecordId], c.Company_Id as [Company_Id], a.Accred_ID as [Accred_ID]
        into #COMPANIES
            from COMPANY c 
                inner join MEMBERSHIP m on c.Company_ID = m.Company_ID 
                inner join ACCREDITATION a on c.Company_ID = a.Company_ID 
            where 
                -- Accreditation expired yesterday 
                cast(a.Accred_ExpDate as DATE) = cast(DATEADD(DAY, -1, GETDATE()) as DATE) 
                and m.IsMember_Ind = 1 
                and (c.HQ_ID IS NULL OR c.HQ_ID = c.Company_ID)  -- FB4640: this isn't a 'team' co (with an HQ) 
                -- and there is no action of this type created within 1 day 
                -- of the expiry date 
                and not exists ( 
                    select * from TaskAction ta where 
                        ta.FirstActionTypeId = @firstActionTypeId and 
                        ta.TaskTypeId = @taskTypeId and 
                        ta.TaskCreatedOn BETWEEN a.Accred_ExpDate AND DATEADD(DAY, 1, a.Accred_ExpDate) and 
                        ta.EntityId = c.Company_ID and  
                        ta.EntityTypeId = 1 )
    
        declare @recordId int = 0;
        declare @title nvarchar(max);
        declare @comment2 nvarchar(max);
    
        while(1=1)
            begin
                select top 1 @recordId = [RecordId]
                            ,@companyId = [CompanyId]
                            ,@accreditationId = [Accred_ID]
                from #COMPANIES
                where [RecordId] > @recordId
    
                if @@ROWCOUNT = 0 break;
    
                set @title =  
                    (select AccredType_Name from ACCREDITATION_TYPE at  
                    inner join ACCREDITATION a on at.AccredType_ID = a.AccredType_ID 
                    where a.Accred_ID = @accreditationId) 
    
                set @comment2 = isnull(@title, '') + ' accreditation for this company has expired.' 
    
                    exec CreateSystemTask  
                        @taskTypeId,  
                        @firstActionTypeId, 
                        @nextActionTypeId, 
                        @companyid, 
                        @comment2, 
                        @title        
            end 
    
        drop table #COMPANIES 
    END 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write the following jquery in prototypejs? <a class=button view style=cursor:pointer id=addComment></a>
How can I write the following code more concisely? scores = [] for f
Possible Duplicate: Stored procedure without cursors How can I write the following sp without
Is there a way I can write the following without typing out the whole
Can someone please help me write the following, without using dot notation: self.bounds.size.width I
How can I write a Regex to represent certain keywords without any letter/number following
Is there a way i can write the following piece of code to be
Someone recently taught me a useful thing: in css you can write the following:
How can you write the following statement in the given languages? a(0) = 1
How can you write the following in MYSQL? SELECT AVG(col1) FROM table WHERE DISTINCT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.