1

我正在尝试为矩阵生成一组行,但@xcolcount唯一的 on 循环保持为零,而内部循环执行它需要的操作:

 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
4

2 回答 2

2

我认为您只需要在外循环内将内部计数器重置为零 - 我将其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 语句)不再执行。

于 2010-08-21T21:26:03.527 回答
0

如果您使用从零开始的数字表,这将成为一个基于集合的语句

;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;
于 2010-08-22T07:56:53.087 回答