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

謝謝 但它沒有顯示任何錯誤。 –