通过从实际表中过滤数据,我将其插入临时表,然后一些批量值来自它,然后我再次将其插入到另一个临时表中以进行检查循环并将实际表中的某些值更新到数据库中。那么如何循环检索列值呢?
直到我到达:
CREATE TABLE #result
(
PackagePeriod varchar(20),
UserId INT,
OldExpiryDate DATE,
AmountToPay FLOAT,
PyingAmount FLOAT,
Balance FLOAT,
LastPaidDate DATE,
Company_Id INT
)
create table #temp
(
PackagePeriod varchar(20),
UserId INT,
OldExpiryDate DATE,
AmountToPay FLOAT,
PyingAmount FLOAT,
Balance FLOAT,
LastPaidDate DATE,
Company_Id INT
)
SET @cmd ='.... where OldExpiryDate<=Replace(CONVERT(VARCHAR(20), (GetDate()-1), 106),' ','-')
select @count=(select COUNT(PackagePeriod) from #temp)
if(PackagePeriod='Monthly')
begin
update ...
end
else if(PackagePeriod='Quarterly')
begin
update ..
答案 0 :(得分:0)
你的问题不明确。我认为您可以使用Cursor或While来解决您的问题。
CREATE TABLE #result
(
PackagePeriod varchar(20),
UserId INT,
OldExpiryDate DATE,
AmountToPay FLOAT,
PyingAmount FLOAT,
Balance FLOAT,
LastPaidDate DATE,
Company_Id INT
)
declare @PackagePeriod varchar(20)
declare @UserId int
declare @OldExpiryDate DATE
declare @AmountToPay FLOAT
declare @PyingAmount FLOAT
declare @Balance FLOAT
declare @LastPaidDate DATE
declare @Company_Id INT
declare result_cursor cursor for
SELECT PackagePeriod, UserId, OldExpiryDate, AmountToPay, PyingAmount, Balance, LastPaidDate, Company_Id from #result
OPEN result_cursor
FETCH NEXT FROM result_cursor INTO @PackagePeriod, @UserId, @OldExpiryDate, @AmountToPay, @PyingAmount, @Balance, @LastPaidDate, @Company_Id
WHILE @@FETCH_STATUS = 0
BEGIN
if(@PackagePeriod='Monthly')
begin
-- update your table
end
else if(@PackagePeriod='Quarterly')
begin
--update table
end
FETCH NEXT FROM result_cursor INTO @PackagePeriod, @UserId, @OldExpiryDate, @AmountToPay, @PyingAmount, @Balance, @LastPaidDate, @Company_Id
END
CLOSE result_cursor
DEALLOCATE result_cursor