How to read XML file from URL in SQL Server 2008 - sql

I would like to read an xml and get a value from it but the xml file is returned via the following url:
http://webserver/XMLResult
I try To read the xml file, I used the folowing code :
INSERT INTO T(XmlCol)
SELECT * FROM OPENROWSET(BULK ‘http://webserver/XMLResult’,SINGLE_BLOB) as c
but I am getting the following error:
Cannot bulk load because the file… could not be opened. Operating
system error code 123(error not found).
Do you know how to fix this issue?
If it is not possible, can someone recommend the best method which is help me reed XML from URL in sql server
thanks

Apparently, the data-file parameter of the OPENROWSET function can't point to an URL. I would suggest to read the XML data outside of the TSQL block, possibly passing the data as a parameter to a stored procedure.
If this is not possible and you must read the XML data from within TSQL, then two options come to my mind, although I am not a big fan of neither of them:
use a CLR function and read your XML data from there;
try to exploit the COM objects available to SQL Server machine by way of the sp_OACreate system stored procedure, as suggested here.

Related

What is the best way to get a XML as output from a SQL Server procedure in Delphi?

I need to read in Delphi the output from a SQL Server procedure that returns a XML as output (#MSG1)
PROCEDURE dbo.PROC_ZUND_XML
(#LOTEPC VARCHAR(10),
#MSG1 NVARCHAR(MAX) OUTPUT)
I've tried to get this output using a TFDStoredProc component as well as a TFDQuery, however, it doesn't matter the variable type I use in the parameters configuration in Delphi (ftWideString, ftWideMemo etc.) it always store just the first 8000 characters of the output. A count statement in the SQL Command section of the Delphi's FireDAC Query Editor shows that the output of the procedure have more than 8000 characters.
So, the problem seems to be occurring internally, when the value is assigned to the variable. Does anybody have an idea about how to fix it? Maybe a different approach to the problem... Thanks in advance.
I'm using SQL Server 2008 and Delphi 10.2
If you need to export this xml file to a specific folder, I advise you to use SQL SERVER itself to export this file to a SHARED NETWORK FOLDER.
With BCP, you can generate your XML, and save directly to an XML file, in the directory you want ..
Some useful tips:     
If necessary, I recommend using the -w argument to avoid confusion
with Wrong CHARACTERS (À, É, È, Ó, Ò)
Useful Documentations:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=171529
https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ms162802(v=sql.110)
https://sqlsouth.wordpress.com/2014/05/23/export-xml-from-sql-server-using-bcp/

Is there any way to get more information about this error?

I am using the following post to try and get the results of a query printed out to a file: Save a SQL query result to an XML file without SSIS or xp_cmdshell
But when I run it I get an error saying:
Msg 50000, Level 16, State 1, Procedure WriteToFile, Line 42
Error whilst Creating file "C:\TEMP\SQL_XML\test.xml",
I was wondering if there is any way to get more information about why exactly it errrored? I dont know much about debugging SQL queries. Im using SQL Server 2008 with SQL Server Management Studio. unfortunately
If you convert HRESULT -2146828218 to HEX you get 800A0046 and if you google for 0x800A0046 you'll find
CTL_E_PERMISSIONDENIED
Most plausible is that the identity running the COM Scripting.FileSystem object doesn't have permissions to write in C:\TEMP\SQL_XML. So verify that the Sql Service user has permission to create files in C:\TEMP\SQL_XML.
A second option might be that the file already exists.
A third option might be that the file is already in use.

How to invoke stored procs with parameter using T-SQL scripting?

We are using edge-sql to execute T-SQL scripts and also stored procedures via C#. I noticed recently that stored proc support has been added and I'm trying to execute would would be:
exec dbo.sgRouteExportDelete #TripDate='2014-05-06', #RouteId = 1234, #Action='DELETE', #Comment='xxxxxx';
in SQL Server Management Studio, using edge-sql 0.1.2.
I've played around with several variations, but I get one of 2 error messages. Either cannot find stored procedure '' or "cannot find stored procedure 'sgRouteExportDelete #TripDate='2014-05-06', #RouteId = 1234, #Action='DELETE', #Comment='xxxxxx'" The stored proc executes just fine in edge.sql when invoked via C# method.
I did some additional experimentation and found I can execute a stored proc with no parameters: exec dbo.sgVersionGet, but any stored procs with parameters return errors.
So what am I doing wrong? And how could I invoke with parameter values that aren't hard-coded, as above? Both SQL Server and edge use the # character for parsing params.
Any help appreciated ...
-BillyB
In SQL Server when you are referring to a database object without the object full path ([database.schema].object), SQL Server will try to locate the object using the Default/Initial Catalog value which points to the default database if that was not specified within the connection string then chances are that when you try running your statement SQL Server won't be able to find the object throwing the "Cannot find XX" error, You should either specify an initial catalog on your connection string or execute your procedures using the full path, database.schema.procedure E.g. mydatabase.dbo.sp_customerCleanUp. On the other hand there is an internal procedure sp_executesql that you can use to run your procedures without having to hard code the parameters, all you need to do is build a string concatenating the hard coded part of the string (the procedure name) and whatever number of variables you are passing as parameters, see example ( https://technet.microsoft.com/en-us/library/ms175170%28v=sql.105%29.aspx )
The variables are assumed when calling a stored procedure with edge-sql. Any parameter you would preface with an '#' symbol will need to have the same name within the stored procedure.

What is the simplest way to convert table Rows into XML and viceversa

I need to develop a sql server procedure which will convert table rows into xml and will transfer it to another stored procedure(Different server) through link server. On that server I have to take this xml data as input and again convert it into table rows. What will be the simplest and time efficient way to do this.
It is not possible to use the XML datatype as a parameter in stored procedure on a linked server. You have to use nvarchar(max) and convert to XML in the stored procedure.
To create the XML you should use FOR XML (SQL Server). RAW and AUTO is easy and if you need more control you could use PATH. I would stay away from EXPLICIT.
To shred the XML on the receiving side you should use nodes() Method (xml Data Type) and value() Method (xml Data Type).
Given that you are using SQL Server, you need only append FOR XML to your query.

SQL Server 2008 Query Result to XML FIle

I'm having some trouble dumping a query result into a XML file. I've read many articles about this subject but i still can't reach a decent solution for my case.
I have a procedure that outputs a query in XML format and it's working well:
alter proc pr_export_xml
as
declare #XmlOutput xml
set #XmlOutput = (select id,ref,sit from RepData for XML PATH('Produto'), ROOT('Produtos'), TYPE)
select #XmlOutput
go
What I need now is to get that result and dump into a file for later use. This procedure will be called during a trigger, so the dump into file must be done automatically.
I hope I was clear enough, any help will be appreciated.
Another alternative for this one is bcp utility.
Look into these.
http://www.brighthub.com/internet/web-development/articles/119542.aspx
Enable 'xp_cmdshell' SQL Server