sql server嵌套游标不返回结果

时间:2013-06-10 13:30:41

标签: tsql cursor nested

我的第一个嵌套游标'uncalcdays'没有返回数据。第一个(非嵌套)游标“代码”正在返回正确的数据,并正确地将此数据传递给变量@codes。

如果我从uncalcdays剪切sql并在单独的查询窗口中运行它返回数据。 如果我从'codes'和'uncalcday'剪切sql,创建适当的varialbes并在单独的查询窗口中运行,它们都返回正确的数据。

这是我的头脑!有什么明显的东西我做错了吗?

计划是我需要识别没有子数据的第一个主记录(即没有子记录的最低id的主记录。)因为我使用了sql server,所以已经有一段时间了。

RGDS 戴夫

SET NOCOUNT ON;

declare @code nvarchar(10)
declare @id numeric
declare @val numeric
declare @total numeric
declare @count numeric
declare @avg numeric
declare @fetch_codes int
declare @fetch_uncalcdays int
declare @fetch_twentyvals int

-- get a list of codes
declare codes cursor for 
    select distinct code from dbo.EOD_Data;

-- get a list of the days that are unprocessed
declare uncalcdays cursor for 
    select d.id
        from dbo.EOD_Data d
             left outer join dbo.EOD_Computed_Stats cs
             on d.id = cs.EOD_Data_Id
       where cs.SMA_20D is null
         and d.CODE = @code
       order by d.id asc; 

-- get the last 20d data for a given stock code
declare twentyvals cursor for 
    select top(20) d.id 
    from dbo.EOD_Data d
    where d.id <= @id
      and d.code = @code
    order by d.id desc;

-- loop through stock codes
open codes
fetch next from codes
    into @code
select @fetch_codes = @@FETCH_STATUS

while @fetch_codes = 0
begin

    open uncalcdays
    fetch next from uncalcdays
      into @id
    select @fetch_uncalcdays = @@FETCH_STATUS

    while @fetch_uncalcdays = 0
    begin

        -- loop through the twenty most recent close prices and calc average
        open twentyvals
        fetch next from twentyvals
            into @val
        select @fetch_twentyvals = @@FETCH_STATUS

        while @fetch_twentyvals = 0
        begin

            ...stuff

        fetch next from uncalcdays
            into @id
        select @fetch_uncalcdays = @@FETCH_STATUS

    end

    close uncalcdays
    deallocate uncaldays

fetch next from codes
    into @code
select @fetch_codes = @@FETCH_STATUS

end

close codes
deallocate codes

END

1 个答案:

答案 0 :(得分:0)

游标很酷,代码和故障排除,它们可能非常有用,但是请尝试使用此代码,它一组股票而不是单个记录:

    --STEP 1 - Get list of unprocessed stocks
    SELECT d.Code
    INTO #unprocessedCodes
    FROM dbo.EOD_Data d
         LEFT OUTER JOIN dbo.EOD_Computed_Stats cs
            ON d.id = cs.EOD_Data_Id
    WHERE cs.SMA_20D IS NULL 

    --STEP 2 - Rate the data base on the date id
    SELECT 
        ROW_NUMBER() OVER ( PARTITION BY Code ORDER BY d.Code, d.id ASC) AS RowNumber,
        d.Price,  --it is good if you have closed price in the table, 
                  --otherwise you would add a join
        d.id AS DateID, 
        d.Code
    INTO #PricingChart
    FROM dbo.EOD_Data d
        INNER JOIN #unprocessedCodes uc
            ON d.Code = uc.Code

    --STEP 3 - Get average of first 20 prices for all unprocessed codes 
    SELECT 
        Code,
        AVG(Price) AS AvgPrice 
    FROM #PricingChart
    WHERE RowNumber <= 20 --you only interested in top 20
    GROUP BY Code

我试图尽可能靠近你的桌子,这并不完美,但它可能比通过光标中的许多记录循环更快。