免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5310 | 回复: 5
打印 上一主题 下一主题

SYBASE如何跨server操作? [复制链接]

论坛徽章:
1
2017金鸡报晓
日期:2017-01-10 15:19:56
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-01-29 11:08 |只看该作者 |倒序浏览
我有两个SYBASE SERVER,SVR1和SVR2,在SVR1中使用sql语句如何查询SVR2中的数据?

论坛徽章:
0
2 [报告]
发表于 2003-01-29 11:16 |只看该作者

SYBASE如何跨server操作?

1. 在远程服务器及本地服务器

sp_addserver local_server_name,local
重新启动保证设置生效

2. 在本地主机中使用 dsedit添加远程服务器的接口
3. 在本地服务器上添加远程服务器名称

sp_addserver remote_server_name,ASEnterprise,server_net_name

2,3步骤应该在本地服务器及远程服务器两方都进行添加

4. 设置远程服务器选项

exec sp_addserver 远程服务器逻辑名称,类型,本地DSEDIT配置的服务器名称

exec sp_addserver sybcdsrv, sql_server, ase12
exec sp_serveroption sybcdsrv, "timeouts", true
exec sp_serveroption sybcdsrv, "net password encryption", false
exec sp_serveroption sybcdsrv, "readonly", false   (只对CIS生效)
exec sp_serveroption sybcdsrv, "rpc security model A", true
go

5. 增加远程用户与本地用户的映射关系

sp_addremotelogin remote_server_name,local_login,remote_login

local_login及remote_login均为服务器上已经存在的login
local_login跟remote_login口令应该一致,如果不一致,在
Open Client Client-Library编程中可以使用 ct_remote_pwd 命令进行设置
但在isql 及 bcp 中不允许指定 rpc 口令

另外,一旦到远程服务器的连接建立成功,在退出当前会话前,不论远程服务器是否对相关remote_login进行了口令更改,远程存储过程的调用及其他远程操作都可以进行。

5. 设置服务器之间的信任关系

sp_remoteoption remote_server_name,local_login,remote_login,trusted,true

6. 调用远程存储过程

isql -Usa -P -Slocal_server_name
1>;exec remote_server_name.database_name.owner_name.procedure_name
2>;go

另外可以在 isql 中使用connect remote_server_name命令连接到远程服务器上,以测试远程服务器是否配置正常、网络是否正常、远程服务器是否接受连接。

除了远程存储过程调用方式,也可以采用其他一些方式对远程服务器上的数据进行更改,其中包括建立代理表,使用sp_remotesql等方式。具体操作方法请参见ASE命令参考手册及相关文档。

sp_remotesql ase12,"insert into pubs2..tr1 values(09,'test remote sql')"

其他信息:

还可以使用代理表

所对应远程服务器表 信息 在系统表 sysattributes 中 char_value 字段

论坛徽章:
0
3 [报告]
发表于 2003-01-29 11:17 |只看该作者

SYBASE如何跨server操作?

例子两台ASE服务器名称为 Server1,Server2,需要在Server1中建立代理表,并通过Server1上的存储过程对Server1自身及Server2中表进行更新

1. 添加本地服务器名称
    使用isql连接进入服务器Server1,Server2
    1>;select @@servername
    2>;go
    确定服务器本地名称已经生效
    如果返回NULL,
    1>;sp_addserver Server_name,local
    2>;go
    Server_name应该跟interfaces中所用服务器名称一致

    重新启动服务器,让本地名称生效

2. 添加远程服务器信息
在Server1上执行如下命令:
isql -Usa -P -SServer1
exec sp_addserver Server2, ASEnterprise, Server2
exec sp_addremotelogin Server2, sa, sa
exec sp_remoteoption Server2, sa, sa, trusted, true
go

3. 建立代理表
    在Server2数据库pubs2建立表t_testproxy
    create table t_testproxy (id int,name char(10))
    在Server1数据库test中添加代理表信息
     use test
     go
     create proxy_table t_testproxy at "Server2.pubs2.dbo.t_testproxy"
     代理表名与原表名可以不同

     Server1上查看代理表 信息如下:

Name                           Owner                          Object_type                     
----                           -----                          -----------                     
t_testproxy                    dbo                            user table                       

Data_located_on_segment        When_created                  
-----------------------        ------------                  
default                        Nov 28 2002 11:59AM            

Column_name     Type            Length      Prec        Scale       Nulls       Default_name    Rule_name       Access_Rule_name               Identity   
-----------     ----            ----------- ----------- ----------- ----------- ------------    ---------       ----------------               -----------
id              int                       4        NULL        NULL           0 NULL            NULL            NULL                                     0
name            char                     10        NULL        NULL           0 NULL            NULL            NULL                                     0

exp_row_size reservepagegap fillfactor  max_rows_per_page identity_gap
------------ -------------- ----------- ----------------- ------------
           1              0           0                 0            0

concurrency_opt_threshold
-------------------------
                        0

Object is Remote/External
-------------------------
presales.pubs2.dbo.t_testproxy            //可从此处看出代理表所指向的真正对象

Object created with 'existing' option

Object does not have any indexes.
No defined keys for this object.
Object is not partitioned.
Lock scheme Allpages
The attribute 'exp_row_size' is not applicable to tables with allpages lock scheme.
The attribute 'concurrency_opt_threshold' is not applicable to tables with allpages lock scheme.


4. 代理表测试
    Server2上插入数据:
    insert into t_testproxy values(1,'Server2')
    Server2上插入数据:
    insert into t_testproxy values(2,'Server1')

    Server1上查询数据:
    select * from t_testproxy

    id          name      
----------- ----      
          1 Server2   
          2 Server1     

5. 视图测试
     在Server1上test数据库中建立用户表t_testproxyview
     create table t_testproxyview (id int,address varchar(30)
     插入测试数据
      insert into t_testproxyview (1,'Chengdu')
      insert into t_testproxyview (2,'Sichuan')   
      insert into t_testproxyview (3,'sky')

     建立视图
      create view v_proxy as select a.*,b.address from t_testproxy a,t_testproxyview b
      where a.id = b.id

       查询视图
select * from v_proxy

id          name       address                        
----------- ----       -------                        
          1 Server2    Chengdu                        
          2 Server1    Sichuan                        

         视图的更新
         update v_proxy set address = 'test' where id = 2    更新本地表 执行成功  
         update v_proxy set name = 'test' where id = 2    更新远程表 报告错误如下:
The optimizer could not find a unique index which it could use to scan table 'pubs2.dbo.t_testproxy' for cursor 'C11'.
          必须为远程表建立 主键或者唯一索引
          Server2上执行(不能在Server1上为代理表建立主键):
           alter table t_testproxy add constraint pk_t_testproxy primary key (id)

          update v_proxy set name = 'test' where id = 2 再次更新代理表,成功

id          name       address                        
----------- ----       -------                        
          1 Server2    Chengdu                        
          2 test       test                           
         
          对试图中本地表及代理表同时作更新
          update v_proxy set name = 'test1',address = 'test1' where id = 1
          报告错误:
          View 'v_proxy' is not updatable because the FROM clause names multiple tables.
         
          Sybase不支持在视图中一次更新多表

6. 存储过程测试
        在Server1上
         create proc p_proxy(@id int)
         as
         begin
         begin tran
         update t_testproxy set name = 'test3' where id = @id  --更新代理表
         update t_testproxyview set address = 'test3' where id = @id
         commit
         end
         
         exec p_proxy 2

id          name       address                        
----------- ----       -------                        
          1 Server2    Chengdu                        
          2 test3      test3        

          结论:可以在存储过程中对本地及远程表进行操作,并利用事务来保证一致性

      
      书写仓促,难免疏漏,见谅!

论坛徽章:
1
2017金鸡报晓
日期:2017-01-10 15:19:56
4 [报告]
发表于 2003-01-29 11:30 |只看该作者

SYBASE如何跨server操作?

请教blackrose,我只想查询一下另一个库中的数据,是否必须通过代理完成,是否可以向跨库操作一样,使用一个SQL语句完成。

论坛徽章:
0
5 [报告]
发表于 2003-01-29 11:39 |只看该作者

SYBASE如何跨server操作?

不行!
存取数据通过代理表
操作远程服务器通过rpc

论坛徽章:
0
6 [报告]
发表于 2003-01-29 23:35 |只看该作者

SYBASE如何跨server操作?

通过代理表,也应该可以查询吧?不行吗?难道是真的不行?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP