大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说达梦数据库dblink使用_达梦指定使用数据文件,希望您对编程的造诣更进一步.
- 软硬件环境
- DM-Oracle
- 环境准备
- 建立DBLink
- 功能验证
- DBlink增删查改
- Dblink事务验证:commit和rollback
- 存储过程中使用Dblink
- 在游标中使用Dblink
- 常见问题
- DM-DM
- 环境准备及建立DBLink
- 建立DBlink
- 功能验证
- DM-SQLServer
- 环境准备及建立DBLink
- 建立DBlink
- 功能验证
- 注意事项
- 参考文献
软硬件环境
软件 | 版本 |
---|---|
操作系统 | Redhat 7 及以上版本,麒麟V10 |
DM 数据库 | DM 8.0 及以上版本 |
CPU 架构 | x86、ARM、龙芯、飞腾等国内外主流 CPU |
DM-Oracle
环境准备
在DM服务器中完成Dblink环境准备工作。
- 在Oracle官网根据DM服务器系统下载oci包,instantclient-basic-linux.x64-12.2.0.1.0.zip,解压后将其中的
*so*
文件上传至服务器/lib64目录中:
方式二:可以将解压后的整个目录上传,并将该目录加至系统环境变量LD_LIBRARY_PATH:LD_LIBRARY_PATH=”$LD_LIBRARY_PATH:/usr/instantclient_12_2″。
或者添加到 /etc/ld.so.conf.d目录下指定的配置文件中:
[root@localhost ~]# echo /usr/instantclient_12_2 > /etc/ld.so.conf.d/liboci.conf
[root@localhost ~]# ldconfig
然后创建软连接
# ln -s libclntsh.so.12.1 libclntsh.so
- 检查依赖,并安装缺失依赖:
# ldd libclntsh.so.12.1
# yum install libnsl
--如果不能联网,则到官方yum源中下载对应rpm包【libnsl-2.28-36.1.ky10.x86_64.rpm】,上传后到对应目录执行离线安装:
# rpm -ivh libnsl-2.28-36.1.ky10.x86_64.rpm
# ldconfig
采用方式二时,需要将不在动态链接库中的文件加入LD_LIBRARY_PATH或/etc/ld.so.conf.d中的配置文件
- 重启DM数据库服务,使其加载 Oracle OCI 驱动
# systemctl restart DmServiceDMSERVER.service
建立DBLink
在DM中建立至Oracle的Dblink:
CREATE OR REPLACE public link ocldblink CONNECT "ORACLE" WITH testdblink IDENTIFIED BY testdblink USING "10.63.33.45/xircw2";
功能验证
在Oracle中准备测试表及数据:
DBlink增删查改
- 通过DBlink查询
- 通过DBLink新增
- 通过DBLink修改
- 通过DBLink删除
Dblink事务验证:commit和rollback
- 会话1:新增数据,未commit
- 会话2:查询时,无法查到新增的数据
- 会话1:commit后,会话2再查询即可查到新增数据:
commit; --会话1
SELECT * FROM YY_T_CRUD@ocldblink; --会话2
- 执行两次insert,然后rollback,新增事务已被回滚,无法查询到新增的数据:
存储过程中使用Dblink
- 创建存储过程
CREATE OR replace PROCEDURE p_test(operate IN varchar, code IN varchar, name IN varchar)
AS
v_code varchar2(50) :=code;
v_name varchar2(50) :=name;
BEGIN
CASE operate
WHEN "INSERT" THEN INSERT INTO YY_T_CRUD@ocldblink(code,name) VALUES (v_code,v_name);
WHEN "DELETE" THEN DELETE FROM YY_T_CRUD@ocldblink WHERE code = v_code;
WHEN "UPDATE" THEN UPDATE YY_T_CRUD@ocldblink SET name = v_name WHERE code = v_code;
ELSE DBMS_OUTPUT.PUT_LINE ("operate[" ||operate|| "] not support !");
--ELSE PRINT "operate[" ||operate|| "] not support !";
END;
END;
/
- 存储过程使用Dblink-新增数据
- 存储过程使用Dblink-更新数据
- 存储过程使用Dblink-删除数据
在游标中使用Dblink
- 在游标中使用Dblink,并直接获取相关数据:
DECLARE
CURSOR cur_row IS SELECT CODE,NAME FROM YY_T_CRUD@ocldblink;
BEGIN
FOR v_row IN cur_row LOOP
DBMS_OUTPUT.PUT_LINE("测试数据code="|| v_row.CODE || ",name=" || v_row.NAME);
END LOOP;
END;
/
执行结果:
- 在游标中使用Dblink,进行删改操作
准备数据
通过游标进行删改操作:
DECLARE
CURSOR cur_row IS SELECT CODE,NAME FROM YY_T_CRUD@ocldblink;
BEGIN
FOR v_row IN cur_row LOOP
IF v_row.CODE = "crud_cursor_update" THEN
DBMS_OUTPUT.PUT_LINE("更新数据code="|| v_row.CODE || ",name=" || v_row.NAME);
UPDATE YY_T_CRUD@ocldblink SET NAME = "游标使用示例_update by cursor" WHERE code = v_row.CODE;
COMMIT;
ELSEIF v_row.CODE = "crud_cursor_delete" THEN
DBMS_OUTPUT.PUT_LINE("删除数据code="|| v_row.CODE || ",name=" || v_row.NAME);
DELETE FROM YY_T_CRUD@ocldblink WHERE code = v_row.CODE;
COMMIT;
ELSE
DBMS_OUTPUT.PUT_LINE("跳过数据code="|| v_row.CODE || ",name=" || v_row.NAME);
END IF;
END LOOP;
END;
/
执行结果:
常见问题
-
DM 数据库使用 ODBC 方式创建 DBLINK 访问 Oracle 时报错 “[-70037]:字符串不完整” 或者中文显示为问号
方法1:使用 Oracle OCI 的方式创建 DBLINK。如果数据库启动时未加载 Oracle OCI 驱动,需要重启数据库加载 Oracle OCI 驱动。
方法2:配置与Oracle相同的NLS环境:
[dmdba@localhost bin]$ vim ~/.bash_profile ---添加下面内容
export NLS_LANG="AMERICAN_AMERICA.ZHS16GBK"
[dmdba@localhost bin]$ source ~/.bash_profile
[dmdba@localhost bin]$ env|grep NLS_LANG
NLS_LANG=AMERICAN_AMERICA.ZHS16GBK
NLS_LANG 环境变量并生效后,在当前窗口重启 DM 数据库服务。
-
dm 通过 dblink 查询 Oracle 表,报错:字符串截断
dm 通过 dblink 查询 Oracle 表时,需要与 Oracle 字符集保持一致。如果 Oracle 端字符集为 GB,dm 端为 UTF-8,则会报字符串截断问题。
DM-DM
环境准备及建立DBLink
- 在DM中完成Dblink环境准备工作。
[yypoc_inst1] --DM-A
mal_inst_name = DAMENG
mal_host = 10.63.34.143
mal_port = 5237
mal_inst_host = 10.63.34.143
mal_inst_port = 5236
[dbtest_inst2] --DM-B
mal_inst_name = DMSERVER
mal_host = 10.63.34.173
mal_port = 5237
mal_inst_host = 10.63.34.173
mal_inst_port = 5236
mal_inst_name为dm.ini中的实例名,两个实例名不能相同
mal_inst_port为dm.ini中的实例端口号
mal_port为对外通讯端口号,自定义配置,不与实例端口相同
A、B服务器的dmmal.ini内容需相同
建立DBlink
在DM-A中建立至DM-B的Dblink:
CREATE OR REPLACE PUBLIC LINK dmdblink CONNECT "DAMENG" WITH "TEST" IDENTIFIED BY "test12345" USING "DMSERVER";
功能验证
在DM-B中准备测试表及数据:
--DM dblink test
CREATE TABLE YY_T_CRUD (
code varchar2(50),
name varchar2(50),
defnum NUMBER,
memo varchar2(100),
dr int DEFAULT 0,
ts char(19) DEFAULT to_char(sysdate, "yyyy-MM-dd hh24:mi:ss")
);
INSERT INTO YY_T_CRUD VALUES ("crud","增删查改示例",235.22,"备注",0,to_char(sysdate, "yyyy-MM-dd hh24:mi:ss"));
将DM-Oracle测试用例中的ocldblink替换为dmdblink后,进行增删查改、存储过程、游标测试,结果与DM-Oracle中的保持一致,皆验证通过。
测试用例如下:
----增删查改验证
INSERT INTO YY_T_CRUD@dmdblink VALUES ("crud_CUD","update示例",22235.22,"备注",0,to_char(sysdate, "yyyy-MM-dd hh24:mi:ss"));
SELECT * FROM YY_T_CRUD@dmdblink;
UPDATE YY_T_CRUD@dmdblink SET memo = "更新by dblink" WHERE code = "crud_CUD";
SELECT * FROM YY_T_CRUD@dmdblink;
DELETE FROM YY_T_CRUD@dmdblink WHERE code = "crud_CUD";
SELECT * FROM YY_T_CRUD@dmdblink;
----事务验证
--会话1
INSERT INTO YY_T_CRUD@dmdblink VALUES ("crud_commit_rollback","事务验证示例",2.23,"备注",0,to_char(sysdate, "yyyy-MM-dd hh24:mi:ss"));
SELECT * FROM YY_T_CRUD@dmdblink;
COMMIT;
INSERT INTO YY_T_CRUD@dmdblink VALUES ("crud_commit_rollback2","事务验证示例2",2,"备注",0,to_char(sysdate, "yyyy-MM-dd hh24:mi:ss"));
INSERT INTO YY_T_CRUD@dmdblink VALUES ("crud_commit_rollback3","事务验证示例3",3,"备注",0,to_char(sysdate, "yyyy-MM-dd hh24:mi:ss"));
SELECT * FROM YY_T_CRUD@dmdblink;
ROLLBACK;
--会话2
SELECT * FROM YY_T_CRUD@dmdblink;
----存储过程验证
CREATE OR replace PROCEDURE p_dm_test(operate IN varchar, code IN varchar, name IN varchar)
AS
v_code varchar2(50) :=code;
v_name varchar2(50) :=name;
BEGIN
CASE operate
WHEN "INSERT" THEN INSERT INTO YY_T_CRUD@dmdblink(code,name) VALUES (v_code,v_name);
WHEN "DELETE" THEN DELETE FROM YY_T_CRUD@dmdblink WHERE code = v_code;
WHEN "UPDATE" THEN UPDATE YY_T_CRUD@dmdblink SET name = v_name WHERE code = v_code;
ELSE DBMS_OUTPUT.PUT_LINE ("operate[" ||operate|| "] not support !");
--ELSE PRINT "operate[" ||operate|| "] not support !";
END;
END;
/
p_dm_test("INSERT","crud_proc","存储过程示例");
SELECT * FROM YY_T_CRUD@dmdblink;
p_dm_test("UPDATE","crud_proc","存储过程示例-update");
SELECT * FROM YY_T_CRUD@dmdblink;
p_dm_test("DELETE","crud_proc","存储过程示例-update");
SELECT * FROM YY_T_CRUD@dmdblink;
p_dm_test("other","code","name");
----游标验证
DECLARE
CURSOR cur_row IS SELECT CODE,NAME FROM YY_T_CRUD@dmdblink;
BEGIN
FOR v_row IN cur_row LOOP
DBMS_OUTPUT.PUT_LINE("测试数据code="|| v_row.CODE || ",name=" || v_row.NAME);
END LOOP;
END;
/
--游标更新数据
p_dm_test("INSERT","crud_cursor","游标使用示例");
p_dm_test("INSERT","crud_cursor_update","游标使用示例");
p_dm_test("INSERT","crud_cursor_delete","游标使用示例");
SELECT * FROM YY_T_CRUD@dmdblink;
DECLARE
CURSOR cur_row IS SELECT CODE,NAME FROM YY_T_CRUD@dmdblink;
BEGIN
FOR v_row IN cur_row LOOP
IF v_row.CODE = "crud_cursor_update" THEN
DBMS_OUTPUT.PUT_LINE("更新数据code="|| v_row.CODE || ",name=" || v_row.NAME);
UPDATE YY_T_CRUD@dmdblink SET NAME = "游标使用示例_update by cursor" WHERE code = v_row.CODE;
COMMIT;
ELSEIF v_row.CODE = "crud_cursor_delete" THEN
DBMS_OUTPUT.PUT_LINE("删除数据code="|| v_row.CODE || ",name=" || v_row.NAME);
DELETE FROM YY_T_CRUD@dmdblink WHERE code = v_row.CODE;
COMMIT;
ELSE
DBMS_OUTPUT.PUT_LINE("跳过数据code="|| v_row.CODE || ",name=" || v_row.NAME);
END IF;
END LOOP;
END;
/
DM-SQLServer
环境准备及建立DBLink
- 在DM中完成Dblink环境准备工作。
-- 解压上传的介质包
#tar -zxvf unixODBC-2.3.0.tar.gz
#tar -zxvf freetds-1.2.21.tar.gz
-- 编译解压的安装包,编译命令如下,编译过程省略
#cd unixODBC-2.3.0/
#./configure
#make && make install
#cd ../freetds-1.2.21/
#./configure
#make && make install
/usr/local/bin/tsql -H 10.63.34.173 -p 1433 -U test -P test12345 -D test
$ cp -r /usr/local/lib/* /data/dm/dmdbms/bin
- 动编写【freetds.conf】内容如下:
#vi /usr/local/etc/freetds.conf
[mssqlserver]
host = 10.63.34.173
port = 1433
tds version = 7.3
client charset = UTF-8
- 测试通过服务器条目进行连接,测试连接成功:
#/usr/local/bin/tsql -S mssqlserver -U test -P test12345
- 编写【odbcinst.ini】内容如下
# vi /usr/local/etc/odbcinst.ini
[FreeTDS]
Description=ODBC of FreeTDS for MS SQL 2019
Driver=/usr/local/lib/libtdsodbc.so
- 编写【odbc.ini】内容如下
#vi /usr/local/etc/odbc.ini
[mssql]
Description = SQLServer
Driver = /usr/local/lib/libtdsodbc.so
Trace = on
Server = 10.63.34.173
Port = 1433
User = test
Password = test12345
Database = test
TDS_Version = 7.3
- 测试ODBC连接SQL Server数据库
$ isql -v mssql test test12345
建立DBlink
在DM建立至SQLServer的Dblink:
CREATE OR REPLACE PUBLIC LINK "ZXZQ_DM_POC"."TESTMS" connect "ODBC" with "test" identified by "test12345" using "mssql";
drop link "ZXZQ_DM_POC"."TESTMS";
功能验证
在SQLServer中准备测试表及数据:
create table testtable (
id int,
name varchar(10),
sex char(2),
adress varchar(30)
)
go
insert into testtable values(1,"小宇","男","贵州");
insert into testtable values(2,"小丽","女","云南");
insert into testtable values(3,"小对","男","广东");
select * from testtable;
将DM-Oracle测试用例中的ocldblink及表替换为TESTMS后,进行增删查改、存储过程、游标测试,结果与DM-Oracle中的保持一致,皆验证通过。
测试用例如下:
----增删查改验证
insert into testtable@testms values(4,"小红","女","深圳");
SELECT * FROM testtable@testms;
update testtable@testms set “adress”=’香港’ where “id”=4;
SELECT * FROM testtable@testms;
delete from testtable@testms where “id”=4;
SELECT * FROM testtable@testms;
----事务验证
--会话1
insert into testtable@testms values(4,"小红","女","深圳");
SELECT * FROM testtable@testms;
COMMIT;
insert into testtable@testms values(2,"小丽","女","云南");
insert into testtable@testms values(3,"小对","男","广东");
SELECT * FROM testtable@testms;
ROLLBACK;
--会话2
SELECT * FROM testtable@testms;
----存储过程验证
CREATE OR replace PROCEDURE p_ss_test(operate IN varchar, code IN INT, name IN varchar, sex IN varchar, adress IN varchar)
AS
v_code NUMBER :=code;
v_name varchar2(50) :=name;
v_sex varchar2(50) :=sex;
v_adress varchar2(50) :=adress;
BEGIN
CASE operate
WHEN "INSERT" THEN INSERT INTO testtable@testms("id","name","sex","adress") VALUES (v_code,v_name,v_sex,v_adress);
WHEN "DELETE" THEN DELETE FROM testtable@testms WHERE "id" = v_code;
WHEN "UPDATE" THEN UPDATE testtable@testms SET "adress" = v_adress WHERE "id" = v_code;
ELSE DBMS_OUTPUT.PUT_LINE ("operate[" ||operate|| "] not support !");
--ELSE PRINT "operate[" ||operate|| "] not support !";
END;
END;
/
p_ss_test("INSERT",99,"存储过程","男","香港");
SELECT * FROM testtable@testms;
p_ss_test("UPDATE",99,"存储过程","男","武汉");
SELECT * FROM testtable@testms;
p_ss_test("DELETE",99,"存储过程","男","武汉");
SELECT * FROM testtable@testms;
p_ss_test("other",99,"存储过程","男","武汉");
----游标验证
DECLARE
CURSOR cur_row IS SELECT "id","name","adress" FROM testtable@testms;
BEGIN
FOR v_row IN cur_row LOOP
DBMS_OUTPUT.PUT_LINE("测试数据id="|| v_row."id" || ",name=" || v_row."name" || ",adress=" || v_row."adress");
END LOOP;
END;
/
--游标更新数据
p_ss_test("INSERT",80,"游标","男","香港");
p_ss_test("INSERT",81,"游标","男","香港");
p_ss_test("INSERT",82,"游标","男","香港");
DELETE FROM testtable@testms WHERE "id" IN (80,81,82);
SELECT * FROM testtable@testms;
DECLARE
CURSOR cur_row IS SELECT "id","name","adress" FROM testtable@testms;
BEGIN
FOR v_row IN cur_row LOOP
IF v_row."id" = 81 THEN
DBMS_OUTPUT.PUT_LINE("更新数据id="|| v_row."id" || ",name=" || v_row."name" || ",adress=" || v_row."adress");
UPDATE testtable@testms SET "name" = "游标up" WHERE "id" = v_row."id";
COMMIT;
ELSEIF v_row."id" = 82 THEN
DBMS_OUTPUT.PUT_LINE("删除数据id="|| v_row."id" || ",name=" || v_row."name" || ",adress=" || v_row."adress");
DELETE FROM testtable@testms WHERE "id" = v_row."id";
COMMIT;
ELSE
DBMS_OUTPUT.PUT_LINE("输出数据id="|| v_row."id" || ",name=" || v_row."name" || ",adress=" || v_row."adress");
END IF;
END LOOP;
END;
/
注意事项
-
数据库连接目前只支持 DM、Oracle 或 ODBC。
-
DM-DM 的同构数据库链接不支持 MPP 环境,DM 与异构数据库的数据库链接支持 MPP 环境。
-
增删改不支持 INTO 语句。
-
不支持使用游标进行增删改操作。
-
不支持操作远程表的复合类型列。
-
DBLINK 理论上不支持 LOB 类型列的操作,但支持简单的增删改语句中使用常量来对 LOB 类型列进行操作。
参考文献
https://eco.dameng.com/docs/zh-cn/sql-dev/practice-dblink.html
https://eco.dameng.com/docs/zh-cn/faq/faq-dm-databse.html
原文地址:https://www.cnblogs.com/yaenli/archive/2022/05/19/16046187.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/5230.html