/*CREATE TABLE m_b (code VARCHAR(10),itemcount INT,type VARCHAR(30),amount MONEY)
insert into m_b values ('B001',1,'Dell',10)
insert into m_b values ('B001',1,'Dell',10)
insert into m_b values ('B001',1,'Apple',10)
insert into m_b values ('B001',2,'Apple',20)
insert into m_b values ('B001',2,'Apple',20)
insert into m_b values ('B114',1,'Apple',30.5)
insert into m_b values ('B114',1,'Apple',10) */
--SELECT * INTO #temp FROM m_c WHERE 1=2
DECLARE cur_test CURSOR
FOR SELECT Jobid,start,end_date,dayrate FROM m_c
go
DECLARE @Jobid INT
DECLARE @start DATE,@end_date DATE
DECLARE @dayrate INT
OPEN cur_test
FETCH cur_test INTO @Jobid,@start,@end_date,@dayrate
--BEGIN
DECLARE @jan INT
SET @jan=0
WHILE (@@sqlstatus != 2)
BEGIN
IF month(@start)=1
BEGIN
SELECT @jan= @jan + datediff(dd,'2013-01-31',@start)
-- testing purpose
INSERT INTO #temp VALUES (@jan,@start,@end_date,56)
END
IF month(@end_date)=1
BEGIN
SELECT @jan= @jan + datediff(dd,'2013-01-31',@end_date)
-- testing purpose
INSERT INTO #temp VALUES (@jan,@start,@end_date,57)
END
FETCH cur_test INTO @Jobid,@start,@end_date,@dayrate
END
SELECT @jan
WAITFOR delay '00:00:10'
CLOSE cur_test
DEALLOCATE CURSOR cur_test
--END
SELECT * FROM #temp
--TRUNCATE TABLE #temp
答案 0 :(得分:0)
我不确定为什么你永远不会停止,但我总是使用以下条件
while @@sqlstatus = 0
所以尝试使用它而不是
WHILE (@@sqlstatus != 2)
答案 1 :(得分:0)
您需要使用以下方法检查获取状态:
WHILE @@FETCH_STATUS = 0
如果永远不会达到退出值,那么将光标循环基于数据值将意味着无限循环。