2014-08-28 39 views
0

我想從遠程數據庫中的表中選擇數據,並將其數據庫鏈接放在變量中,我該怎麼做?動態從數據庫鏈接的表中選擇

我的查詢是這樣的:

select `table_column` form [email protected]:any_variable_1 where any_column= :any_variable_2; 

注: any_variable_1是一個字符串變量包含數據庫鏈接的名稱 any_variable_2是一個字符串變量包含字符串過濾 *這段代碼是在功能在PowerBuilder 8或9 執行*其中我連接到數據庫是Oracle11克

回答

0
 string ls_sql 

     ls_sql="Select column_name from [email protected]"+var_db_link_name+""+& 
        " where column_name='"+var_value+"' and another_column_name='"+another_var+"'" 

     DECLARE var_cursor DYNAMIC CURSOR FOR SQLSA ; 

     PREPARE SQLSA FROM :ls_sql ; 

     OPEN DYNAMIC var_cursor ; 
     FETCH var_cursor INTO :another_var ;//another_var holds the result of the cursor 
     CLOSE var_cursor; 
0

嘗試這種情況:

DECLARE 
    l_var number(10); -- data type you are expecting from the table_column column 
    l_cursor sys_refcursor; 
    l_any_variable_1 varchar2(50) := 'YOUR_DBLINK_HERE'; 
    l_any_variable_2 varchar2(50); 
BEIGN 
    open l_cursor for 'select table_column from [email protected] '|| 
           l_any_variable_1 || 
        ' where any_column = :any_variable_2' 
         using l_any_variable_2; 
    loop 
    fetch l_cursor into l_var; 
    exit when l_cursor%NOTFOUND; 
    DBMS_OUTPUT.put_line(l_var); 
    end loop; 
    close l_cursor; 
END;