Solved

VbScript to SQL

Posted on 2004-10-07
7
826 Views
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 = http://www.mygolfgear.com/products/iStourplusequilizer.jpg 
 sDest = C:\temp\iStourplusequilizer.jpg


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

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

</vb script>


0
Comment
Question by:webago
  • 4
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
boblah earned 500 total points
ID: 12253980
Hi webago,

vb example:

oHTTP.open "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
    BEGIN
        EXEC sp_OAGetErrorInfo @oNewMail, NULL, @SA OUTPUT
        RAISERROR(@SA, 16, 1)
        GOTO ErrorHandler
    END



Cheers!
0
 
LVL 6

Expert Comment

by:boblah
ID: 12253998
webago,

For clarity, the var declarations for this are:

DECLARE @hr INT
DECLARE @ReturnValue VARCHAR(8000)
DECLARE @Source VARCHAR(255)

SELECT @Source = "http://www.mygolfgear.com/products/iStourplusequilizer.jpg"
0
 

Author Comment

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

0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 6

Expert Comment

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

Author Comment

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

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'http://www.microsoft.com'
set @trg =N'C:\temp\a.html'

exec @RESULT = SP_OACreate 'Microsoft.XMLHTTP', @ohttp out
  if @result = 0
     begin
          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
               begin
                    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'                                  
                    else
                         print 'Stream Write           : Error'                                                      
                    exec @RESULT = sp_Oamethod @ostream,'SaveToFile',@trg,@adSaveCreateOverWrite
                    if @result = 0
                         print 'Stream Saved to File     : Ok  File : '+     @trg
                    else
                            print 'Stream Saved to File     :   Error : '+     str(@result,15)

               end

          EXEC sp_OADestroy @ostream
          EXEC sp_OADestroy @ohttp
          end

go
0
 
LVL 6

Expert Comment

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

First

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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsproarguments.asp
)), 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.

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

919 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now