Calling a URL from a stored procedure in SQL Server?
Just wondering if its possible to call a URL from a stored procedure (eventually adding that procedure to a sql job) As the webpage refreshes my database, it would be excellent if I could automate this process.
I want to be able to request a webpage from a store procedure. On the page load of the desired webpage there is a function that refreshes my database. I want it to refresh my database at 4 am every day. In order for me not to manually go onto the site at 4am (still sleeping) I need something else to do it for me. I thought sql jobs would be excellent, as I can set the time, and the job up. I don’t know PowerShell all that well, and wanted to know if I could request a URL, or visit a url using a stored procedure or any other way.
3 Solutions collect form web for “Calling a URL from a stored procedure in SQL Server?”
You can do it with Task Scheduler (part of Windows). Just create a scheduled task that opens Internet Explorer and browses to the page:
"C:\Program Files\Internet Explorer\iexplore.exe" "http://yoursite.com/yourpage.aspx"
Or for 64-bit Windows:
"C:\Program Files (x86)\Internet Explorer\iexplore.exe" "http://yoursite.com/yourpage.aspx"
Alternatively, create a job using SQL Server Agent, and create a single step of type “Operating System (CmdExec)” with the above command.
I’m pretty sure MS SQL doesn’t allow you to do that directly.. obvious security issues. However, I think you can get around it by using xp_cmdshell to execute a vbscript file and in that file, create an xmlhttp request to a site.
EXEC master..xp_cmdshell 'c:\<file>.vbs',no_output
call main() sub main() Dim xmlHTTP, url Set xmlHTTP = WScript.CreateObject("Msxml2.XMLHTTP") url = "<url>" xmlHTTP.Open "GET", url, False xmlHTTP.Send "" end sub
In response to a comment on how to do this asynchronously.
EXEC master..xp_cmdshell 'c:\caller.vbs',no_output
VBScript for caller:
call main() sub main() Dim scmd Set scmd = "c:\windows\system32\cscript.exe //nologo c:\<originalVBS>.vbs" createobject("wscript.shell").run scmd,0,false end sub
@newurl is url you want to hit
@response is response you received
EXEC Sp_oacreate 'MSXML2.XMLHTTP',@obj OUT; EXEC Sp_oamethod @obj,'open',NULL,'get',@newurl,'false' EXEC Sp_oamethod @obj,'send' EXEC Sp_oamethod @obj,'responseText',@response OUTPUT EXEC Sp_oadestroy @obj