10

在 TSQL 中,我想将以下代码从必须使用硬编码的 dhome 更改为使用循环进行优化。我尝试添加循环失败的尝试也包括在内。

Declare  @dhome Tinyint, @bp smallint, @lr smallint, @q smallint

    // Set @dhome = 1
  While(@dhome <= 3) // My attempt to add a loop

  SELECT @lr = MAX(NQdDate), @q = NQd
  FROM NQdHistory
  WHERE dhomeId = @dhome 
  GROUP BY NQdDate, NQd

  SELECT @bd = COUNT(*)
  FROM bdhome
  WHERE NQdDate= @lr AND dhomeID= @dhome 

  DELETE FROM ND1 WITH(XLOCK)
  WHERE dhomeID= @dhome  AND NQdDate= @lr

  UPDATE NQdHistory
  SET Nbd = @q - @@RowCount - @bp, NBd = @bp
  WHERE NQdDate= @lr AND dhomeID= @dhome 

  Set @dhome = @dhome +1 //My attempt to end a loop
4

3 回答 3

17

你在正确的轨道上。你错过了开始和结束。另外,一定要给出@dhome一个值。看起来你已经开始并在你的第三行注释掉了:

Declare  @dhome Tinyint, @bp smallint, @lr smallint, @q smallint

    // Set @dhome = 1
While(@dhome <= 3) // My attempt to add a loop
begin
  SELECT @lr = MAX(NQdDate), @q = NQd
  FROM NQdHistory
  WHERE dhomeId = @dhome 
  GROUP BY NQdDate, NQd

  SELECT @bd = COUNT(*)
  FROM bdhome
  WHERE NQdDate= @lr AND dhomeID= @dhome 

  DELETE FROM ND1 WITH(XLOCK)
  WHERE dhomeID= @dhome  AND NQdDate= @lr

  UPDATE NQdHistory
  SET Nbd = @q - @@RowCount - @bp, NBd = @bp
  WHERE NQdDate= @lr AND dhomeID= @dhome 

  Set @dhome = @dhome +1 //My attempt to end a loop
end  

如果您熟悉 C/C#/C++,可以将 T-SQL 的 Begin 和 End 想象成花括号{},如果您更熟悉 VBThenEnd If. 或者更像帕斯卡BeginEnd. 你明白了:)

于 2013-04-02T17:59:29.667 回答
2

在你的时间里错过了开始和结束。

同时 (Transact-SQL)

于 2013-04-02T17:59:29.863 回答
2

示例 1

DECLARE @I INT,@COUNTVAR INT
SET @I = 1
DECLARE @Parent_Child TABLE(ID INT IDENTITY(1,1),ParentPositionID INT NULL,ChildPositionId Int)

INSERT INTO @Parent_Child(ParentPositionID,ChildPositionId) 
SELECT DISTINCT PARENT_POSITION_ID,CHILD_POSITION_ID from tblPOSITION_HIERARCHY 
--WHERE CHILD_POSITION_ID IN (--YOUR CONDITION IF ANY)
SELECT @COUNTVAR =COUNT(*) FROM @PTS_Parent_Child
DECLARE @int_SUPE_POSITION_ID INT, @int_CHILD_POSITION_ID INT
 --loop through records here 
WHILE @I <= @COUNTVAR
BEGIN
SELECT @int_SUPE_POSITION_ID=ParentPositionID,@int_CHILD_POSITION_ID=ChildPositionId  FROM @Parent_Child WHERE ID=@I   
--Whatever you want to do with records
SET @I=@I+1
END

示例 2

如果您可以使用临时表,这只是另一种方法。我已经亲自测试过了,它不会导致任何异常(即使临时表没有任何数据。)

CREATE TABLE #TempTable
    (
      ROWID int identity(1,1) primary key,
      HIERARCHY_ID_TO_UPDATE int,
    )
    --INSERT DATA INTO TEMP TABLE USING INSERT INTO CLAUSE OR FOR EAXMPLE BELOW
    --INSERT INTO #TempTable VALUES(1)
    --INSERT INTO #TempTable VALUES(2)
    --INSERT INTO #TempTable VALUES(4)
    --INSERT INTO #TempTable VALUES(6)
    --INSERT INTO ##TempTable VALUES(8)


    DECLARE @MAXID INT
    SET @COUNTER =1
    SELECT @MAXID=COUNT(*) FROM #TempTable
    --PRINT @MAXID
    WHILE (@MAXID > 0)
    BEGIN
         --DO THE PROCESSING HERE 
         SELECT @HIERARCHY_ID_TO_UPDATE =PT.HIERARCHY_ID_TO_UPDATE FROM      #TempTable  PT WHERE ROWID=@COUNTER
         --PRINT '@MAXID VALUE '
         --PRINT  @MAXID
        SET @MAXID=@MAXID-1
        SET @COUNTER =@COUNTER + 1
    End


    If(OBJECT_ID('tempdb..#TempTable') IS NOT NULL)
    BEGIN
        DROP TABLE #TempTable
    END
于 2015-12-01T18:22:39.233 回答