Thursday, April 12, 2012

Send SMS using Stored Procedure in Sql Server


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=xxxxxxxxx&api_id=xxxxxxxx&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())endend



Using the Above Stored Procedure and with SMS Gateway API  - simply u can send sms to anybody - its a simple task

Just pass the parameters to Stored Procedure for which number you want to send sms. thats it.

i tested this using Sql Server 2008 with clickatell sms gateway.

Thanks friends This is my first POst

17 comments:

  1. when i execute sp i got he following error
    can u pl solve this erreor

    SQL Server blocked access to procedure 'sys.sp_OAMethod' of component 'Ole Automation Procedures' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

    ReplyDelete
  2. @harshal

    enable those Stored Procedures in sql server

    ReplyDelete
  3. I know, it's late but still wish to make point clear:

    Below T-SQL need to be run in order to ensure that all those Stored Procedures are running.

    sp_configure 'Ole Automation Procedures' , 1
    RECONFIGURE

    ReplyDelete
  4. 'http://api.clickatell.com/http/sendmsg?user=devendar&password=xxxxxxxxx&api_id=xxxxxxxx&to=#MobNo#&text=#Msg#'
    could u please explain me where to get user,password,api_id and all. user=devendar ? password=xxxx ? api_id =xxx .? Here mentioned as xxx.
    It will be really helpful for me to proceed further

    ReplyDelete
  5. ERR: 114, Cannot route message is showing as error. can u please clarify

    ReplyDelete
  6. http://stackoverflow.com/questions/10088230/send-sms-using-triggers-and-stored-procedures-from-sql-server-2008

    check the above url .. this is also solved by me

    ReplyDelete
  7. Hi ,
    sorry to ask question again.when i try this in separate tab in browser, sms is going, whereas, inside the sp, it is not working. can u help me to solve this issue

    ReplyDelete
  8. hii
    i am new in sql so plz tell me how to run this procedure in sql

    ReplyDelete
  9. After 2 sms , againi have to restart the sql services again.
    What's the problem , it will work only for 2 sms only.
    It required to start sql service again after 2 sms why ?
    - Ravi chaudhari - chaudhariravi@hotmail.com

    ReplyDelete
    Replies
    1. Any Solution for After 2 sms , againi have to restart the sql services again.
      What's the problem , it will work only for 2 sms only.
      It required to start sql service again after 2 sms why ?

      Delete
  10. Process executing successfully.. but sms not sending

    my url is === set @sUrl='https://powersms.banglaphone.net.bd/httpapi/sendsms?userId=XXXXX&password=**********&smsText=#Msg#&commaSeperatedReceiverNumbers=#MobNo#'

    if i paste and execute in browser then sms sends successfully,
    but not sending from SQL Server 2008 r2


    NB : i have executed the following code first

    USE [ISTCLAPSDB]
    GO
    sp_configure 'show advanced options', 1
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ole Automation Procedures', 1
    GO
    RECONFIGURE;
    GO
    sp_configure 'show advanced options', 1
    GO
    RECONFIGURE;



    Can anybody help me about the problem.

    ReplyDelete
    Replies
    1. hi your cUSE [ISTCLAPSDB]
      GO
      sp_configure 'show advanced options', 1
      GO
      RECONFIGURE;
      GO
      sp_configure 'Ole Automation Procedures', 1
      GO
      RECONFIGURE;
      GO
      sp_configure 'show advanced options', 1
      GO
      RECONFIGURE;
      omment

      Delete
  11. Message Submitted
    Credits Consumed: 2
    Balance Credits: 98
    Mobile=919822012345 MsgId=1177855328
    Mobile=919370119223 MsgId=1177855329

    how will i get output like this .....plz help me
    i want this output to check wether msg gone or not

    ReplyDelete
  12. The stored procedure only executes twice and afer that, doesn't send the parameters to the third party. Please, i need a olution

    ReplyDelete