SQL Server 2008 Query Result to XML FIle - sql

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

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/

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.

How to read XML file from URL in SQL Server 2008

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.

Create text file in oracle using sql

I need to grab data from some oracle database tables and format it into a fixed width text file.
I want to know if its possible to create a text file using sql.
I looked at some stuff and found the bc and xp_cmdshell but they are a bit confusing.
I am pretty new to sql and oracle databases.
Is this possible and how can I begin?
I don't need to open a file or check for existing file, overwriting is fine, what ever makes it easiest.
I don't need anything big or complex, a simple script to grab values and create a text file.
Just an update:
I don't think bcp works in the toad for oracle editor.
I found this tutorial here: http://www.sqlteam.com/article/exporting-data-programatically-with-bcp-and-xp_cmdshell
but the first bcp command does not compile, it says invalid sql query
If you are using the SQL*Plus client, you can spool to an output file. Here is a sample SQL*Plus file:
set serveroutput on size 1000000
set linesize 150
spool C:\path_to_file\filename.extension
-- Your SQL statement
select columns
from table
where somecondtion;
-- Your next SQL Statement
select ...
from ...;
spool off
I think you can use sqlplus command line tool todo this. See oracle manual for formatting hints.

Creating a SQL table from a xls (Excel) file

I'm trying to convert an Excel document into a table in SQL 2005. I found the link below and am wondering if it looks like a solution. If so, what would the #excel_full_file_name syntax be and where would the path be relative to?
http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-convert-Excel_to_table.html
You can use the BULK INSERT T-SQL command if you just want a pure sql solution. You have to save the file as csv/text first.
BULK
INSERT YourDestinationTable
FROM 'D:\YourFile.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
Alternatively, you can try OPENROWEST - again , a pure T-SQL solution.
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;DATABASE=D:\YourExcelFile.xls', 'Select * from YourExcelFile')
It really depends on how much control and flexibility you want, the SSIS route will have benefits over these methods.
Glancing over the code, I would expect it to be the full path name of the excel document:
For example: c:\path\to\my\excel\document.xls
I haven't installed the procedure though or run it, so I could be wrong - but that's what it appears to be at first glance.
I would suggest using an SSIS/DTS Package, to convert. It's much easier.
SSIS Excel example
** note that this example is using the wizard. you can schedule the SSIS/DTS package as a job to run, on your SQL box.
This example copies data from SQL to Excel.
But it is just a matter of swapping the OleDb providers to get it to work in the opposite direction.