1.创建Insert触发器
USE [db1]
GO
/****** Object: Trigger [dbo].[tr_sm_msg] Script Date: 2019/1/15 19:17:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <lingdian>
-- Create date: <2019-01-15>
-- Description: <Insert触发器>
-- =============================================
DROP TRIGGER IF EXISTS `tr_sm_msg`;
CREATE TRIGGER [dbo].[tr_sm_msg]
ON [dbo].[sm_msg]
AFTER insert
AS
BEGIN
declare @msgId int
set @msgId = (select id from inserted)
exec proc_useJPushAPI @msgId --调用存储过程并传参 如果传入的参数未变则不会触发
SET NOCOUNT ON;
END
2.创建存储过程
USE [db1]
GO
/****** Object: StoredProcedure [dbo].[proc_useJPushAPI] Script Date: 2019/1/15 17:59:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <lingdian>
-- Create date: <2019-01-15>
-- Description: <调用消息推送接口>
-- =============================================
DROP PROCEDURE IF EXISTS `proc_useJPushAPI`;
CREATE PROCEDURE [dbo].[proc_useJPushAPI]
@msgId varchar(100) --sm_msg表的id字段
AS
BEGIN
declare @url varchar(4000) --接口路由
declare @object int --OLE对象实例
declare @responseText varchar(4000) --文本
set @url = 'http://127.0.0.1:8001/JPush/send/' + @msgId
print @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
3.在表中插入数据时触发器会调用接口
insert into sm_msg (msgTitle,msgContent) values ('标题','内容')
备注:可能出现错误
SQL Server 阻止了对组件“Ole Automation Procedures”的 过程“sys.sp_OACreate”的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用“Ole Automation Procedures”。有关启用“Ole Automation Procedures”的详细信息,请搜索 SQL Server 联机丛书中的“Ole Automation Procedures”。语句已终止。
请访问下面地址,可解决:
https://blog.csdn.net/dling8/article/details/86497929