VbScript to SQL

Posted on 2004-10-07
Last Modified: 2008-01-09
I have a ASP/VB Script that I need converted to SQL for use in a Stored Procedure. Basically the script uses the http object to download a file.

The SP_OACreate procedure will need to be used to create the object.  I have tried, but can't seem to figure out the arguments when running SP_OAMethod.  There may also be another way of doing this...who knows?  Hopefully you!

<VB script>

 sSource = 
 sDest = C:\temp\iStourplusequilizer.jpg

 set oHTTP = Server.CreateObject("Microsoft.XMLHTTP") "GET", sSource, False

 set oStream = server.createobject("")
 Const adTypeBinary = 1
 Const adSaveCreateNotExist = 1
 Const adSaveCreateOverWrite = 2
 oStream.type = adTypeBinary
 oStream.write oHTTP.responseBody
 oStream.savetofile sDest, adSaveCreateOverWrite
set oStream = nothing
set oHTTP = nothing

</vb script>

Question by:webago
  • 4
  • 2

Accepted Solution

boblah earned 500 total points
ID: 12253980
Hi webago,

vb example: "GET", sSource, False

SP_OAMethod equivalent (ignoring the creation of the object and assigning reference to @oHTTP etc, and including error handling)

    EXEC @hr = sp_OAMethod @oHTTP, 'open', @ReturnValue OUTPUT, "GET", @Source, 0
    IF @hr <> 0
        EXEC sp_OAGetErrorInfo @oNewMail, NULL, @SA OUTPUT
        RAISERROR(@SA, 16, 1)
        GOTO ErrorHandler


Expert Comment

ID: 12253998

For clarity, the var declarations for this are:

DECLARE @ReturnValue VARCHAR(8000)

SELECT @Source = ""

Author Comment

ID: 12254550
Still having problem getting this to work:
set oStream = server.createobject("")
 Const adTypeBinary = 1
 Const adSaveCreateNotExist = 1
 Const adSaveCreateOverWrite = 2
 oStream.type = adTypeBinary
 oStream.write oHTTP.responseBody
 oStream.savetofile sDest, adSaveCreateOverWrite

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.


Expert Comment

ID: 12257739
show me the t-sql you have converted it to.

Author Comment

ID: 12259771
create Proc dbo._DownloadHTTP(
           @src varchar(400),
           @trg varchar(400)

declare @ohttp int
declare @ostream int
declare @ResponseBody binary
declare @result int

declare @adTypeBinary int
declare @adSaveCreateNotExist int
declare @adSaveCreateOverWrite int

set @adTypeBinary = 1
set @adSaveCreateNotExist = 1
set @adSaveCreateOverWrite = 2

set @src =N''
set @trg =N'C:\temp\a.html'

exec @RESULT = SP_OACreate 'Microsoft.XMLHTTP', @ohttp out
  if @result = 0
          Print 'Http Object           : Created'
          exec @RESULT = sp_Oamethod @ohttp, 'OPEN',null, 'GET',@src,False
          if @result = 0 print 'Http Open           : Ok  Addr : ' + @src
          exec @RESULT = sp_Oamethod @ohttp, 'SEND'
          if @result = 0 print 'Http Send           : Ok'
          exec @RESULT = sp_Oamethod @ohttp,'RESPONSETEXT',@ResponseBody out
          if @result = 0 print 'Http Response           : Ok'    

          exec @RESULT = SP_OACreate 'AdoDb.Stream', @ostream out
          if @Result = 0
                    Print 'Stream Object           : Created'
                    exec @RESULT = sp_OaSetProperty @ostream, 'TYPE',@adTypebinary
                    if @result = 0 print 'Stream Type Set          : Ok  Type : ' + str(@adTypebinary,1)
                    exec @RESULT = sp_Oamethod @ostream, 'OPEN'
                    if @result = 0 print 'Stream Open           : Ok'
                    exec @RESULT = sp_Oamethod @ostream,'write',@ResponseBody
                    if @result = 0
                         print 'Stream Write           : Ok'                                  
                         print 'Stream Write           : Error'                                                      
                    exec @RESULT = sp_Oamethod @ostream,'SaveToFile',@trg,@adSaveCreateOverWrite
                    if @result = 0
                         print 'Stream Saved to File     : Ok  File : '+     @trg
                            print 'Stream Saved to File     :   Error : '+     str(@result,15)


          EXEC sp_OADestroy @ostream
          EXEC sp_OADestroy @ohttp


Expert Comment

ID: 12266541
There are several problems with that code, but fixing all of them doesn't make it work...


you've switched the responseBody to responseText, but left the variable as an INT. However, switching it to  textual datatype still causes problems:

However, you are limited to 8000 characters into the @responsebody - most web pages will be larger than this, and when they are, the value is returned as null.

Correcting all this, and testing with a page that returns fewer than 800 characters, there is still a problem with the stream object. I have tried playing with permissions (need to make sure the account that SQL server runs under has write permissions to c:\temp) but still get errors on the write and saveToFile.

Switching back to responseBody and passing the reference in an int doesn't appear to work - the @ResponseBody doesn't get populated.

The best way to resolve all this would be to write a simple com component (maybe in vb) that handles the xmlhttp and the stream objects.

Alternatively, a quicker and dirtier answer, you could convert your vbs script to use commandline parameters (access commanline parameters using the WScript.Arguments object (see
)), and call it using

EXEC master..xp_cmdshell

Also, minor one:

          exec @RESULT = sp_Oamethod @ohttp,'RESPONSETEXT',@ResponseBody out

would be more correct as a sp_OAGetProperty, but the sp_OAMethod does work.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question