1 下载pg odbc驱动
https://www.postgresql.org/ftp/odbc/versions/msi/
2 创建pg 数据源
(1)cmd odbcad32
(2)选择系统DSN,选择驱动程序 PostgreSQL ANSI(),配置pg 数据源信息。
3 将sqlserver中表的结构和数据导入到pg
4 创建pg链接服务器(以下操作在sqlserver查询窗口执行,本例pg数据库名pg01,sqlserver数据库名sql01,表名student)
USE [sql01]exec sp_addlinkedserver
@server='PostgreSQL01', --ODBC里面data source name
@srvproduct='postgres', --自己随便
@provider='MSDASQL', --固定这个
@datasrc=NULL, @location=NULL,
@provstr='DRIVER={PostgreSQL ANSI};SERVER=127.0.0.1;DATABASE=pg01;UID=postgres;PORT=5432;',
@catalog = NULL
GO
USE [sql01]
---创建连接postgres数据库的账号及密码
exec sp_addlinkedsrvlogin
@rmtsrvname='PostgreSQL01',----ODBC里面data source name
@useself='false',
@rmtuser='postgres',---postgres账号
@rmtpassword='root';--postgres账号其密码
GO
5 建立允许远程访问操作
USE [sql01]
GO
EXEC sql01.dbo.sp_serveroption @server=N'PostgreSQL01',@optname=N'rpc out', @optvalue=N'TRUE'
GO
EXEC sql01.dbo.sp_serveroption @server=N'PostgreSQL01',@optname=N'remote proc transaction promotion',@optvalue=N'false'
GO
--PostgreSQL01为数据源名
6 建立LOOPBACK 服务器链接 (回路)
USE sql01
GO
EXEC sp_addlinkedserver @server=N'loopback01',@srvproduct=N' ',@provider=N'SQLNCLI',
@datasrc=@@SERVERNAME
go
7 设置服务器链接选项,阻止SQL Server 由于远过程调用而将本地事务提升为分布事务(重点)
USE sql01
GO
EXEC sql01.dbo.sp_serveroption @server=N'loopback01',@optname=N'rpc out',@optvalue=N'TRUE'
GO
EXEC sql01.dbo.sp_serveroption @server=N'loopback01',@optname=N'remote proc transaction promotion',@optvalue=N'false'
GO
**8 编写触发器和存储过程>*(根据表结构自行更改,本例student表结构为:
id INT,
sname varchar(100),
sage INT
(1) insert
--创建触发器
USE sql01
GO
CREATE TRIGGER TR_INSERT_pg_TB ON sql01.dbo.student
FOR INSERT
AS
DECLARE @id INT,@sname varchar(100),@sage INT
SELECT @id=id,@sname=sname,@sage=sage FROM INSERTED;
BEGIN
EXEC loopback01.sql01.dbo.sp_insert_pg_tb @id,@sname,@sage;
END
go
--创建存储过程,
USE sql01
GO
CREATE PROCEDURE SP_INSERT_pg_tb
@id INT,
@sname varchar(100),
@sage INT
AS
BEGIN
SET NOCOUNT ON
--插入数据到pg表中,若id为主键,以下插入语句可能出现主键必须唯一错误,可不插入自增主键id
INSERT OPENQUERY(PostgreSQL01,'select * from public.student') values (@id,@sname,@sage);
SET NOCOUNT OFF
END
GO
(2)update
USE sql01
GO
CREATE TRIGGER TR_UPDATE_pg_TB ON sql01.DBO.student
FOR UPDATE
AS
DECLARE @id INT,@sname varchar(100),@sage INT
SELECT @id=id,@sname=sname,@sage=sage FROM INSERTED;
BEGIN
EXEC loopback01.sql01.dbo.sp_update_pg_tb @id,@sname,@sage;
END
GO
USE sql01
GO
CREATE PROCEDURE SP_UPDATE_pg_tb
@id INT,
@sname varchar(100),
@sage INT
AS
BEGIN
SET NOCOUNT ON
--更新pg的表,以下语句通过WHERE id=@id 寻找要更新的行,若sqlserver和pg中id不一致,where后条件需更改
UPDATE OPENQUERY(PostgreSQL01,'select * from public.student') SET sname=@sname,sage=@sage WHERE id=@id
SET NOCOUNT OFF
END
GO
(3) delete
USE sql01
GO
CREATE TRIGGER TR_DELETE_pg_TB ON sql01.dbo.student
FOR DELETE
AS
DECLARE @id INT
SELECT @id=id FROM DELETED;
BEGIN
EXEC loopback01.sql01.dbo.sp_DELETE_pg_tb @id;
END
go
USE sql01
GO
CREATE PROCEDURE SP_DELETE_pg_tb
@id INT
AS
BEGIN
SET NOCOUNT ON DELETE OPENQUERY(PostgreSQL01,'select * from public.student) WHERE id=@id
SET NOCOUNT OFF
END
go
来源:CSDN
作者:weixin_45876565
链接:https://blog.csdn.net/weixin_45876565/article/details/104579753