How to add a variable to a MSXML2.ServerXMLHTTP.6.0 URL in a SQL Server 2016 stored procedure - sql-server-2016

I'm following the example on this page
https://www.sqlservercentral.com/forums/topic/using-msxml2-serverxmlhttp-within-stored-procedure-to-grab-source-of-html-page-and-save-to-table
I need to add a variable to the url. I have tried id='+ #PMIDLIST ' and also id='& #PMIDLIST ' but both cause errors. I have also tried
id=pmidList but that just does not return a valid response. I have also tried double quotes but that did not work.
These are the errors I got:
Incorrect syntax near '&'.
Incorrect syntax near '+'.
Code:
DECLARE #Object AS INT;
DECLARE #hr INT
DECLARE #json AS TABLE (Json_Table NVARCHAR(MAX))
DECLARE #pmidList NVARCHAR(MAX)
SET #PMIDLIST = '17784783,19505939,30166592'
EXEC #hr = sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', #Object OUT;
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr = sp_OAMethod #Object, 'open', NULL, 'get', 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?
db=pubmed&retmode=json&id='+ #PMIDLIST ' ;out;',
'false'
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr = sp_OAMethod #Object, 'send'
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
EXEC #hr=sp_OAMethod #Object, 'responseText', #json OUTPUT
IF #hr <> 0
EXEC sp_OAGetErrorInfo #Object
INSERT INTO #json (Json_Table)
EXEC sp_OAGetProperty #Object, 'responseText'
-- select the JSON string
SELECT * FROM #json

I figured out the answer I needed
Declare #Object as Int;
DECLARE #hr int
Declare #json as table(Json_Table nvarchar(max))
DECLARE #pmidList NVARCHAR(max)
SET #PMIDLIST = '17784783,19505939,30166592'
declare #url NVARCHAR(max)
SET #url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&retmode=json&id='+ #pmidList
Exec #hr=sp_OACreate 'MSXML2.ServerXMLHTTP.6.0', #Object OUT;
IF #hr <> 0 EXEC sp_OAGetErrorInfo #Object
Exec #hr=sp_OAMethod #Object, 'open', NULL, 'get',
#url OUT,
'false'

Related

how to get and store data from 3rd party xml web service into sql table

Is there any way to get and store 3rd party Web Service XML data directly into SQL Server DB?
I know there is a lot of other easy solutions like Rest or WCF but in my case I want to add new job to SQL that will trigger every day and get the data from the Web Service.
To connect to some resources and get XML response you can use this code:
DECLARE #URI nvarchar(max) = 'http://example.com/',
#methodName nvarchar(50) ='GET',
#objectID int,
#setTimeouts nvarchar(255) = 'setTimeouts(9000,90000,900000,9000000)'
EXEC sp_OACreate 'MSXML2.XMLHTTP', #ObjectID OUT
EXEC sp_OAMethod #objectID, 'open', null, #methodName, #URI, 'false'
EXEC sp_OAMethod #objectID, #setTimeouts
EXEC sp_OAMethod #objectID, 'send', null
INSERT INTO SomeTable (xmlResponse)
EXEC sp_OAGetProperty #objectID, 'responseText'
EXEC sp_OAGetErrorInfo #objectID
EXEC sp_OADestroy #objectID
You can comment #setTimeouts part, it is needed if response time may be big.
Put this in stored proc. Pass #URI as parameter and this will write response in SomeTable. Then you can put EXEC YourStoredProv #URI into job and schedule each day launch.
EDIT
With authorization:
DECLARE #URI nvarchar(max) = 'http://example.com/',
#auth nvarchar(max),
#methodName nvarchar(50),
#objectID int,
#setTimeouts nvarchar(255) = 'setTimeouts(9000,90000,900000,9000000)'
EXEC sp_OACreate 'MSXML2.XMLHTTP', #ObjectID OUT
SELECT #auth= #URI + 'AppEngine_2/api_login?user_name=myuserName&password=myPassword%20&TOKEN=chinaurl&CLIENT_TYPE=1%22',
#methodName='POST'
EXEC sp_OAMethod #objectID, 'open', null, #methodName, #auth, 'false'
EXEC sp_OAMethod #objectID, 'send', null
SELECT #methodName='GET'
EXEC sp_OAMethod #objectID, 'open', null, #methodName, #URI, 'false'
EXEC sp_OAMethod #objectID, #setTimeouts
EXEC sp_OAMethod #objectID, 'send', null
INSERT INTO SomeTable (xmlResponse)
EXEC sp_OAGetProperty #objectID, 'responseText'
EXEC sp_OAGetErrorInfo #objectID
EXEC sp_OADestroy #objectID
You can pass myuserName, myPassword and TOKEN as variables as URL to get response.

how to Call web service using stored procedure which returns json and store data in table using sql server?

how to Call web service using stored procedure which returns json and store data in table using sql server?
==============
Declare #Object as Int;
Declare #ResponseText as Varchar(8000);
--Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', #Object OUT;
Exec sp_OAMethod #Object, 'open', NULL, 'get',
'WEB SERVICE LINK', --Your Web Service Url (invoked)
'false'
Exec sp_OAMethod #Object, 'send'
Exec sp_OAGetProperty#Object, 'responseText', #ResponseText OUTPUT
Select #ResponseText
Exec sp_OADestroy #Object
DECLARE #Object AS INT ;
DECLARE #ResponseText AS VARCHAR(8000) ;
DECLARE #Url AS VARCHAR(1000) ;
SET #Url = 'Web service'
EXEC sp_OACreate 'MSXML2.XMLHTTP', #Object OUT ; --
EXEC sp_OAMethod #Object, 'open', NULL, 'get', #Url,'false'
EXEC sp_OAMethod #Object, 'send'
EXEC sp_OAMethod #Object, 'responseText', #ResponseText OUTPUT
SELECT #ResponseText
EXEC sp_OADestroy #Object
DECLARE #handle INT
DECLARE #PrepareXmlStatus INT
EXEC #PrepareXmlStatus= sp_xml_preparedocument #handle OUTPUT,
#ResponseText

sql server - Google maps api returns too much data

Im using sql server to call out to the google maps api and get data for a gps location. It worked fine but lately my stored procedure that calls out fails with this error
0x8004271F
ODSOLE Extended Procedure
The source data specified for this string or binary column or parameter is too long.
It seems that the character limit is being reached when google returns a "boat load" of location data.
This is what Im calling out with so far:
-- Select dbo.fnGetAddressFromGPS('33.83528299,-118.28754585')
-- -- GO
Declare #GPS VarChar(50)
Set #GPS = '34.55,-118.50'
DECLARE #URL VarChar(4000)
DECLARE #win int
DECLARE #hr int
DECLARE #text varchar(8000)
Set #URL = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=' + #GPS + '&sensor=false'
Set #URL = 'http://maps.googleapis.com/maps/api/geocode/xml?latlng=34.5566767,-118.50677688&sensor=false'
Set #URL = 'http://maps.googleapis.com/maps/api/geocode/json? latlng=34.5566767,-118.50677688&sensor=false'
EXEC #hr=sp_OACreate 'WinHttp.WinHttpRequest.5.1',#win OUT
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr=sp_OAMethod #win, 'Open',NULL,'GET',#url,'false'
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr=sp_OAMethod #win,'Send'
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr=sp_OAGetProperty #win, 'ResponseText',#text OUTPUT
IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #hr=sp_OADestroy #win
--IF #hr <> 0 EXEC sp_OAGetErrorInfo #win
Select #Text As 'GoogleResult'
Return
IF CHARINDEX('ZERO_RESULTS', #text) > 0
Select 'No Address Found'
Declare #i Int
Declare #j Int
Declare #Address VarChar(150)
SELECT #i = CHARINDEX('<formatted_address>', #Text);
SELECT #j = CHARINDEX('</formatted_address>', #Text);
SELECT #i = CHARINDEX('"formatted_address" : "', #Text);
Select #i
Return
If #j-#i-Len('<formatted_address>') <= 0
Select #Address = 'N/A'
Else
Select #Address = SubString(#text, #i+Len('<formatted_address>'), #j-#i- Len('<formatted_address>'))
--Select #Address
Select #Address
What I would like to do is limit the returning data from google to only the formatted address portion which is all I really need but I havent found anywhere in the documenation on how to do such a thing. The only other thing I can think of doing is truncating the result data but I cant figure that out either at the moment. Any help would be much appreciated.
The interesting part here is inserting into a table:
DECLARE #resTbl AS TABLE (result NVARCHAR(MAX))
INSERT #resTbl EXEC #HttpWinRequest=sp_OAGetProperty #win, 'ResponseText'
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE dbo.usp_http_get
/*
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
*/
#url VARCHAR(8000)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #win int, #HttpWinRequest int
DECLARE #resTbl AS TABLE (result NVARCHAR(MAX))
EXEC #HttpWinRequest=sp_OACreate 'WinHttp.WinHttpRequest.5.1',#win OUT
IF #HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #HttpWinRequest=sp_OAMethod #win, 'Open',NULL,'GET',#url,'false'
IF #HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo #win EXEC #HttpWinRequest=sp_OAMethod #win,'Send'
IF #HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo #win
INSERT #resTbl EXEC #HttpWinRequest=sp_OAGetProperty #win, 'ResponseText'
IF #HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo #win
EXEC #HttpWinRequest=sp_OADestroy #win
IF #HttpWinRequest <> 0 EXEC sp_OAGetErrorInfo #win
SELECT * FROM #resTbl
END
GO
EXEC usp_http_get #url = 'http://www.yahoo.com' -- varchar(8000)

How to attach a file to an email using SQL stored procedure?

Can't seem to find much information about how to attach a file that's been stored as a BLOB as part of an email.
I know you can attach files from the file system (C:\temp...) to email that are being setup in DBMAIL or custom stored procedures. However, I haven't seen any references to attaching something such as a PDF that's been stored as a binary object in a table.
I see where you can attach a query as a file, but I don't think that's what I'm looking for.
We would do this programatically via the application, but we need to be able to kick this SP via triggers or the application's server side code.
Is this possible or should i be asking other questions, since usually a binary object also needs to have a content type associated with it for browsers or mail objects to know how to handle?
The solution can not attach binary object stored in db field, you may change your schema a little bit store the path to the binary file. if you could enable .net clr in your database server, you will have more options.
use master
go
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[usp_send_cdosysmail]')
and objectproperty(id, N'isprocedure') = 1)
drop procedure [dbo].[usp_send_cdosysmail]
go
create procedure usp_send_cdosysmail
#from varchar(500) ,
#to varchar(500) ,
#subject varchar(500),
#body nvarchar(max) ,
#smtpserver varchar(25),
#bodytype varchar(10) ,
#attachment varchar(100)= ' '
as
declare #imsg int
declare #hr int
declare #source varchar(255)
declare #description varchar(500)
declare #output varchar(1000)
exec #hr = sp_oacreate 'cdo.message', #imsg out
exec #hr = sp_oasetproperty #imsg,
'configuration.fields("http://schemas.microsoft.com/cdo/configuration/sendusing").value','2'
exec #hr = sp_oasetproperty #imsg, 'configuration.fields("http://schemas.microsoft.com/cdo/configuration/smtpserver").value', #smtpserver
exec #hr = sp_oamethod #imsg, 'configuration.fields.update', null
exec #hr = sp_oasetproperty #imsg, 'to', #to
exec #hr = sp_oasetproperty #imsg, 'from', #from
exec #hr = sp_oasetproperty #imsg, 'subject', #subject
-- if you are using html e-mail, use 'htmlbody' instead of 'textbody'.
exec #hr = sp_oasetproperty #imsg, #bodytype, #body
-- Attachments...
IF #attachment IS NOT NULL AND LEN(#attachment) > 0 BEGIN
Declare #files table(fileid int identity(1,1),[file] varchar(255))
Declare #file varchar(255)
Declare #filecount int ; set #filecount=0
Declare #counter int ; set #counter = 1
DECLARE #outVar INT
SET #outVar = NULL
INSERT #files SELECT cValue FROM master..fn_split(#attachment,',')
SELECT #filecount=##ROWCOUNT
WHILE #counter<(#filecount+1)
BEGIN
SELECT #file = [file]
FROM #files
WHERE fileid=#counter
EXEC #hr = sp_OAMethod #imsg, 'AddAttachment',#outVar OUT, #file
SET #counter=#counter+1
END
END
exec #hr = sp_oamethod #imsg, 'send', null
-- sample error handling.
if #hr <>0
select #hr
begin
exec #hr = sp_oageterrorinfo null, #source out, #description out
if #hr = 0
begin
select #output = ' source: ' + #source
print #output
select #output = ' description: ' + #description
print #output
end
else
begin
print ' sp_oageterrorinfo failed.'
return
end
end
exec #hr = sp_oadestroy #imsg
go
set quoted_identifier off
go
set ansi_nulls on
go
sp_configure 'Ole Automation Procedures', 1
RECONFIGURE
GO

SQL Server 2008 alternative for SQL-DMO

I previously was using SQL-DMO to automatically generate scripts from the database.
Now I upgraded to SQL Server 2008 and I don’t want to use this feature anymore since Microsoft will be dropping this feature off.
Is there any other alternative I can use to connect to a server and generate scripts automatically from a database?
Any answer is welcome. Thanks in advance.
This is the procedure i was previously using:
CREATE PROC GenerateSP (
#server varchar(30) = null,
#uname varchar(30) = null,
#pwd varchar(30) = null,
#dbname varchar(30) = null,
#filename varchar(200) = 'c:\script.sql'
)
AS
DECLARE #object int
DECLARE #hr int
DECLARE #return varchar(200)
DECLARE #exec_str varchar(2000)
DECLARE #spname sysname
SET NOCOUNT ON
-- Sets the server to the local server
IF #server is NULL
SELECT #server = ##servername
-- Sets the database to the current database
IF #dbname is NULL
SELECT #dbname = db_name()
-- Sets the username to the current user name
IF #uname is NULL
SELECT #uname = SYSTEM_USER
-- Create an object that points to the SQL Server
EXEC #hr = sp_OACreate 'SQLDMO.SQLServer', #object OUT
IF #hr <> 0
BEGIN
PRINT 'error create SQLOLE.SQLServer'
RETURN
END
-- Connect to the SQL Server
IF #pwd is NULL
BEGIN
EXEC #hr = sp_OAMethod #object, 'Connect', NULL, #server, #uname
IF #hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
ELSE
BEGIN
EXEC #hr = sp_OAMethod #object, 'Connect', NULL, #server, #uname, #pwd
IF #hr <> 0
BEGIN
PRINT 'error Connect'
RETURN
END
END
--Verify the connection
EXEC #hr = sp_OAMethod #object, 'VerifyConnection', #return OUT
IF #hr <> 0
BEGIN
PRINT 'error VerifyConnection'
RETURN
END
SET #exec_str = 'DECLARE script_cursor CURSOR FOR SELECT name FROM ' + #dbname + '..sysobjects WHERE type = ''P'' ORDER BY Name'
EXEC (#exec_str)
OPEN script_cursor
FETCH NEXT FROM script_cursor INTO #spname
WHILE (##fetch_status <> -1)
BEGIN
SET #exec_str = 'Databases("'+ #dbname +'").StoredProcedures("'+RTRIM(UPPER(#spname))+'").Script(74077,"'+ #filename +'")'
EXEC #hr = sp_OAMethod #object, #exec_str, #return OUT
IF #hr <> 0
BEGIN
PRINT 'error Script'
RETURN
END
FETCH NEXT FROM script_cursor INTO #spname
END
CLOSE script_cursor
DEALLOCATE script_cursor
-- Destroy the object
EXEC #hr = sp_OADestroy #object
IF #hr <> 0
BEGIN
PRINT 'error destroy object'
RETURN
END
GO
Download the SQLServer2005_BC.msi from
http://www.microsoft.com/en-us/download/details.aspx?id=24793
then install DMO, also works with 2008/R2!
Are you aware that DMO has been replaced with SMO? Microsoft has no plans to discontinue this, that I'm aware of.
I would suggest you try SQL CRL Stored Procedure. See http://www.sqlteam.com/article/writing-clr-stored-procedures-in-charp-introduction-to-charp-part-1