SQLServer触发器调用JavaWeb接口的过程详解

时间:2022-06-01 12:28:12

这几天接到一个需求需要吧不同系统的数据库进行同步,需要我做一个中间平台进行连接,瞬间就想到了触发器调用接口然后通过API进行传递再写入另一个数据库。
sqlServer触发器调用JavaWeb接口
1、开启 Ole Automation Procedures
sqlServer要想调用web接口,就要使用自带的存储过程。而这些存储过程2005版本以后默认时关闭的,所以要先开启。

?
1
2
3
4
5
6
7
8
9
10
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

?
1
2
3
4
5
6
7
8
9
10
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

关闭高级选项

?
1
2
3
4
sp_configure 'show advanced options', 0;
GO
RECONFIGURE;
GO

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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执行插入另一个数据库。
然后执行插入语句就可以看到执行的结果了~

SQLServer触发器调用JavaWeb接口的过程详解

到此这篇关于SQLServer触发器调用JavaWeb接口的过程详解的文章就介绍到这了,更多相关SQLServer触发器调用接口内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/xgcc/archive/2021/12/30/15747664.html