DECLARE @t datetimeDECLARE getCursor CURSOR FOR SELECT ord_date FROM salesOPEN getCursorFETCH next FROM getCursor INTO @tWHILE @@FETCH_STATUS=0
        
    PRINT  @t
    
CLOSE getCursor
DEALLOCATE getCursor
---结束---
其实是帮助文档上的例子,我把红色部分改为现在这样后 出现死循环,不知是为什么?
原来是 FETCH next FROM getCursor
大家帮诊断一下。谢谢。

解决方案 »

  1.   

    DECLARE @t datetime DECLARE getCursor CURSOR FOR SELECT ord_date FROM sales OPEN getCursor FETCH next FROM getCursor INTO @t WHILE @@FETCH_STATUS=0         
        PRINT  @t 
    FETCH next FROM getCursor INTO @t     --少了这句
    CLOSE getCursor 
    DEALLOCATE getCursor 
      

  2.   

    在循环中再加一个 fetch next 语句.
      

  3.   

    这样不行,要与上面一句组成一个块
    DECLARE @t datetime DECLARE getCursor CURSOR FOR SELECT ord_date FROM sales OPEN getCursor FETCH next FROM getCursor INTO @t WHILE @@FETCH_STATUS=0  
    begin       
        PRINT  @t 
    FETCH next FROM getCursor INTO @t     --少了这句
    end
    CLOSE getCursor 
    DEALLOCATE getCursor 
      

  4.   

    if object_id('tb') is not null
      drop table tb
    go
    create table tb([c] varchar(10),[intime] datetime,[outtime] datetime)
    insert tb select '车A','2009-10-2','2009-10-4'
    insert tb select '车B','2009-10-4','2009-10-10'
    insert tb select '车C','2009-10-2','2009-10-3'
    insert tb select '车D','2009-10-5','2009-10-8'
    insert tb select '车E','2009-10-6',NULL
    insert tb select '车F','2009-10-10','2009-10-12'
    go
    --select * from tbDECLARE @t datetime DECLARE getCursor CURSOR FOR SELECT intime FROM tb OPEN getCursor FETCH next FROM getCursor INTO @t WHILE @@FETCH_STATUS=0    
    begin     
        PRINT  @t 
    FETCH next FROM getCursor INTO @t  
    end   --少了这句
    CLOSE getCursor 
    DEALLOCATE getCursor /*
    10  2 2009 12:00AM
    10  4 2009 12:00AM
    10  2 2009 12:00AM
    10  5 2009 12:00AM
    10  6 2009 12:00AM
    10 10 2009 12:00AM
    */还少了一个begin end
      

  5.   

    DECLARE @t datetime DECLARE getCursor CURSOR FOR SELECT ord_date FROM sales OPEN getCursor FETCH next FROM getCursor INTO @t WHILE @@FETCH_STATUS=0  
    begin       
        PRINT  @t 
    FETCH next FROM getCursor INTO @t     --少了这句
    end
    CLOSE getCursor 
    DEALLOCATE getCursor 
    正确。
    谢谢各位的参与。
    原来while必须加上begin end