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

  • SEARCH
  • Home
  • 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 8869639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:31:38+00:00 2026-06-14T17:31:38+00:00

DECLARE @PaymentTime VARCHAR (50) DECLARE @DueDate VARCHAR (50) DECLARE @CustomerID int DECLARE MYCURSOR5 CURSOR

  • 0
DECLARE @PaymentTime VARCHAR (50)

DECLARE @DueDate VARCHAR (50)
DECLARE @CustomerID int

DECLARE MYCURSOR5 CURSOR 
FOR SELECT   distinct  Payment.PaymentTime,Invoice.DueDate, Customer.CustomerID
FROM         Cus_Cred_Terms INNER JOIN
                      Customer ON Cus_Cred_Terms.CustomerID = Customer.CustomerID INNER JOIN
                      Payment ON Customer.CustomerID = Payment.CustomerID INNER JOIN
                      Invoice ON Payment.InvoiceID = Invoice.InvoiceID


open MYCURSOR5 

FETCH MYCURSOR5 INTO @PaymentTime, @DueDate, @CustomerID

WHILE @@FETCH_STATUS = 0
BEGIN 


if(@PaymentTime > @DueDate)
begin
print'payment time:'
PRINT @PaymentTime 
print'due date:'
PRINT @DueDate 

print''
print @CustomerID
print''
update dbo.Cus_Cred_Terms
set dbo.Cus_Cred_Terms.CreditAllowed = 0
FROM Cus_Cred_Terms INNER JOIN
 Customer ON Cus_Cred_Terms.CustomerID = Customer.CustomerID INNER JOIN
 Payment ON Customer.CustomerID = Payment.CustomerID
 where Customer.CustomerID = @CustomerID and dbo.Cus_Cred_Terms.CreditAllowed = 1
end



FETCH MYCURSOR5  INTO @PaymentTime, @DueDate, @CustomerID

END
CLOSE MYCURSOR5

It loops through each field checking if the DueDate is smaller than the actual PaymentDate and if it is, then it sets a field in another table to 0 (1 for credit allowed and 0 for credit not allowed)

So the question is, can I avoid using a cursor to do this? If you need more information please ask.

  • 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-14T17:31:39+00:00Added an answer on June 14, 2026 at 5:31 pm

    Try something like this:

    ;WITH CTE AS 
    (
        SELECT DISTINCT  
            Customer.CustomerID
        FROM         
            Cus_Cred_Terms 
        INNER JOIN
            Customer ON Cus_Cred_Terms.CustomerID = Customer.CustomerID 
        INNER JOIN
            Payment ON Customer.CustomerID = Payment.CustomerID 
        INNER JOIN
            Invoice ON Payment.InvoiceID = Invoice.InvoiceID
        WHERE
            Payment.PaymentTime > Invoice.DueDate
    )
    UPDATE
        dbo.Cus_Cred_Terms
    SET
        dbo.Cus_Cred_Terms.CreditAllowed = 0
    FROM 
        CTE 
    WHERE 
        dbo.Cus_Cred_Terms.CustomerID = CTE.CustomerID
        AND dbo.Cus_Cred_Terms.CreditAllowed = 1
    

    The CTE (Common Table Expression) basically has the SELECT that your cursor had – except it also check the constraint Payment.PaymentTime > Invoice.DueDate. Only those customer ID’s that match this criteria are selected.

    From those CustomerID values, the table dbo.Cus_Cred_Terms is updated if the CreditAllowed column for this CustomerID in that table is equal to 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

declare @i int declare @skool float declare schoolCursor cursor for select distinct choice from
DECLARE @TEMP_TBL TABLE (COL1 INT, COL2 VARCHAR(100) SELECT ID, NAME INTO @TEMP_TBL FROM TB1
DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
DECLARE @Symb varchar SET @Symb = 'Lara' SELECT Names FROM Cricket where Names like
declare @top int set @top = 5 select top @top * from tablename Is
declare v_date date; CURSOR abc is select a_date from abc where part_id ='E00000001'; begin
DECLARE @result varchar(MAX) SELECT NAME FROM Table WHERE ID = @Id I need to
Declare @ID nvarchar(14) = '12345678912345' DECLARE @Key int = (SELECT key FROM table WHERE
declare @customerID int set @customerID=1 ;with cteDates as ( select dateadd(hh, datediff(hh, 0, getdate()),
DECLARE cursor curs is select * from starting; appleId number; bananaId number; BEGIN for

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.