Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear,

I have created a trigger for sending sms through SQL, But how to put httpget in SQL please help me on this.
Below is the trigger. Note that java script code working fine.

SQL
alter TRIGGER CallInserted
ON [dbo].[CustomerCall]
FOR INSERT
AS
BEGIN
    SET NOCOUNT ON;
 
    IF EXISTS (SELECT * FROM inserted)
    BEGIN
	//below is javascript code working fine, But i want to use in SQL
	function httpGet(theUrl)
	{
    	var xmlHttp = new XMLHttpRequest();
    	xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
    	xmlHttp.send( null );
    	return xmlHttp.responseText;
	}
	
    END
END
GO

Thanks
Basit.
Posted
Comments
Ben J. Boyle 19-Aug-15 10:43am    
You can't execute javascript in a stored procedure, but take a look at this stored proc making an HTTP Request, might be enough to get you going: http://www.geekzilla.co.uk/View24CF9B2B-4099-4ED7-B360-89B14C11DAE8.htm
Suket shah 20-Aug-15 2:03am    
HTTPRequest directly not possible from SQL
_Asif_ 20-Aug-15 8:46am    
why on earth you like to push SMS via SQL Server Triggers. This seems like a design flaw!

1 solution

The javascript code does not work in sql

You can send email via Sql like

Step 1) Create Profile and Account

You need to create a profile and account using the Configure Database Mail Wizard which can be accessed from the Configure Database Mail context menu of the Database Mail node in Management Node. This wizard is used to manage accounts, profiles, and Database Mail global settings

Step 2)

RUN:

sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO
Step 3)

USE msdb
GO
EXEC sp_send_dbmail @profile_name='yourprofilename',
@recipients='test@Example.com',
@subject='Test message',
@body='This is the body of the test message.
Congrates Database Mail Received By you Successfully.'
To loop through the table

DECLARE @email_id NVARCHAR(450),@id BIGINT,@max_id BIGINT,@query NVARCHAR(1000)

SELECT @id=MIN(id),@max_id=MAX(id) FROM [email_adresses]

WHILE @id<@max_id
BEGIN
SELECT @email_id=email_id
FROM [email_adresses]

set @query='sp_send_dbmail @profile_name=''yourprofilename'',
@recipients='''+@email_id+''',
@subject=''Test message'',
@body=''This is the body of the test message.
Congrates Database Mail Received By you Successfully.'''

EXEC @query
SELECT @id=MIN(id)FROM [email_adresses] where id>@id

END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900