0

我正在使用以下代码从SQL Server 2014发送短信。它用于正常工作,但是有几天它不工作。我还在另一个SQL Server中执行了下面的代码,它工作得很好。但由于某种原因,不能在SQL Server 2014中工作。我还尝试在浏览器中使用url发送低谷,并且它也起作用。但一些如何我不通过存储过程工作。从SQL Server 2014 Issu Identification发送SMS Issu Identification

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. 
+0

备注:您应该**不要**为存储过程使用'sp_'前缀。微软已经保留了这个前缀以供自己使用(参见*命名存储过程*)](http://msdn.microsoft.com/en-us/library/ms190669%28v=sql.105%29.aspx),以及你将来有可能冒着名字冲突的风险。 [这对你的存储过程性能也是不利的](http://www.sqlperformance.com/2012/10/t-sql-queries/sp_prefix)。最好只是简单地避免使用'sp_'并将其他内容用作前缀 - 或者根本没有前缀! –

+0

谢谢您的建议 –

回答

0

你没有提供的SQL服务器返回的错误信息,但是这肯定听起来就像是一个安全问题。 DBCC FREEPROCCACHE需要更改服务器状态权限。此外,sp_oa ...方法需要启用,并且用户必须有权运行它们(SQL Server blocked access to procedure 'sys.sp_OACreate' of component 'Ole Automation Procedures')。最后,我不确定为什么你要在存储过程中运行DBCC命令。 FREEPROCCACHE有一些限制,但您可以使用选项(https://msdn.microsoft.com/en-us/library/ms174283.aspx)来调用它。 DROPCLEANBUFFERS实际上是为了测试查询性能。如果您的DBCC命令仅用于测试,请将其从存储过程中移除,并在调用存储过程之后立即将其放入。

+0

谢谢 但它没有显示任何错误。 –