不循环时嵌套 SQL 存储过程

发布于 2024-09-15 22:18:58 字数 522 浏览 9 评论 0原文

我正在尝试为矩阵生成一组行,但 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏雨凉 2024-09-22 22:18:58

我认为您只需要在外部循环内将内部计数器重置为零 - 我将 SET @yRowCount = 0 移动到外部循环中:

DECLARE @xColCount int
DECLARE @yRowCount int

SET @xColCount = 0

WHILE (@xColCount < @widthCol) 
BEGIN
    SET @yRowCount = 0

    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

使用您的代码,一旦内部循环完成, @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:

DECLARE @xColCount int
DECLARE @yRowCount int

SET @xColCount = 0

WHILE (@xColCount < @widthCol) 
BEGIN
    SET @yRowCount = 0

    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

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.

月野兔 2024-09-22 22:18:58

如果您使用从零开始的数字表,则这将成为一组基于声明

;WITH
  cX AS (SELECT Num AS xColCount FROM NumTable WHERE Num <= @widthCol),
  cY AS (SELECT Num AS yRow  Count FROM NumTable WHERE Num <= @heightRow)
INSERT Scenario.MapCell(Guid, Map, Col, ColAplha, Row)
SELECT
    @mapGuid, xColCount, 'A', yRowCount
FROM
    cX CROSS JOIN cY;

If you use a numbers table starting at zero, this becomes one set based statement

;WITH
  cX AS (SELECT Num AS xColCount FROM NumTable WHERE Num <= @widthCol),
  cY AS (SELECT Num AS yRow  Count FROM NumTable WHERE Num <= @heightRow)
INSERT Scenario.MapCell(Guid, Map, Col, ColAplha, Row)
SELECT
    @mapGuid, xColCount, 'A', yRowCount
FROM
    cX CROSS JOIN cY;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文