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

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

Related

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

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'

How to call Rest API in a SQL Server stored procedure

I am trying to call a Rest API call in sql. When I run this, I get a NULL in the ResponseText.
Note: The api-key, x-domain and host are not real in this example. Could it be the setRequestHeader are not set correctly?
DECLARE #Object AS INT;
DECLARE #ResponseText AS VARCHAR(400);
EXEC sp_OACreate 'MSXML2.ServerXMLHTTP', #Object OUT;
EXEC sp_OAMethod #Object, 'open', NULL, 'get', 'https://api3.website.com/v3/contacts', 'false'
EXEC sp_OAMethod #Object, 'setRequestHeader', NULL, 'X-Auth-Token','api-key as152das24wdw52dstw53fsg'
EXEC sp_OAMethod #Object, 'setRequestHeader', NULL, 'X-DOMAIN', 'em.website.com'
EXEC sp_OAMethod #Object, 'setRequestHeader', NULL, 'Host', 'api3.website.com'
Exec sp_OAMethod #Object, 'send'
Exec sp_OAMethod #Object, 'responseText', #ResponseText OUTPUT
SELECT #ResponseText
Exec sp_OADestroy #Object

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.

Saving files in Folder with a folder name as date

I would like to know how to save a pdf file in a folder which is created with the date as its name.
I have to save the pdf in the folder just created.
Here is my code as is:
DECLARE #SP VARBINARY(MAX),
#DT VARCHAR(MAX),
#OT INT,
#Rr varchar(30),
#PL varchar(100),
#fL varchar(100)
SET #fL = 'L' + REPLACE(CONVERT(varchar(10), GETDATE(), 101), '/', '')
set #PL = 'c:\'+ #fL
EXEC master.dbo.xp_create_subdir #PL
SET #DESTPATH = '#PL' + CAST(#Rr AS varchar) + '.pdf'
EXEC sp_OACreate 'ADODB.Stream', #OT OUTPUT
EXEC sp_OASetProperty #OT, 'Type', 1
EXEC sp_OAMethod #OT, 'Open'
EXEC sp_OAMethod #OT, 'Write', NULL, #SOURCEPATH
EXEC sp_OAMethod #OT, 'SaveToFile', NULL, #DESTPATH, 2
EXEC sp_OAMethod #OT, 'Close'
EXEC sp_OADestroy #OT

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