2012-03-26 17 views
0

DBから取得したデータを1つの変数に格納して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

このビット:

WHILE @@FETCH_STATUS = 0 
BEGIN 
---PRINT @@FETCH_STATUS 

FETCH NEXT FROM cursorName 
INTO @rulescheduleid, @scheduletype 

は間違いなく間違っている - @@FETCH_STATUSは、前FETCH文の結果に基づいて更新された - しかし、あなたはすでに使用されてきましたあなたがこのループに到達する前にそのFETCHの結果を確認してからすぐに別のFETCHを実行し、それを使用した後でなければその成功を確認しないでください。

通常の形式は、次のようになります。

DECLARE <cursor> 
OPEN <cursor> 

FETCH NEXT FROM <cursor> 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    <process result from previous fetch> 

    FETCH NEXT FROM <cursor> 
END 

CLOSE <cursor> 
DEALLOCATE <cursor> 

の推測では、あなたは結果セットの最後を超えつもりたび、それはNULLにローカル変数(例えば@rulescheduleid)を設定した後、あなたですNULLとの連結を実行してNULL結果を生成する。

私は、あなたがしようとしていることと、カーソルがまったく必要かどうかを理解するために、コードの壁の残りの部分を読んだことはありません。

+1

カーソルが必要かどうかについての良い点は、常に言及する価値は+1です。 –

+0

私は私のコードを変更しましたプロシージャはエラーなしで実行されていますが、ループの終わりに(condititinsに基づいてデータベースからretrived)変数にasaignedループが完了した後に空になっています私の変更されたコード – ameenulla

0

基本的には、カーソルを次のように構成されています。今

open cursor 

fetch first row into vars 

process the fetched values 

while @@fetch_status = 0 

    fetch next row into vars 
    process the fetched values 

end 

close cursor 

process the fetched values部分は、あなたの手順ではかなりのもので、それは一つのことで、あなたのコードで2回繰り返します。もう1つ重要なことは、ループ本体でフェッチされた値を処理するときに、値がフェッチされた直後であることです。しかし、最後の反復であれば、NULLをフェッチし、NULLを処理した結果、出力変数もNULLになります。

このような状況ではどうしたらよいですか?ただ、ループの前にprocess the fetched values段階をドロップすると、カーソルの構造は次のようになるように、それ前の位置に、fetch next row into varsステップの後で、他のものを、移動:

open cursor 

fetch first row into vars 

while @@fetch_status = 0 

    process the fetched values 
    fetch next row into vars 

end 

この方法では、 の値を処理している場所が1つしかない場合は、値がNULLでないときにのみ実行します。FETCH NEXTがNULLを生成すると、ループが終了し、蓄積された出力が保持されます。

+0

私はコードプロシージャーはエラーなしで実行されていますが、ループ値の終わりに(conditinに基づいてデータベースから取得された)変数にasaignedはループが完了した後に空になります。変更されたコード – ameenulla

+0

私のコードプロシージャーは、任意のエラーがループの終わりに(conditinsに基づいてデータベースからretrived)変数にasaignedループが完了した後ここで私の変更されたコードです私は私の変更されたコードを貼り付けた – ameenulla

0
i had modified my code procedure is running without any error but at the end of the loop values(retrived from db based on conditoins) asaigned to a variable is becoming empty after the loop is completed here is my modified code 

FOR RuleIds = '' DECLAREの@RuleIds DECLARE CNAME CURSOR @セットの別個のIDを選択し、TABLE1 OPEN CURSORNAMEからSTYPE - 、 は@rul INTO NEXT CNAME からのフェッチカーソルをオープン@sche

WHILE @@FETCH_STATUS = 0 
BEGIN 
if(lower(@sched) ='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]))) 
-- here iam trying to store retreved values in to variable and returning to the java code it is retuning null values 
PRINT @RuleIds 
-- it prints the values assaigned to @RuleIds variable ex values(13,12,2) 
FETCH NEXT FROM cName 
INTO @rul, @sche 
END 
CLOSE cName 
DEALLOCATE cName 
--after deaalocating of cursor the valuse stored in @RuleIds(variable) is becoming empty 
PRINT @RuleIds 
---this print statement printing nothing 
+0

これは質問と回答ベースですサイト - これは答えではありません - あなたの質問を編集*して、新しい情報を追加するべきです。また、上記の書式設定は、書式設定ツールを使用して実験し、質問にまともな編集を追加することでかなり壊れています。 –

0

私は個人的にカーソルを使用しません。私はCASEステートメントをselectで使用して、アプリケーションに送り返し、データセット全体を返すフィールドを取得します。次に、アプリ側でデータセットをループして、アプリで必要に応じてパラメータ値を設定します。

関連する問題