0

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

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

这让我头疼!有什么明显的我做错了..?

计划是我需要识别第一个没有子数据的主记录(即没有子记录的具有最低 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

结尾

4

1 回答 1

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

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

于 2014-10-13T21:33:42.763 回答