Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VbScript to SQL

Posted on 2004-10-07
7
Medium Priority
?
867 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 1000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

971 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