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

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/

Related

Declare SQL construct or statement is not supported for simple query

I have simple SQL Query and need to get the input from parameter. I am getting the when i click parameter button. Please let me know how to get the parameter if i have declare statement in my scripts.
Note :OLEDB connection manager used for connection
You can't use parameters in an OLEDB Source if your command contains DECLARE, or anything other than a single SELECT statement.
The way to do what you want is to use build your entire SQL Command into a single SSIS variable, and use the "SQL Command from variable" option in the OLEDB source.
EDIT based on comments:
If you have to handle a SQL Command over 4000 characters, the only way to do it is in a Script Component. The SQL Command in a script component can be any length. Here is an example.

SQL Server Management Studio - how to specify UTF-8 parameter to stored procedure arguments?

I have a stored procedure which searches by text passed by a parameter. I noticed that if the text is not in English (i.e. Hebrew, Arabic), the query finishes without returning any rows. I am sure there is data to be found
I dont know which SQL server is being used : it is whatever is provided by GoDaddy on shared Windows hosting plan.
The thing is, I have a asp.net site which can search and fetch the data from this column encoded as UTF-8. The path which does not work is the MS SQL Management Studio. Even when I manually run same stored procedures which work from inside asp.net - they dont manage to find non English characters. The parameters I pass to the query are prefixed by N qualifier.
Try using the N character like the sample :
Select * from students where name like N'%بیژن%'
and as mentioned in the comment the column data type should be Nvarchar.

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.

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

tsql : outputting each record to their own text file

is there a simple way to just output each record in a select statement to write to its own file?
for example, if you have the tsql query in sql server 2005,
select top 10 items, names + ':' + address from book
and you ended up with 10 text files with the individual name and addresses in each file.
is there a way to do this without writing an extensive spWriteStringToFile procedure? I'm hoping there is some kind of output setting or something in the select statement.
thanks in advance
SQL returns the result set first, there's no opportunity in there for writing records to specific files until afterwards.
Being SQL Server 2005, it's possible you could use a SQLCLR (.NET 2.0 code) function in a SQL statement without having to make a separate application.
In SSMS, you can do a results to file, but that wouldnt split each record out into its own file. I pretty sure you cannot do this out of the box, so it sounds like you will be rolling your own solution.
You'd do this in some client, be it Java, VBA or SSIS typically.