Solved

VbScript to SQL

Posted on 2004-10-07
7
823 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

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

Author Comment

by:webago
Comment Utility
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
Comment Utility
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now