Solved

VbScript to SQL

Posted on 2004-10-07
7
845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

623 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