0

我想知道这是否可以在 SQL Server 中做到:

我的表如下所示:

 CREATE TABLE MEMBERSHIP
(
    Memebership_ID INT IDENTITY(1,1)PRIMARY KEY NOT NULL,   
    MemberShip_Expiry_Date Datetime,
    Member_Account_Balance Money,
    Member_Blacklisted bit ,--(0 for no, 1 for yes)
    Customer_ID INT not null,
    Last_Payment datetime
)

我想知道如果从必须插入的日期起超过 6 个月,是否可以使用带有或不带有光标的存储过程来更改Member_Blacklisted列,例如Last_Payment

DECLARE @MemberID int,@Date datetime

到目前为止我的尝试:

DECLARE @MemberID int,@Date datetime

-- Declaring the Cursor. 
DECLARE C_Expired_Penalty_BlackList CURSOR
FOR
(
 SELECT 
 FROM MEMBERSHIP
)

-- Open the Cursor declared.
OPEN C_Expired_Penalty_BlackList
FETCH NEXT FROM C_Expired_Penalty_BlackList INTO @MemberID,@Date
WHILE @@FETCH_STATUS = 0
BEGIN
 IF @Date > DATEPART(MONTH,getdate()+6)
 BEGIN
  update MEMBERSHIP
  set Member_Blacklisted = 1
  Where Memebership_ID = @MemberID 
 END
  FETCH NEXT FROM C_Expired_Penalty_BlackList INTO @MemberID,@Date
END

CLOSE C_Expired_Penalty_BlackList
DEALLOCATE C_Expired_Penalty_BlackList
4

1 回答 1

4

尝试这个:

UPDATE MEMBERSHIP 
SET Member_Blacklisted = 1 
WHERE Last_Payment < DATEADD(Month, -6, GETDATE())
于 2013-05-08T18:54:00.547 回答