2012-12-05 31 views
0

我是sql新手,所以我需要幫助理解這一點。我有3個表格: -使用軟件包和光標連接三張表

表1: - 員工編號。 員工姓名 person_id

表2: - 聯繫號碼: PERSON_ID

表3: - 地址 PERSON_ID

我試圖使一包,並使用一個過程來定義一個遊標。現在我想從使用連接的表中顯示聯繫人號碼,地址和emp號碼。我已經應用了連接條件,但無法理解如何顯示結果。

包體

create or replace package pacakge_name 
    as 
    procedure procedure_name 

    declare 
    cursor cur_name is select * from table1 join table2 on table1.person_id=table2.person_id join table 3 on table1.person_idd=table3.id; 

    var_curname cur_name%rowtype; 

    begin 

    open cur_name; 

    loop 
    fetch cur_name into var_curname 
    exit 
    when cur_name%NOTFOUND; 
    end loop; 
    close cur_name; 
    end; 
+1

您標記了這個mysql,sql-server和oracle,您使用的是什麼RDBMS? – Taryn

+0

對不起。錯誤的標籤。我更正了它 –

回答

1

顯示結果安慰你必須dbms_output.put_line(var_curname.fieldname);

這將是你的光標查詢:

select table2.contact_no, 
table3.address, 
table1.employee_no 
from 
table1, 
table2, 
table3 
where table1.person_id = table2.person_id and 
table2.person_id = table3.person_id 

所以你DBMS_OUTPUT.PUT_LINE會

dbms_output.put_line(var_curname.contact_no||'-'||var_curname.address||'-'||var_curname.employee_no); 
+0

是的,但我已經通過遊標加入了3個表(select * from table1 join table2 on table1.person_id = table2.person_id join table 3 on table1.person_idd = table3.id) will dbms_output.put_line(var_curname.fieldname )仍然工作? –

+0

thanx。查詢的其餘部分是否正確? –

+0

創建包的正常方式是創建一個規範和包體 –