2017-11-09 4 views
0
私はselect文で変数 city_noを使用していたが、私は結果が 直接値と変数 city_noも、それらの値を使用しての間で異なっていることがわかった

は同じものです。直接値 '020'を使用したときの出力は377でした。変数を使用したときの出力は16でした。なぜ違うのか分からないのですか?実際に私はカウントが(1)16 知っている、私はにselect文の条件を変更し変数を使用しているselectステートメントを使用しているpl/sql forループは間違った結果を出しますか?

city_no:='020'; 
    need_kinds := 0; 
    for need_item in (select n.rootcode, n.need2018, n.got2018 
         from jq_temp_hy_need_2018 n 
         where n.city_no = city_no 
         and (n.need2018 - n.got2018) > 0) loop 
     current_need_map(need_item.rootcode) := need_item.need2018 - 
               need_item.got2018; 
     already_get_map(need_item.rootcode) := need_item.got2018; 
     need_kinds:=need_kinds+1; 
    end loop; 
    -- out put 377 
    dbms_output.put_line(need_kinds); 

がn.city_no =「020」は、出力値が16になった:以下のSQLコードですn.city_no = '020'

city_no:='020'; 
    need_kinds := 0; 
    for need_item in (select n.rootcode, n.need2018, n.got2018 
         from jq_temp_hy_need_2018 n 
         where n.city_no = '020' 
         and (n.need2018 - n.got2018) > 0) loop 
     current_need_map(need_item.rootcode) := need_item.need2018 - 
               need_item.got2018; 
     already_get_map(need_item.rootcode) := need_item.got2018; 
     need_kinds:=need_kinds+1; 
    end loop; 
    -- out put 16 
    dbms_output.put_line(need_kinds); 

答えて

2

あなたの変数の名前は、したがって、列が変数の代わりに使用され、あなたのテーブルの列名と同じであるあなたselectステートメント。

変数の名前を変更して問題を解決します。

+0

ありがとう、それは大きな間違いでした..... – Jaycee

0

値がNULL、または暗黙の変換の場合に問題が発生する可能性があります。

あなたはTO_CHARで両側をキャストしてみてください、と使うべきNVL

-- change your where condition to this: 
where TO_CHAR(NVL(n.city_no, '#@')) = TO_CHAR(city_no) 
+0

あなたの条件を変更したときに377が出力され、city_no型がVARCHAR2(16)でNULLではありません – Jaycee

関連する問題