我的第一个嵌套游标“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
结尾