我想寫一個存儲過程,在AS400的DB2,它在本地數據庫上運行查詢(存儲過程相同;它是命名爲DBLocale
),另一個在遠程數據庫(我們稱之爲DBRemoto
)。在iSeries Navigator中,我可以看到他們都選擇「數據庫」節點。我嘗試了一些CONNECT TO DBRemoto;
和/或SET CONNECTION DBRemoto;
,但得到了奇怪的結果:「連接已經存在」,「連接不存在」,「CALL指令完成」(但沒有結果集)等。在一個過程連接到2個數據庫:一個本地和另一個遠程
這將是我所期望的事情:
CREATE PROCEDURE MYLIB.TEST_CONNECT_INSIDE_PROCEDURE
DYNAMIC RESULT SETS 1
LANGUAGE SQL
READS SQL DATA
BEGIN
DECLARE sql_string VARCHAR(1024) DEFAULT '';
DECLARE locale INT DEFAULT -1;
DECLARE remoto INT DEFAULT -1;
DECLARE cur CURSOR FOR stmt;
DECLARE curOut CURSOR FOR select locale, remoto from SYSIBM.SYSDUMMY1;
-- run the same query in both DBs; I checked, results must be different
SET sql_string = 'select count(*) as QTY from MYLIB.MYTABLE';
-- run locally
CONNECT TO DBLocale; --it's one of my attempts
PREPARE stmt FROM sql_string;
OPEN cur;
FETCH cur INTO locale;
CLOSE cur;
-- run remotely
CONNECT TO DBRemoto; --it's one of my attempts
PREPARE stmt FROM sql_string;
OPEN cur;
FETCH cur INTO remoto;
CLOSE cur;
-- output results
OPEN curOut;
SET RESULT SETS CURSOR curOut;
END;
我把它與
call MYLIB.TEST_CONNECT_INSIDE_PROCEDURE()
誰能告訴我,如果有可能,並說明它是如何工作的,好嗎?如果在程序中不可行,是否有解決方法?非常感謝!
Bob Cozzi和家庭人蔘考。真棒。 – jamesallman 2012-02-10 17:27:07
謝謝!你確定密碼需要引號嗎?無論如何,它在任何情況下都不起作用:它不會創建過程並給我一個SQL7032錯誤。 – bluish 2012-02-13 13:16:20