Sql server SQL Server游标跳过While循环内的IF条件(游标范围)
我有一个存储过程,我在其中执行下面的游标代码, 这是我第一次使用游标Sql server SQL Server游标跳过While循环内的IF条件(游标范围),sql-server,stored-procedures,Sql Server,Stored Procedures,我有一个存储过程,我在其中执行下面的游标代码, 这是我第一次使用游标 declare @Gold_Min int, @Gold_Max int, @Silver_Min int, @Silver_Max int, @Bronze_Min int, @Bronze_Max int, @gold_c int, @silver_c int, @bronze_c int; set @gold_c = 0; set @silver_c = 0; set @bronze_c = 0; set @Gold_M
declare @Gold_Min int, @Gold_Max int, @Silver_Min int, @Silver_Max int, @Bronze_Min int, @Bronze_Max int, @gold_c int, @silver_c int, @bronze_c int;
set @gold_c = 0;
set @silver_c = 0;
set @bronze_c = 0;
set @Gold_Max = 100;
set @Gold_Min = 75;
set @Silver_Max = 74;
set @Silver_Min = 50;
set @Bronze_Max = 49;
set @Bronze_Min = 25;
DECLARE @ParticipantId int, @Transaction_date date, @Transaction_Time float, @Transaction_completed int, @gold int, @silver int, @bronze int;
DECLARE @MyCursor CURSOR;
BEGIN
SET @MyCursor = CURSOR DYNAMIC FOR
select TransactionsCompleted, Gold, Silver, Bronze from #Gamification_Transaction_Per_Date
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @Transaction_completed , @gold , @silver , @bronze;
WHILE @@FETCH_STATUS = 0
BEGIN
/*
YOUR ALGORITHM GOES BELOW
*/
while(@Transaction_completed > 0)
begin
if(@Transaction_completed >= @Gold_Min and ( (@Transaction_completed <= @Gold_Max) or (@Transaction_completed > @Gold_Max) ))
BEGIN
if(@Transaction_completed between @Gold_Min and @Gold_Max)
BEGIN
set @Transaction_completed = @Gold_Max - @Transaction_completed;
set @gold_c = @gold_c + 1;
break;
END
ELSE
BEGIN
set @Transaction_completed = @Transaction_completed - @Gold_Max;
set @gold_c = @gold_c + 1;
break;
END
END
if(@Transaction_completed >= @Silver_Min and ( (@Transaction_completed <= @Silver_Max) or (@Transaction_completed > @Silver_Max) ))
BEGIN
if(@Transaction_completed between @Silver_Min and @Silver_Max)
BEGIN
set @Transaction_completed = @Silver_Max - @Transaction_completed;
set @silver_c = @silver_c + 1;
break;
END
ELSE
BEGIN
set @Transaction_completed = @Transaction_completed - @Silver_Max;
set @silver_c = @silver_c + 1;
break;
END
END
if(@Transaction_completed >= @Bronze_Min and ( (@Transaction_completed <= @Bronze_Max) or (@Transaction_completed > @Bronze_Max) ))
BEGIN
if(@Transaction_completed between @Bronze_Min and @Bronze_Max)
BEGIN
set @Transaction_completed = @Bronze_Max - @Transaction_completed;
set @bronze_c = @bronze_c + 1;
break;
END
ELSE
BEGIN
set @Transaction_completed = @Transaction_completed - @Bronze_Max;
set @bronze_c = @bronze_c + 1;
break;
END
END
end
Update #Gamification_Transaction_Per_Date SET Gold = @gold_c WHERE CURRENT OF @MyCursor
Update #Gamification_Transaction_Per_Date SET Silver = @silver_c WHERE CURRENT OF @MyCursor
Update #Gamification_Transaction_Per_Date SET Bronze = @bronze_c WHERE CURRENT OF @MyCursor
/*
YOUR ALGORITHM GOES ABOVE
*/
FETCH NEXT FROM @MyCursor
INTO @Transaction_completed , @gold , @silver , @bronze;
END;
CLOSE @MyCursor ;
DEALLOCATE @MyCursor;
声明@Gold\u Min int、@Gold\u Max int、@Silver\u Min int、@Silver\u Max int、@brown\u Min int、@brown\u Max int、@Gold\u c int、@Silver\u c int、@browner\u c int;
设置@gold_c=0;
设置@silver_c=0;
设置为青铜_c=0;
设置@Gold_Max=100;
设置@Gold_Min=75;
设置@Silver_Max=74;
设置@Silver_Min=50;
设置为青铜_Max=49;
设置为青铜_最小值=25;
声明@ParticipantId int、@Transaction\u date date、@Transaction\u Time float、@Transaction\u completed int、@gold int、@silver int、@brown int;
声明@MyCursor游标;
开始
将@MyCursor=光标设置为动态
从#游戏化(交易)每个(日期)中选择交易已完成、黄金、白银、青铜
打开@MyCursor
从@MyCursor获取下一个
进入@Transaction_完成、@gold、@silver、@brown;
而@@FETCH\u STATUS=0
开始
/*
你的算法如下
*/
而(@Transaction_completed>0)
开始
如果(@Transaction_completed>=@Gold_Min和(@Transaction_completed@Gold_Max)))
开始
如果(@Transaction_在@Gold_Min和@Gold_Max之间完成)
开始
设置@Transaction\u completed=@Gold\u Max-@Transaction\u completed;
设置@gold_c=@gold_c+1;
打破
结束
其他的
开始
设置@Transaction\u completed=@Transaction\u completed-@Gold\u Max;
设置@gold_c=@gold_c+1;
打破
结束
结束
如果(@Transaction_completed>=@Silver_Min和(@Transaction_completed@Silver_Max)))
开始
如果(@Transaction_在@Silver_Min和@Silver_Max之间完成)
开始
设置@Transaction\u completed=@Silver\u Max-@Transaction\u completed;
设置@silver\u c=@silver\u c+1;
打破
结束
其他的
开始
设置@Transaction\u completed=@Transaction\u completed-@Silver\u Max;
设置@silver\u c=@silver\u c+1;
打破
结束
结束
如果(@Transaction_completed>=@brown_Min和(@Transaction_completed@brown_Max)))
开始
如果(@brown_Min和@brown_Max之间的@Transaction_已完成)
开始
设置@Transaction\u completed=@brown\u Max-@Transaction\u completed;
设置@brown_c=@brown_c+1;
打破
结束
其他的
开始
设置@Transaction\u completed=@Transaction\u completed-@brown\u Max;
设置@brown_c=@brown_c+1;
打破
结束
结束
结束
更新#游戏化(事务)每(日期集Gold=@Gold\u c其中当前为@MyCursor
更新#游戏化(事务)每(日期集)Silver=@Silver\u c其中当前为@MyCursor
更新#游戏化(事务)每(日期集)青铜=@brown(其中当前为@MyCursor)
/*
你的算法在上面
*/
从@MyCursor获取下一个
进入@Transaction_完成、@gold、@silver、@brown;
结束;
关闭@MyCursor;
释放@MyCursor;
结束
我的动机是更新某个名为“游戏化交易日”的表中的金币、银币和铜币的价值
表结构定义为:
- 主要的查询是While循环内的If循环没有执行,而当我在循环外打印“HII i am here”时,它工作完全正常
- 预期产出为:
- 任何线索都将不胜感激
shohil Sethia您的预期产量是多少?在大多数案例中,您可能不需要循环/光标@KannanKandasamy是的,我可能不需要光标,预期输出是我随新数量的金、银或铜徽章所包含的表格的更新。Hi@KannanKandasamy,我已使用预期输出更新了问题。您可能需要基于logicHi@KannanKandasamy使用MERGE with Insert/Update/delete,我尊重您的知识,您能否就问题的上下文提出解决方案?谢谢,你的预期产量是多少?在大多数案例中,您可能不需要循环/光标@KannanKandasamy是的,我可能不需要光标,预期输出是我随新数量的金、银或铜徽章所包含的表格的更新。Hi@KannanKandasamy,我已使用预期输出更新了问题。您可能需要基于logicHi@KannanKandasamy使用MERGE with Insert/Update/delete,我尊重您的知识,您能否就问题的上下文提出解决方案?非常感谢。