gpt4 book ai didi

sql-server-2008 - 使用 SQL Server 2008 中的触发器和存储过程发送 SMS

转载 作者:行者123 更新时间:2023-12-01 22:42:33 25 4
gpt4 key购买 nike

我在smslog 表上写了一个触发器

ALTER TRIGGER [dbo].[mytrigger]    
ON [dbo].[smslog]
AFTER INSERT
AS
Declare @MobileNo int
Declare @smstext as varchar(300)
Begin
set @MobileNo = ( select mobile from inserted)
set @smstext = (select smstext from inserted)
set @sResponse = 'test'
Exec pr_SendSmsSQL @MobileNo, @smstext, @sResponse
END

存储过程是

create procedure [dbo].[pr_SendSmsSQL] 
@MobileNo varchar(12),
@smstext as varchar(300),
@sResponse varchar(1000) OUT
as
BEGIN
Declare @iReq int,@hr int
Declare @sUrl as varchar(500)
DECLARE @errorSource VARCHAR(8000)
DECLARE @errorDescription VARCHAR(8000)

-- Create Object for XMLHTTP
EXEC @hr = sp_OACreate 'Microsoft.XMLHTTP', @iReq OUT

print @hr

if @hr <> 0
Raiserror('sp_OACreate Microsoft.XMLHTTP FAILED!', 16, 1)

set @sUrl='http://api.clickatell.com/http/sendmsg?user=devendar&password=csx19csx&api_id=3360313&to=#MobNo#&text=#Msg#'

set @sUrl=REPLACE(@sUrl,'#MobNo#',@MobileNo)
set @sUrl=REPLACE(@sUrl,'#Msg#',@smstext)

print @sUrl

-- sms code start
EXEC @hr = sp_OAMethod @iReq, 'Open', NULL, 'GET', @sUrl, true
print @hr

if @hr <> 0
Raiserror('sp_OAMethod Open FAILED!', 16, 1)

EXEC @hr = sp_OAMethod @iReq, 'send'
select @iReq
print @hr

if @hr <> 0
Begin
EXEC sp_OAGetErrorInfo @iReq, @errorSource OUTPUT, @errorDescription OUTPUT

SELECT [Error Source] = @errorSource, [Description] = @errorDescription

Raiserror('sp_OAMethod Send FAILED!', 16, 1)
end
else
Begin
EXEC @hr = sp_OAGetProperty @iReq,'responseText', @sResponse OUT
print @hr

insert into send_log (Id, mobile, sendtext, response, created, createddate)
values(0, @MobileNo, @smstext, @sResponse, 'System', GETDATE())
end
end

我在这里使用 clickatell 网关,请帮我解决这个问题

当我在 smslog 表中插入一行时,我无法发送 SMS

我在触发器中使用after insert 并将参数传递给存储过程来发送SMS。

请帮帮我

提前致谢开发商

最佳答案

ALTER TRIGGER [dbo].[mytrigger]    
ON [dbo].[smslog]
AFTER INSERT
AS
Declare @MobileNo int
Declare @smstext as varchar(300)
Begin
set @MobileNo = ( select mobile from inserted)
set @smstext = (select smstext from inserted)
set @sResponse = 'test'
Exec pr_SendSmsSQL @MobileNo, @smstext, @sResponse
END

程序

create procedure [dbo].[pr_SendSmsSQL] 
@MobileNo varchar(12),
@smstext as varchar(300),
@sResponse varchar(1000) OUT
as
BEGIN
Declare @iReq int,@hr int
Declare @sUrl as varchar(500)
DECLARE @errorSource VARCHAR(8000)
DECLARE @errorDescription VARCHAR(8000)

-- Create Object for XMLHTTP
EXEC @hr = sp_OACreate 'Microsoft.XMLHTTP', @iReq OUT

print @hr

if @hr <> 0
Raiserror('sp_OACreate Microsoft.XMLHTTP FAILED!', 16, 1)

**set @sUrl='http://api.clickatell.com/http/sendmsg?user=devendar&password=csx19csx&api_id=3360313&to=#MobNo#&text=#Msg#'**

**APi 无法正常工作 - 剩余的代码非常好,它以一种很好的方式工作**

   set @sUrl=REPLACE(@sUrl,'#MobNo#',@MobileNo) 
set @sUrl=REPLACE(@sUrl,'#Msg#',@smstext)

print @sUrl

-- sms code start
EXEC @hr = sp_OAMethod @iReq, 'Open', NULL, 'GET', @sUrl, true
print @hr

if @hr <> 0
Raiserror('sp_OAMethod Open FAILED!', 16, 1)

EXEC @hr = sp_OAMethod @iReq, 'send'
select @iReq
print @hr

if @hr <> 0
Begin
EXEC sp_OAGetErrorInfo @iReq, @errorSource OUTPUT, @errorDescription OUTPUT

SELECT [Error Source] = @errorSource, [Description] = @errorDescription

Raiserror('sp_OAMethod Send FAILED!', 16, 1)
end
else
Begin
EXEC @hr = sp_OAGetProperty @iReq,'responseText', @sResponse OUT
print @hr

insert into send_log (Id, mobile, sendtext, response, created, createddate)
values(0, @MobileNo, @smstext, @sResponse, 'System', GETDATE())
end
end

关于sql-server-2008 - 使用 SQL Server 2008 中的触发器和存储过程发送 SMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com