不循环时嵌套 SQL 存储过程
我正在尝试为矩阵生成一组行,但 @xcolcount 只在循环中保持为零,而内部循环则执行所需的操作:
Declare @xColCount int
Declare @yRowCount int
set @xColCount = 0
set @yRowCount = 0
WHILE (@xColCount < @widthCol)
BEGIN
WHILE (@yRowCount < @heightRow)
BEGIN
-- do the insert
INSERT
INTO Scenario.MapCell(Guid, Map, Col, ColAplha, Row)
VALUES (newID(), @mapGuid, @xColCount, 'A', @yRowCount)
SET @yRowCount = @yRowCount + 1
END
SET @xColCount = @xColCount + 1
END
I am trying to generate a set of rows for a matrix but the @xcolcount
only does on loop staying at zero while the inner loop does what it needs to:
Declare @xColCount int
Declare @yRowCount int
set @xColCount = 0
set @yRowCount = 0
WHILE (@xColCount < @widthCol)
BEGIN
WHILE (@yRowCount < @heightRow)
BEGIN
-- do the insert
INSERT
INTO Scenario.MapCell(Guid, Map, Col, ColAplha, Row)
VALUES (newID(), @mapGuid, @xColCount, 'A', @yRowCount)
SET @yRowCount = @yRowCount + 1
END
SET @xColCount = @xColCount + 1
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您只需要在外部循环内将内部计数器重置为零 - 我将
SET @yRowCount = 0
移动到外部循环中:使用您的代码,一旦内部循环完成,
@yRowCount
位于@heightRow
并且从未重置 - 因此内部循环(以及 INSERT 语句)不再执行。I think you only just need to reset your inner counter back to zero inside the outer loop - I moved the
SET @yRowCount = 0
into the outer loop:With your code, once the inner loop completed, the
@yRowCount
was at@heightRow
and never got reset - so the inner loop (and thus the INSERT statement) never executed anymore.如果您使用从零开始的数字表,则这将成为一组基于声明
If you use a numbers table starting at zero, this becomes one set based statement