Write stored procedure parameters to an XML Column - sql

I have a need to take the parameters passed in to a Stored Procedure (SQL 2005) and write those values into an XML column as one xml document.
Looking for an idea on how to start it.

Well, let's do this thing!
select 1 [one],2 [two],3 [three]
from (select null dummy) t
for xml auto
and we get
<t one="1" two="2" three="3" />
Neat, eh?
You can also experiment with for xml path like so:
select 1[one],2[two],3[three]
from (select null dummy) t
for xml path('foo')
And the result is:
<foo>
<one>1</one>
<two>2</two>
<three>3</three>
</foo>

Related

How can I save an XML file from a SQL query?

I'm using SQL Server 2019 - v15.0.4123.1.
This is the sample query (from this question):
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, city VARCHAR(30))
INSERT INTO #tbl (city)
VALUES ('Miami'), ('Orlando');
SELECT
'SIN_OPE' AS [#cod_1],
'08' AS [#cod_2],
'12' AS [#num_reg],
'yyyyyyyyyyyyyyyyyyy.xsd' AS [#xsi:noNamespaceSchemaLocation],
(SELECT *
FROM #tbl
FOR XML PATH('r'), TYPE)
FOR XML PATH('root'), TYPE, ELEMENTS XSINIL;
The result of the query is:
I just want to save/export this file as an XML file, automatically (with a stored procedure) to a folder in my PC. I've found many possible solutions but they are pretty old and doesn't work with my environment. I still can't understand if it's possible to export XML from SQL Server. I would like to avoid the manual passage "Query - Results to - Results to file"
Any advice?
Thank you
On you SQL Server Management Tool go to Query --> Result --> Result to file and then execute your query.
once you execute the query it will open a window & give you an option to save the file, there you can save it as xml.

Result Set of procedure into XML

how can we convert the result set of a procedure into an XML without creating a temporary table since the result set may vary in their structure based on the inputs ? the scenario must work in both on premise and azure hosted SQL server databases.
CREATE PROCEDURE SPSample(#Scope VARCHAR(2))
AS
BEGIN
IF(#Scope='A')
SELECT 1 AS ID,2 AS NAME
IF(#Scope='B')
SELECT 1 AS CustomerID,'Sample#sample.com' AS Email,'121314' AS ContactNumber
END
I believe you are asking to use FOR XML or FOR XML AUTO:
https://learn.microsoft.com/en-us/sql/relational-databases/xml/for-xml-sql-server?view=sql-server-2017
create table mytable(col1 int, col2 nvarchar(20), col3 money)
insert into mytable(col1, col2, col3) values (1, 'string', 123.45)
select * from mytable
for xml auto
Emits:
You can do this on any query result set:
select * from sys.objects
for xml auto
This will emit that structure similarly.
i need a way that the result set from a stored procedure directly converted to XML object without creating a table structure since the result set from procedure may vary
There's no way in TSQL to do that. TSQL can only convert to XML using a SELECT, not an EXECUTE. You could do this with a loopback linked server or CLR, but those are hacks, and not available in Azure SQL DB. You'll have to find another way. EG
CREATE PROCEDURE SPSample(#Scope VARCHAR(2), #ResultsAsXml bit = 0)
. . .

Fetch Xml Content from a table in oracle

This is my XML Content stored same as it is in a Column name "Xml_Column" in the table "TEST_TABLE" and the data type of the column is clob.
<ns0:TEST_EVENTS xmlns:ns0="http://TEST.APPLICATION.ABC.Schemas.XML_Target_TESTEvents">
<compname>Sherlock</compname>
<Add>Homes</Add>
<Employee>
<firstname>Jim</firstname>
<lastname>Moriarty</lastname>
<age>52</age>
<email>jim#sh.com</email>
</Employee>
</ns0:TEST_EVENTS>
My requirement is to fetch the firstname and last name from this column where my xml content is stored and display. Any help
Use EXTRACTVALUE, it's a simpliest way: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions052.htm
For optimization, you should make inner query for constructing XMLType instance based on your clob; in the outer query use EXTRACTVALUE to select every value you wish.
For example:
SELECT extractValue(OBJECT_VALUE, '/firstname'),
extractValue(OBJECT_VALUE, '/lastname')
FROM TEST_TABLE;
All firstname and lastname:
SELECT XMLQuery('for $d in /Employee
return ($d/firstname/text(),$d/lastname/text())
PASSING OBJECT_VALUE
RETURNING CONTENT) FROM TEST_TABLE;

pass list of int's as xml to be used with where clause

I am sending the following xml to a sql server stored procedure to be used in a where clause.
<root>
<id>1</id>
<id>2</id>
<id>3</id>
</root
I am trying to work out a way to have a select statement where the where clause will check for any of the id values in the passed xml parameter.
Something like this:
#xml xml
select * from table
where (if exists (select id in #xml))
sorry i am not very good at sql.
declare #xml xml;
set #xml = CAST('<root><id>1</id><id>2</id><id>3</id></root>' AS XML)
select id from mytable
where exists(select 1 from #xml.nodes('/root/id')as result(node)
where node.value('(.)[1]', 'int') = id)
I always refer to this link from Erland Sommarskog for handling arrays in sql. It provides a good example and contrasts it with other mechanisms of passing in arrays to stored procedures.
Note if you are using SQL Server 2008 the he has another link detailing the use of table valued parameters. It might be overkill for your example, but if you are keen to learn what is available at your disposal, then its all worth a read.

How to delete an attribute from an XML variable in sql server 2008?

I have a table called XML (in SQL Server 2008) and it has a field called XmlDocument of type XML. I am trying to to delete an attribute from an XML variable.
Here is how my xml looks like
<clue_personal_auto xmlns="http://cp.com/rules/client">
<admin>
<receipt_date>03/16/2011</receipt_date>
<date_request_ordered>03/16/2011</date_request_ordered>
<report_usage>Personal</report_usage>
</admin>
</clue_personal_auto>
My query
UPDATE XML
SET XmlDocument.modify('delete (/clue_personal_auto/#xmlns)[1]')
WHERE xmlid = 357
When I run this query in query analyzer I see the message "1 row(s) affected" but in reality the xmlns attribute of clue_personal_auto element is not being removed. Any idea what am I doing wrong.
Thanks
BB
You need to use WITH xmlnamespaces, otherwise "/clue_personal_auto" does not match the NAMESPACED clue_personal_auto xmlns="..." node.
Not only that, you cannot actually remove a namespace since it is not a normal attribute.
Example of removing a regular attribute
declare #xml table (xmlid int, xmldocument xml)
insert #xml select 357, '
<clue_personal_auto xmlns="http://cp.com/rules/client" otherattrib="x">
<admin>
<receipt_date>03/16/2011</receipt_date>
<date_request_ordered>03/16/2011</date_request_ordered>
<report_usage>Personal</report_usage>
</admin>
</clue_personal_auto>'
;WITH XMLNAMESPACES ('http://cp.com/rules/client' as ns)
UPDATE #XML
SET XmlDocument.modify('delete (/ns:clue_personal_auto/#otherattrib)[1]')
WHERE xmlid = 357
select * from #xml
UPDATE XML
SET CONVERT(XML, REPLACE(CONVERT(NVARCHAR(MAX), XmlDocument), N' xmlns=...'))
WHERE ID = 357
I can't seem to find an easy way to do this - but the real question remains: why do you want to remove the namespace?? Using the WITH XMLNAMESPACES ... construct, you can easily make use of the namespaces.
Instead of putting a lot of effort in getting rid of it - learn about XML namespaces and start using them!
You can quite easily use that XML namespace in your queries:
;WITH XMLNAMESPACES (DEFAULT 'http://cp.com/rules/client' )
SELECT
XmlDocument.value('(/clue_personal_auto/admin/report_usage)[1]', 'varchar(25)')
FROM XML
WHERE ID = 357
and be happy with it - no need to artificially remove xmlns= declarations anymore!