SQL Server 访问发送Http请求创建过程

时间:2024-04-09 20:26:38

第一步:创建存储过程

SQL Server 访问发送Http请求创建过程

定位到“存储过程“,单击鼠标右键“新建存储过程“

SQL Server 访问发送Http请求创建过程

发送http请求的存储过程sql语句:

 /*
参数说明?
@URL=http请求地址
@status=状态代?
@returnText=返回?
@object=对象令牌
@errSrc=错误源编?
*/
CREATE PROCEDURE P_GET_HttpRequestData(
@URL varchar(500),
@status int=0 OUT,
@returnText varchar(2000)='' OUT
)
AS
BEGIN
DECLARE @object int,
@errSrc int
/*初始化对*/
EXEC @status = SP_OACreate 'Msxml2.ServerXMLHTTP.3.0', @object OUT
IF @status <> 0
BEGIN
 EXEC SP_OAGetErrorInfo @object, @errSrc OUT, @returnText OUT
 RETURN
END

/*创建链接*/
EXEC @status= SP_OAMethod @object,'open',NULL,'GET',@URL
IF @status <> 0
BEGIN
 EXEC SP_OAGetErrorInfo @object, @errSrc OUT, @returnText OUT
 RETURN
END
EXEC @status=SP_OAMethod @object,'setRequestHeader','Content-Type','application/x-www-form-urlencoded'
/*发起请求*/
EXEC @status= SP_OAMethod @object,'send',NULL
IF @status <> 0
BEGIN
 EXEC SP_OAGetErrorInfo @object, @errSrc OUT, @returnText OUT
 RETURN
END

第二步:创建触发器

SQL Server 访问发送Http请求创建过程

定位到“触发器“,单击鼠标右键选择“新建触发器”

SQL Server 访问发送Http请求创建过程

新建触发器sql语句:

USE [cao]
GO
/****** Object:  Trigger [dbo].[tag_cutest]    Script Date: 07/18/2018 09:49:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[tag_cutest]
on [dbo].[cutest]
    for insert --插入触发
as
begin
declare @id varchar(50), @name varchar(50),@url varchar(4000),@t varchar(10);
select @id=age,@name=name from inserted;
--insert into test2 select id,name from inserted;
set @t='''';--为字符串中添加'我是信息'
set @url='http://www.newcrm.com:8002/index.php/Test/sqltest?'+   
        +'sql=insert into t_test(name1,name2)values('[email protected][email protected][email protected]+','[email protected][email protected][email protected]+')';--url
EXECUTE P_GET_HttpRequestData @url;
--insert into test2(id,name) values(@id,@name);
end;