使用 鏈接服務器執行SELECT、UPDATE、INSERT 或 DELETE 及其它命令

當在SQL SERVER中建立好鏈接服務器之後,我們可以使用下面的方法對遠程的數據庫進行相關的操作。

假設鏈接服務器的名稱爲 Orcl

 

SELECT:

    指明要選擇的列的過慮條件,可以傳入到openquery方法中,也可以在選出之後過濾。

    select * from openquery(Orcl,'select * from dept');

    select * from openquery(Orcl,'select * from dept where deptno > 10');

    select * from openquery(Orcl,'select * from dept') where deptno > 10;

 

    也可以使用下面的方法來檢查數據。

    select * from ORCL..SCOTT.DEPT; ---表名一定要用大寫,不然會出現表找不到的錯誤。

    select * from ORCL..SCOTT.DEPT where deptno > 10;

 

UPDATE

    指明要更新的行,過濾條件可以傳入到openquery參數中,也可以在選出後添加過濾條件。

    update openQuery(orcl,'select * from dept where deptno = 10') set dname = 'New Name';

    update openQuery(orcl,'select * from dept') set dname = 'New Name2' where deptno = 10;

 

    同上,也可以使用下面的方法來更新數據

    update ORCL..SCOTT.DEPT set loc = 'NEW YORK' where deptno=10;---表名一定要用大寫

 

INSERT

    指明要插入的列,只要指出要插入的列即可,不需要選出數據。

    insert into openquery(orcl,'select deptno,dname,Loc from dept where 1=0') values (50,'newDept','loc');

    insert into openquery(orcl,'select deptno,dname from dept where 1=0') values (60,'newDept');

 

    同上,也可以使用下面的方法來插入數據

    insert into ORCL..SCOTT.DEPT values(50,'dname','loc')

    對Oracle10g測試發現,如果列數與待插入表的列數不一致時,會報錯,所以必須與原表的列數一致才行。

 

DELETE

    指明過濾條件,過濾條件可以作爲openquery的參數傳入,也可以在openquery之外使用。

    delete from openquery(orcl,'select deptno from dept where deptno=60');

    delete from openquery(orcl,'select deptno from dept') where deptno=50;

    同上,也可以使用下面的方法來刪除數據

    delete from ORCL..SCOTT.DEPT where deptno=60;---表名一定要用大寫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章