【发布时间】:2017-03-18 08:03:51
【问题描述】:
我正在使用以下代码从 SQL Server 2014 发送短信。它曾经可以正常工作,但几天后它无法正常工作。我还在另一个 SQL Server 中执行了以下代码,它运行良好。但由于某种原因无法在 SQL Server 2014 中运行。我还尝试在浏览器中使用 url 发送槽,它也可以。但有些我不能通过存储过程工作。
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--EXEC sp_SendSmsSQL '*****','Today is Saturday',''
ALTER procedure [dbo].[sp_SendSmsSQL]
@MobileNo varchar(max),
@smstext as varchar(300),
@sResponse varchar(8000) 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
--EXEC @hr = sp_OACreate 'MSXML2.ServerXMLHTTP', @iReq OUT
print '*'
print 'hr : ' + cast(@hr as varchar)
if @hr <> 0
Raiserror('sp_OACreate Microsoft.XMLHTTP FAILED!', 16, 1)
set @sUrl='****'
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 '**'
print @hr
if @hr <> 0
Raiserror('sp_OAMethod Open FAILED!', 16, 1)
EXEC @hr = sp_OAMethod @iReq, 'Send'
select @iReq
print '***'
print 'hr : ' + cast(@hr as varchar)
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
print '****'
print @sresponse
end
EXEC @hr=sp_OADestroy @iReq
print @hr
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
END
关注结果
*
hr : 0
url ='******'
**
0
(1 row(s) affected)
***
hr : 0
-2147483638
****
0
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
【问题讨论】:
-
旁注:您应该不为您的存储过程使用
sp_前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用其他东西作为前缀 - 或者根本不使用前缀! -
感谢您的建议
标签: sql-server sql-server-2008 stored-procedures sms-gateway