一聚教程网:一个值得你收藏的教程网站

热门教程

SQLServer触发器调用JavaWeb接口代码过程解析

时间:2022-06-29 07:42:45 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下SQLServer触发器调用JavaWeb接口代码过程解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

这几天接到一个需求需要吧不同系统的数据库进行同步,需要我做一个中间平台进行连接,瞬间就想到了触发器调用接口然后通过API进行传递再写入另一个数据库。

sqlServer触发器调用JavaWeb接口

1、开启 Ole Automation Procedures

sqlServer要想调用web接口,就要使用自带的存储过程。而这些存储过程2005版本以后默认时关闭的,所以要先开启。

 sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

关闭 Ole Automation Procedures

sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

关闭高级选项

sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO

然后就可以进行写触发器,因为我这边表的内容比较多,大家直接看代码就好了。

CREATE TRIGGER [dbo].[kingdee]
ON [dbo].[称重信息]
WITH EXECUTE AS CALLER
FOR INSERT
AS
BEGIN
 -- Type the SQL Here.
	DECLARE @fbillno int;
	DECLARE @carid varchar(4000);
	DECLARE @type varchar(4000);
	DECLARE @sendplace varchar(4000);	
	DECLARE @consignee varchar(4000);		
	DECLARE @material varchar(4000);
	DECLARE @specification varchar(4000);
	DECLARE @gw varchar(4000);
	DECLARE @tare varchar(4000);	
	DECLARE @nw varchar(4000);
	DECLARE @kw varchar(4000);
  DECLARE @aw varchar(4000);
	DECLARE @price varchar(4000);
	DECLARE @money varchar(4000);
	DECLARE @zfxs varchar(4000);
	DECLARE @square varchar(4000);
	DECLARE @weighingcost varchar(4000);
	DECLARE @gwtime varchar(4000);
	DECLARE @taretime varchar(4000);
	declare @url varchar(4000);
	declare @object int;
	declare @responseText varchar(4000);
	
	SELECT @fbillno = (select 流水号 from inserted);
	SELECT @carid = (select 车号 from inserted);
	SELECT @type = (select 过磅类型 from inserted);
	SELECT @sendplace = (select 发货单位 from inserted);
	SELECT @consignee = (select 收货单位 from inserted);
	SELECT @material = (select 货名 from inserted);
	SELECT @specification = (select 规格 from inserted);	
	SELECT @gw = (select 毛重 from inserted);
	SELECT @tare = (select 皮重 from inserted);
	SELECT @nw = (select 净重 from inserted);	
	SELECT @kw = (select 扣重 from inserted);
	SELECT @aw = (select 实重 from inserted);
	SELECT @price = (select 单价 from inserted);	
	SELECT @money = (select 金额 from inserted);
	SELECT @zfxs = (select 折方系数 from inserted);
	SELECT @square = (select 方量 from inserted);	
	SELECT @weighingcost = (select 过磅费 from inserted);
	SELECT @gwtime = (select 毛重时间 from inserted);
	SELECT @taretime = (select 皮重时间 from inserted);
	SELECT @url = 'http://ip/saveweight/' + CONVERT(INT, @fbillno)+'/'+ CONVERT(VARCHAR, @carid)
	+'/'+ CONVERT(VARCHAR, @type)+'/'+ CONVERT(VARCHAR, @sendplace)+'/'+ CONVERT(VARCHAR, @consignee)+'/'+ CONVERT(VARCHAR, @material)
	+'/'+ CONVERT(VARCHAR, @specification)+'/'+ CONVERT(VARCHAR, @gw)+'/'+ CONVERT(VARCHAR, @tare)+'/'+ CONVERT(VARCHAR, @nw)
	+'/'+ CONVERT(VARCHAR, @kw)+'/'+ CONVERT(VARCHAR, @aw)+'/'+ CONVERT(VARCHAR, @price)+'/'+ CONVERT(VARCHAR, @money)
	+'/'+ CONVERT(VARCHAR, @zfxs)+'/'+ CONVERT(VARCHAR, @square)+'/'+ CONVERT(VARCHAR, @weighingcost)+'/'+ CONVERT(VARCHAR, @gwtime)
	+'/'+ CONVERT(VARCHAR, @taretime);
	print @url;
-- 	insert into one (id, name) VALUES (@id, @url);
	
	exec sp_OACreate'MSXML2.XMLHTTP',@object out
	exec sp_OAMethod @object,'open',null,'get',@url,'false'
	exec sp_OAMethod @object,'send'
	exec sp_OAMethod @object,'responseText',@responseText output
 
	print @responseText
 
	exec sp_OADestroy @object
 
	SET NOCOUNT ON;
END
GO

EXEC sp_addextendedproperty
'MS_Description', N'称重信息表插入后触发器',
'SCHEMA', N'dbo',
'TABLE', N'称重信息',
'TRIGGER', N'kingdee'

看着复杂其实一点也不复杂,因为这个数据库的表是中文(不是我建的表系统中的中文的我没办法改),大致就是获取插入的数据然后进行触发,请求,然后API执行插入另一个数据库。

然后执行插入语句就可以看到执行的结果了~

热门栏目