本文介绍了如何在SQL中使用while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我一直在编写一个查询来获取序列nu,mber使用while循环。我想将该值乘以.. DECLARE @ intFlag INT SET @ intFlag = 1 WHILE ( @ intFlag < = 5) BEGIN PRINT @ intFlag SET @ intFlag = @ intFlag + 1 END GO i想要出1 * 2 * 3 * 4 * 5 = 120 我尝试了什么: DECLARE @intFlag INT SET @intFlag = 1 WHILE(@intFlag< = 5) BEGIN PRINT @intFlag SET @intFlag = @intFlag + 1 END GO 解决方案 你需要另一个变量来保存你想要的结果,例如 DECLARE @ result INT DECLARE @ intFlag INT - 这是你的LOOP计数器 SET @intFlag = 1 SET @result = 1 WHILE ( @ intFlag < = 5) BEGIN PRINT @ intFlag SET @result = @ result * @ intFlag SET @ intFlag = @ intFlag + 1 END PRINT @ result GO Quote:如何在SQL中使用while循环?您应该通过阅读文档并遵循tutos来正确学习SQL。 Sql Server中的WHILE循环| SqlHints.com [ ^ ] I have been writing one query to get serial nu,mber using while loop.I want to multiply the value also..DECLARE @intFlag INTSET @intFlag = 1WHILE (@intFlag <=5)BEGINPRINT @intFlagSET @intFlag = @intFlag + 1ENDGOi want out put 1*2*3*4*5=120What I have tried:DECLARE @intFlag INTSET @intFlag = 1WHILE (@intFlag <=5)BEGINPRINT @intFlagSET @intFlag = @intFlag + 1ENDGO 解决方案 You need another variable to hold the result you want e.g.DECLARE @result INTDECLARE @intFlag INT -- This is your LOOP counterSET @intFlag = 1SET @result = 1WHILE (@intFlag <=5)BEGIN PRINT @intFlag SET @result = @result * @intFlag SET @intFlag = @intFlag + 1ENDPRINT @resultGOQuote:How to use while loop in SQL?You should learn SQL properly by reading documentation and follow tutos.WHILE loop in Sql Server | SqlHints.com[^] 这篇关于如何在SQL中使用while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-14 09:46