inline schema is not supported with for xml path xmlschema - sql-server

I would like to write a stored procedure as below in SQL Server.
CREATE PROC [dbo].[Employee_delete]
AS
BEGIN
SELECT *
FROM #employee
FOR XML PATH(''), root ('EmployeeDelete'),xmlschema
END
But I'm getting the error--> 'inline schema is not supported with for xml path xmlschema'. May I know, how can I achieve XMLSchema with code "FOR XML PATH(''), root ('EmployeeDelete'),xmlschema"
To auto generate schemas in biztalk , I have to use word "xmlSchema"
Thanks in advance

If you are using BizTalk-2010 you should be using the WCF-SQL adapter for which you don't need to select as FOR XML to be able to generate schemas.

Try...
FOR XML AUTO,ROOT('EmployeeDelete'), TYPE, ELEMENTS XSINIL, XMLSCHEMA

Related

How to Import/export SQL Server tables via XML files

I am receiving data files in XML format from a public agency. The structure of the files and the notations within make me think that what I am seeing is a bulk table dump from SQL Server. Each XML file starts with a schema definition. This is followed by multiple elements, each element looking like it contains one table row of data. The structure of the files makes me think that they were created by some facility within SQL Server itself.
I have looked at the SQL Server docs and online articles, but I cannot find information on how this would be accomplished. I am thinking that if there is a built in facility to export the data in this format, there must be a built in facility to import the data back into SQL Server.
A snippet of one XML file showing the opening schema section and a couple of data elements can be found here;
sample data XML file
Am I correct in thinking that these files are SQL Server dumps? If so, who are these exported and then imported?
You can use xml functionality of SQL Server. Something like this.
Import
declare #x xml = N'--your xml here'
; with xmlnamespaces('urn:schemas-microsoft-com:sql:SqlRowSet1' as p)--required as xml has xmlns attribute
--insert mytable
select t.v.value('p:pool_idn[1]','int') pool_idn, --note p: prefix
t.v.value('p:eff_dte[1]','datetime') eff_dte,
t.v.value('p:pool_nam[1]','varchar(50)') pool_nam
--and so on
from #x.nodes('root/p:pool') t(v)
Export
select * from mytable
for xml auto, elements, root, xmlschema('urn:my:schema')

How to read XML items or data in a SQL Server stored procedure?

It looks like we are trying to parse XML and certain values? Do we do that always with T.item.value? and T(Item) an alias here? I found reference here https://learn.microsoft.com/en-us/sql/relational-databases/xml/load-xml-data?view=sql-server-2017 but still need clarification.
Need to understand what this code is doing and where can I learn more about writing such code like what is T, T(item), T.item.value here..
CREATE PROCEDURE [dbo].[MyTestSP]
#exServers xml = N'<a />' --sample: N'<a><s>abc.com</s><s u="user#example.com>outlook.com</s></a>'
AS
BEGIN
select
T.item.value('.', 'nvarchar(256)') as ExServer,
T.item.value('#u', 'nvarchar(256)') as Account
from #exServers.nodes('a/s') T(item)
END
GO
You can start by going through the tutorial Stairway to XML. After that all will be clear to you.
what is T?
T is an alias for a table of shredded xml fragments from #exServers.
what is T(item)?
This specifies the name T of the derived table from shredding the nodes and it is giving the column with the XML fragments a name item.
T.item.value
It is a way to extract a value from the xml in column item from the alias T.
nodes() Method (xml Data Type)
value() Method (xml Data Type)

How to get a XML structure from SQL Server stored procedure

I am working on a vb.net application, the management wants me to change the applications data source from SQL Server to XML.
I have a class called WebData.vb in the old application I need to somehow find a way to replace the stored procedures in it and make it read xml. So I was thinking of getting the xml structure from the returning result set of the stored procedure. I looked online and they said that for normal select statement you can do something like this:
FOR xml path ('Get_Order'),ROOT ('Get_Orders')
I am looking for something like
EXEC dbo.spMML_GET_ORDERS_FOR_EXPORT
FOR xml path ('Get_Order'),ROOT ('Get_Orders')
so now that I have the structure I can pass that data to a datatable and then return that datatable to the method.
Also if there is an alternative way in creating a XML stored procedure please let me know thanks coders.
Assuming you can't modify the stored proc (due to other dependencies or some other reason) to have the SELECT within the proc have the FOR XML syntax, you can use INSERT/EXEC to insert the results of the stored proc into a temp table or table variable, then apply your FOR XML onto a query of those results.
Something like this should work:
DECLARE #Data TABLE (...) -- Define table to match results of stored proc
INSERT #Data
EXEC dbo.spMML_GET_ORDERS_FOR_EXPORT
SELECT * FROM #Data FOR xml path ('Get_Order'),ROOT ('Get_Orders')
There are a few methods, one adding namespaces using WITH XMLNAMESPACES(<STRING> AS <NAMESPACE string>). XMLNAMESPACES can embed appropriate XML markers to your tables for use with other applications (which hopefully is a factor here), making documentation a little easier.
Depending on your application use, you can use FOR XML {RAW, PATH, AUTO, or EXPLICIT} in your query, as well as XQUERY methods...but for your needs, stick to the simpler method like XML PATH or XML AUTO.
XML PATH is very flexible, however you lose the straightforward identification of the column datatypes.
XMLNAMESPACE
WITH XMLNAMESPACES('dbo.MyTableName' AS SQL)
SELECT DENSE_RANK() OVER (ORDER BY Name ASC) AS 'Management_ID'
, Name AS [Name]
, Began AS [Team/#Began]
, Ended AS [Team/#Ended]
, Team AS [Team]
, [Role]
FROM dbo.SSIS_Owners
FOR XML PATH, ELEMENTS, ROOT('SQL')
XML AUTO
Because you might want to return to the database, I suggest using XML AUTO with XMLSCHEMA, where the sql datatypes are kept in the XML.
SELECT DENSE_RANK() OVER (ORDER BY Name ASC) AS 'Management_ID'
, Name AS [Name]
, Began AS [Team/#Began]
, Ended AS [Team/#Ended]
, Team AS [Team]
, [Role]
FROM dbo.SSIS_Owners
FOR XML AUTO, ELEMENTS, XMLSCHEMA('SSIS_Owners')
Downside is XMLNAMESPACES is not an option, but you can get around this through solutions like XML SCHEMA COLLECTIONS or in the query itself as I showed.
You can also just use XML PATH directly without the namespace, but again, that depends on your application use as you are transforming everything to XML files.
Also note how I defined the embedded attributes. A learning point here, but think about the query in the same order that the XML would appear. That is why I defined the variable attributes first before I then stated what the text for that node was.
Lastly, I think you'll find Paparazzi has a question on this topic that covers quite. TSQL FOR XML PATH Attribute On , Type

Extract XML Node data SQL Query

I am trying to extract XML node from SQL query. I have XML report specification in one of the column of SQL table.
I want to extract sqlText node from the XML using SQL query.
I have uploaded the xml to dropbox:
https://www.dropbox.com/sh/28xu7ifu78h6gm0/AACQS24NEjPFO0GXEI9vLuefa?dl=0
Any help would be greatly appreciated.
If you are using xml document
declare #xml xml ='<report ....</report>'
select #xml.value('(/report/queries/query/source/sqlQuery/sqlText)[1]', 'varchar(100)') as [SqlText]
Assuming that the XML data is stored in XML typed column named myXmlColumn in SQL table named myTable, you can specify the default namespace for the XPath using ;WITH XMLNAMESPACES(), for example :
;WITH XMLNAMESPACES(default 'http://developer.cognos.com/schemas/report/8.0/')
select myTable.myXmlColumn.value('(/report/queries/query/source/sqlQuery/sqlText)[1]'
, 'varchar(max)') as sqlText
If there are more than one sqlQuery node, following SQL XML query can be used
SELECT
[Query].value('.','varchar(100)') AS QueryText
FROM #xml.nodes('/report/queries/query/source/sqlQuery/sqlText') Queries([Query])

What's mean of the "T(C)"? when I query xml in sql server 2008

What's mean of the "T(C)"?
DECLARE #xml XML ='<root><id>1</id><id>2</id><id>3</id><id>4</id></root>'
SELECT
T.C.value('(text())[1]','varchar(20)')
FROM #xml.nodes('/root/id') AS T(C)
Note:can you give me a perfessional url,I want to see carefully.
thx very much.
From MSDN Nodes() Function syntax is
nodes (XQuery) as Table(Column)
Table(Column)
Is the table name and the column name for the resulting rowset.

Resources