我正在尝试为矩阵生成一组行,但@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
答案 0 :(得分: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语句)从未执行过。
答案 1 :(得分:0)
如果您使用从零开始的numbers table,这将成为一个基于集合的语句
;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;