我試圖將從DB中檢索到的數據存儲到一個變量中並將其返回給java代碼。SQL Server過程中使用遊標時的變量範圍
它返回NULL值。這是因爲遊標中變量的範圍。任何人都可以提出解決方案嗎?
這裏是我的方法:
ALTER PROCEDURE [dbo].[rml_ups_profilerscheduler] @RuleIds varchar(200) output
-- Add the parameters for the stored procedure here
--<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
--<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @rulescheduleid varchar(50), @scheduletype varchar(50),@finalop varchar(50)
DECLARE cursorName CURSOR GLOBAL
FOR
select distinct rulescheduleid,scheduletype
from rml_ups_ruleschedulemaster
OPEN cursorName -- open the cursor
FETCH NEXT FROM cursorName
INTO @rulescheduleid, @scheduletype
set @finalop=''
if(lower(@scheduletype) ='daily')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE()))
and a.rulescheduleid=1)))
-- PRINT @rulescheduleid + ' ' + @scheduletype -- print the name
if(lower(@scheduletype) ='weekly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofweek =(SELECT DATEPART(dw, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='monthly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='yearly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE())) and
a.schedulemonth=(SELECT DATEPART(mm, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='on at time')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE()))
and schedulemonth=(SELECT DATEPART(mm, GETDATE())) and
a.scheduleyear=(SELECT DATEPART(yy, GETDATE()))
and [email protected])))
--PRINT @rulescheduleid + ' ' + @scheduletype -- print the name
--PRINT @@FETCH_STATUS
WHILE @@FETCH_STATUS = 0
BEGIN
---PRINT @@FETCH_STATUS
FETCH NEXT FROM cursorName
INTO @rulescheduleid, @scheduletype
if(lower(@scheduletype) ='daily')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE()))
and [email protected])))
-- PRINT @rulescheduleid + ' ' + @scheduletype -- print the name
if(lower(@scheduletype) ='weekly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofweek =(SELECT DATEPART(dw, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='monthly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='yearly')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE())) and
a.schedulemonth=(SELECT DATEPART(mm, GETDATE()))
and [email protected])))
if(lower(@scheduletype) ='on at time')
set @[email protected]+','+(SELECT CONVERT(varchar(150),
(select ruleid from rml_ups_ruleschedulemaster a,rml_ups_rulescheduletimemapping b
where a.rulescheduleid=b.rulescheduleid and b.schedulestarthour <= (SELECT DATEPART(hh, GETDATE()))
and scheduleendhour >=(SELECT DATEPART(hh, GETDATE())) and a.scheduledayofmonth =(SELECT DATEPART(dd, GETDATE()))
and schedulemonth=(SELECT DATEPART(mm, GETDATE())) and
a.scheduleyear=(SELECT DATEPART(yy, GETDATE()))
and [email protected])))
PRINT @finalop
set @[email protected]
PRINT @RuleIds
-- print the name
END
--set @[email protected]
--PRINT @RuleIds
CLOSE cursorName
-- close the cursor
--PRINT @RuleIds
DEALLOCATE cursorName
-- Deallocate the cursor
END
關於是否需要光標,總是值得一提的是+1。 – 2012-03-26 07:37:04
我已經修改我的代碼過程運行沒有任何錯誤,但在循環結束後,循環結束時(從基於conditoins的數據庫中回收)賦值爲變量在循環完成後變爲空,這裏是我的修改代碼 – ameenulla 2012-03-27 15:25:29