52
loading...
This website collects cookies to deliver better user experience
<!-- sample comment -->
<Customers> <!-- root element -->
<Customer> <!-- nested element -->
<FirstName>Helena</FirstName>
</Customer>
<Customer>
...
</Customer>
...
</Customers>
id | first_name | last_name | city | state |
---|---|---|---|---|
1 | François | Tremblay | Montreal | QC |
2 | Bjørn | Hansen | Oslo | |
3 | Helena | Holý | Prague | |
4 | Fernanda | Ramos | Brasília | DF |
<Customers>
</Customers>
SELECT CONCAT(
'<Customer>',
'<Id>', id, '</Id>',
'<FirstName>', first_name, '</FirstName>',
'<LastName>', last_name, '</LastName>',
'<City>', COALESCE(city, ''), '</City>',
'<State>', COALESCE(state, ''), '</State>',
'</Customer>') customers_xml
FROM customers LIMIT 2;
<Customers>
<Customer><Id>1</Id><FirstName>François</FirstName><LastName>Tremblay</LastName><City>Montreal</City><State>QC</State></Customer>
<Customer><Id>2</Id><FirstName>Bjørn</FirstName><LastName>Hansen</LastName><City>Oslo</City><State></State></Customer>
</Customers>
query_to_xml(query text, nulls boolean, tableforest boolean, targetns text)
function for displaying data in XML format. The query_to_xml
function requires four (4) arguments:query
- The actual SQL query as text.nulls
- If elements with null values should be included. When the value is false, columns with null values will be omitted from the XML document when the XML elements are generated. A true value for the parameter will output a self-closing element containing <columnname xsi:nil="true"/>
attribute.tableforest
- Will put each row in different XML documents. That means that each query data row will be wrapped in a row root element.targetns
- The namespace to put the result in. Pass a blank string to use the default namespace.SELECT query_to_xml(
'SELECT t.id "Id"
, t.first_name "FirstName"
, t.last_name "LastName"
, t.city "City"
, t.state "State"
FROM customers t LIMIT 2', true , false, '')
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<Id>1</Id>
<FirstName>François</FirstName>
<LastName>Tremblay</LastName>
<City>Montreal</City>
<State>QC</State>
</row>
<row>
<Id>2</Id>
<FirstName>Bjørn</FirstName>
<LastName>Hansen</LastName>
<City>Oslo</City>
<State xsi:nil="true"/>
</row>
</table>
query_to_xml_and_xmlschema
function instead of query_to_xml
function.COPY(
SELECT query_to_xml(
'SELECT t.id "Id"
, t.first_name "FirstName"
, t.last_name "LastName"
, t.city "City"
, t.state "State"
FROM customers t', true , false, '')
) TO '~/tmp/customers.xml'
SELECT TOP 2
id Id
,first_name FirstName
,last_name LastName
,city City
,COALESCE(state, '') State
FROM customers
FOR XML PATH('Customer'), ROOT('Customers')
<Customers>
<Customer>
<Id>1</Id>
<FirstName>François</FirstName>
<LastName>Tremblay</LastName>
<City>Montreal</City>
<State>QC</State>
</Customer>
<Customer>
<Id>2</Id>
<FirstName>Bjørn</FirstName>
<LastName>Hansen</LastName>
<City>Oslo</City>
<State></State>
</Customer>
</Customers>
bcp "SELECT TOP 2
id Id
,first_name FirstName
,last_name LastName
,city City
,COALESCE(state, '') State
FROM customers
FOR XML PATH('Customer'), ROOT('Customers')" queryout ~/Documents/customers.xml -S localhost -d testdb -c -U sa
-S
specifies the SQL Server instance to connect to-d
species the database to connect to-c
performs the operation using a character data type without prompting for each field-U
specifies the login ID used to connect to SQL Server