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
它遍历每个字段,检查 DueDate 是否小于实际 PaymentDate,如果小于,则将另一个表中的字段设置为 0(1 表示允许信用,0 表示不允许信用)
所以问题是,我可以避免使用光标来执行此操作吗?如果您需要更多信息,请询问。