2015-11-11 19 views
-1

我有一個名爲CustomerDetails的表:在Oracle中行值爲NULL時打印輸入的函數

DeletKey |已更換密鑰

CID0001 | CUSTID01

CID0002 | CUSTID02

CID0003 | (NULL)

當給予功能的輸入是CID0001,CID0003

輸出應該CUSTID01,CID0003。

由於CID0001被替換爲CUSTID01且CID0003爲空,它應該返回輸入值。

如何在plsql中使用多個輸入值編寫函數。當輸入的客戶ID被刪除並被替換時,它應該返回被替換的客戶ID。 如果它沒有被替換(ReplacedKey爲NULL),它應該打印輸入值。

我的代碼:沒有輸入。我應該如何爲此添加輸入值? CID0001 CID0003

我所需要的輸出:CUSTID01的prg-沒有

的prg- DelettedKey CID0003的輸出 ReplacedKey CUSTID01

輸入我應該給的

create or replace function ChangeList 
as 
cursor c1 
select replaced ReplacedKey.CustomerDetails%type 
deleted DeletKey.CustomerDetails%type 
from CustomerDetails; 
begin 
    open c1; 
    loop `` 
    if ReplacedKey is null 
    then 
    dbms_output.put_line('DeletedKey' ||deleted); 
    else 
    dbms_output.put_line('ReplacedKey'||replaced); 
    end if; 
    end loop; 
close c1: 
end; 

輸入 CID0003

+1

你具有特定什麼問題? – EvilTeach

+0

@eviltech其實我用一個光標寫了一個函數,但沒有成功。 – user5505661

+0

我有的另一個問題是沒有任何輸入。正如我給出的輸入數量。我不知道如何給功能輸入! – user5505661

回答

2

所有你需要的是一個nvl fucntio ñ

select NVL(ReplacedKey,DELETKEY) from table 
+0

創建或替換函數changelist(x varchar2) – user5505661

0

帶參數的函數:

create or replace function ChangeList(p_cid varchar2) return varchar2 
as 
    ret varchar2; 
begin 
    select nvl(ReplacedKey, p_cid) 
    into ret 
    from CustomerDetails 
    where DeletKey = p_cid; 

    return ret; 
exception when no_data_found then return 'no data found'; 
end;