Given this xml
<items>
<item>
<position>2</position>
<code>123-122</code>
<description>Circulator 5GPM</description>
</item>
<item>
<position>4</position>
<code>124-128</code>
<description>Circulator 25GPM</description>
</item>
</items>
I want to insert values from a relational table like
preferably using T-Sql sql:column("position") etc..
How would I do that ?
You could insert new nodes into xml like this
DECLARE #xml XML = '<items>
<item>
<position>2</position>
<code>123-122</code>
<description>Circulator 5GPM</description>
</item>
<item>
<position>4</position>
<code>124-128</code>
<description>Circulator 25GPM</description>
</item>
</items>'
DECLARE #SampleData AS TABLE
(
position int,
code varchar(20),
[description] varchar(100)
)
INSERT INTO #SampleData
VALUES
(1,'123-123','description 1'),
(2,'124-124','description 2')
DECLARE #NewXmlNode XML =
(
SELECT *
FROM #SampleData
FOR XML PATH('item'),TYPE
)
SELECT #xml, #NewXmlNode
SET #xml.modify('
insert sql:variable("#NewXmlNode") as last into (/items)[1]
')
Result:
<items>
<item>
<position>2</position>
<code>123-122</code>
<description>Circulator 5GPM</description>
</item>
<item>
<position>4</position>
<code>124-128</code>
<description>Circulator 25GPM</description>
</item>
<item>
<position>1</position>
<code>123-123</code>
<description>description 1</description>
</item>
<item>
<position>2</position>
<code>124-124</code>
<description>description 2</description>
</item>
</items>
Related
I have a text like that.
<div>
blabla <b> BOLD </b> babla <b> BOLD2</b> blabla
<table width="100%">
<tr>
<td valign="top" style="width:30px">-</td>
<td>blabla <b>BOLD3</b></td>
</tr>
</div>
I want to extract the word between the b tag. But there is several path.
SELECT t1,t2
FROM mytext,
XMLTABLE (
'/div'
PASSING xmltype (raw_text.inhalt)
COLUMNS t2 VARCHAR2 (1000) PATH './b', --
t1 VARCHAR2 (1000) PATH './table/tr/td/b'))
[Error] Execution (11: 1): ORA-19279: XPTY0004 - XQuery dynamic type mismatch: expected singleton sequence - got multi-item sequence
Assuming that your HTML is valid XML (which is not always the case as it needs a single root node and all opening tags to be closed) then given the sample data:
CREATE TABLE mytext (rawtext CLOB);
INSERT INTO mytext (rawtext) VALUES (
'<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/TR/xhtml1/strict" >
<div>
blabla <b> BOLD </b> babla <b> BOLD2</b> blabla
<table width="100%">
<tr>
<td valign="top" style="width:30px">-</td>
<td>blabla <b>BOLD3</b></td>
</tr>
</table>
</div>
</html>'
);
Then you can use:
SELECT bold
FROM mytext m
CROSS JOIN XMLTABLE (
XMLNAMESPACES(DEFAULT 'http://www.w3.org/TR/xhtml1/strict'),
'//b'
PASSING XMLTYPE(m.rawtext)
COLUMNS bold VARCHAR2(1000) PATH './text()'
)
Which outputs:
BOLD
BOLD
BOLD2
BOLD3
db<>fiddle here
This version works too. And you don't have to add more html markup.
You only have to remove XMLNAMESPACES(DEFAULT 'http://www.w3.org/TR/xhtml1/strict')
CREATE TABLE mytext (
rawtext CLOB
);
INSERT INTO mytext (rawtext) VALUES (
'<div>
blabla <b> BOLD </b> babla <b> BOLD2</b> blabla
<table width="100%">
<tr>
<td valign="top" style="width:30px">-</td>
<td>blabla <b>BOLD3</b></td>
</tr>
</table>
</div>'
);
SELECT bold
FROM mytext m
CROSS JOIN XMLTABLE (
'//b'
PASSING XMLTYPE(m.rawtext)
COLUMNS bold VARCHAR2(1000) PATH './text()'
)
https://dbfiddle.uk/?rdbms=oracle_21&fiddle=0889004081eb0c86bf3b869d953cf04e
I am trying to create an unordered list of html links in SQL, and output the results as the desired unordered list.
select tblRoster.ID,tblRoster.pRank,tblRoster.pName,tblRoster.pSquad,
(select '<ul>' + stuff((select '<li>'+ '<'+ 'img src=' + pm.dLink from tblPlayerMedals pm where tblRoster.ID = pm.pID order by dID asc for xml path('')),1,0,'') + ' /></li>' + '</ul>') AS dLink,
stuff((select ','+ pm.dName from tblPlayerMedals pm where tblRoster.ID = pm.pID order by dID asc for xml path('')),1,1,'') [dName]
from tblRoster
order by 1
The output shows the results as plain text and the closing tags for each element - the image,list,and unordered list elements - are not rendered.
Here is a sample of what renders in the aspx page:
<li><img src=https://myimage.jpg?w=84&h=32&pad=0<li><img src=https://myimage.jpg?w=84&h=32&pad=0
Biml (Or SSIS) doesn't seem to want to automatically identify the row Delimiter. Without a column with a manually set on the last column of the column list to the intended Row Delimiter SSIS does not set the delimiter correctly for the row. I'm guessing that SSIS just implies the row delimiter even if it is set in the connection properties. Any one know of a fix for this other than writing around the problem and setting the last column's delmiter to the intended row delimiter (See "T" Column below)?
I checked the properties of the output Connection and it properly states the "RowDelimiter" as the CRLF, but if you look at the
Here is the Biml file:
<Biml xmlns="http://schemas.varigence.com/biml.xsd">
<Connections>
<OleDbConnection Name="Source" ConnectionString="Provider=SQLNCLI11;Server=localhost;Initial Catalog=test;Integrated Security=SSPI;">
</OleDbConnection>
<FlatFileConnection Name="Created" FilePath="D:\\created.dat" FileFormat="Changed">
<Expressions>
<Expression PropertyName="ConnectionString">#[$Package::FileDropRoot] + "\\"+REPLACE((DT_WSTR, 10)(DT_DBDATE)GETDATE(),"-","") + "." + "created.dat"</Expression>
</Expressions>
</FlatFileConnection>
</Connections>
<FileFormats>
<FlatFileFormat Name="Changed" ColumnNamesInFirstDataRow="true" HeaderRowDelimiter="CRLF" RowDelimiter="CRLF">
<Columns>
<Column Name="col1" DataType="String" Delimiter="Comma" ColumnType="Delimited" />
<Column Name="col2" DataType="String" Delimiter="Comma" ColumnType="Delimited"/>
<!-- this must be here in order to terminate the row -->
<Column Name="T" DataType="String" Delimiter="Comma" ColumnType="CRLF"/>
</Columns>
</FlatFileFormat>
</FileFormats>
<Packages>
<Package Name="Test" ConstraintMode="Linear" ProtectionLevel="EncryptSensitiveWithUserKey">
<Tasks>
<Dataflow Name="Test">
<Transformations>
<OleDbSource Name="Select Stagement" ConnectionName="Source">
<DirectInput>
Select * From Test
</DirectInput>
</OleDbSource>
<FlatFileDestination Name="UpdateFile" ConnectionName="Created">
</FlatFileDestination>
</Transformations>
</Dataflow>
</Tasks>
<Parameters>
<Parameter Name="FileDropRoot" DataType="String">D:\FileDrop</Parameter>
</Parameters>
</Package>
</Packages>
</Biml>
Here is the SSIS "Code" with the T Column above, please note that a Row Delimiter is specified (Line Breaks in export file):
<DTS:ConnectionManager DTS:CreationName="FLATFILE" DTS:DTSID="{9CDCB838-2A42-4CCA-A59C-DC60E9B3A967}" DTS:ObjectName="Created" DTS:refId="Package.ConnectionManagers[Created]">
<DTS:ObjectData>
<DTS:ConnectionManager DTS:CodePage="1252" DTS:ColumnNamesInFirstDataRow="True" DTS:ConnectionString="D:\\created.dat" DTS:Format="Delimited" DTS:HeaderRowDelimiter="_x000D__x000A_" DTS:LocaleID="1033" DTS:RowDelimiter="_x000D__x000A_" DTS:TextQualifier="_x003C_none_x003E_" DTS:Unicode="True">
<DTS:FlatFileColumns>
<DTS:FlatFileColumn DTS:ColumnDelimiter="_x002C_" DTS:ColumnType="Delimited" DTS:CreationName="" DTS:DataType="303" DTS:DTSID="{D64391D4-4551-44E9-8539-4C473EB700AA}" DTS:ObjectName="col1" DTS:TextQualified="True">
</DTS:FlatFileColumn>
<DTS:FlatFileColumn DTS:ColumnDelimiter="_x002C_" DTS:ColumnType="Delimited" DTS:CreationName="" DTS:DataType="303" DTS:DTSID="{974ED1AD-7D72-4A65-A877-BADEC09DAF20}" DTS:ObjectName="col2" DTS:TextQualified="True">
</DTS:FlatFileColumn>
<DTS:FlatFileColumn DTS:ColumnDelimiter="_x000D__x000A_" DTS:ColumnType="Delimited" DTS:CreationName="" DTS:DataType="303" DTS:DTSID="{4347C3C1-39BD-40B1-B38F-526730FE7BFB}" DTS:ObjectName="T" DTS:TextQualified="True">
</DTS:FlatFileColumn>
</DTS:FlatFileColumns>
</DTS:ConnectionManager>
</DTS:ObjectData>
<DTS:PropertyExpression DTS:Name="ConnectionString">#[$Package::FileDropRoot] + "\\"+REPLACE((DT_WSTR, 10)(DT_DBDATE)GETDATE(),"-","") + "." + "created.dat"</DTS:PropertyExpression>
</DTS:ConnectionManager>
Here is the SSIS "Code" without the T Column above, please note that a Row Delimiter is specified (No line breaks in export file):
<DTS:ConnectionManager DTS:CreationName="FLATFILE" DTS:DTSID="{79E9C576-FD53-4D4F-A07C-AED8D4CE72E6}" DTS:ObjectName="Created" DTS:refId="Package.ConnectionManagers[Created]">
<DTS:ObjectData>
<DTS:ConnectionManager DTS:CodePage="1252" DTS:ColumnNamesInFirstDataRow="True" DTS:ConnectionString="D:\\created.dat" DTS:Format="Delimited" DTS:HeaderRowDelimiter="_x000D__x000A_" DTS:LocaleID="1033" DTS:RowDelimiter="_x000D__x000A_" DTS:TextQualifier="_x003C_none_x003E_" DTS:Unicode="True">
<DTS:FlatFileColumns>
<DTS:FlatFileColumn DTS:ColumnDelimiter="_x002C_" DTS:ColumnType="Delimited" DTS:CreationName="" DTS:DataType="303" DTS:DTSID="{BBCA22D2-5D3E-47AC-AA0A-413C0C1A5CB2}" DTS:ObjectName="col1" DTS:TextQualified="True">
</DTS:FlatFileColumn>
<DTS:FlatFileColumn DTS:ColumnDelimiter="_x002C_" DTS:ColumnType="Delimited" DTS:CreationName="" DTS:DataType="303" DTS:DTSID="{44E567E4-BE78-432C-A8AC-C388E8BCFADC}" DTS:ObjectName="col2" DTS:TextQualified="True">
</DTS:FlatFileColumn>
</DTS:FlatFileColumns>
</DTS:ConnectionManager>
</DTS:ObjectData>
<DTS:PropertyExpression DTS:Name="ConnectionString">#[$Package::FileDropRoot] + "\\"+REPLACE((DT_WSTR, 10)(DT_DBDATE)GETDATE(),"-","") + "." + "created.dat"</DTS:PropertyExpression>
</DTS:ConnectionManager>
Here is the Script I used to Create the table on the database Connection
CREATE TABLE Test(col1 varchar(25),col2 varchar(25))
INSERT INTO Test
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2' UNION all
SELECT '1','2'
You have an incorrect assumption in that the last column's delimiter shouldn't be CRLF. You'll see in all the linked examples, while counter-intuitive, your flat file format file should use your row delimiter as the column delimiter for the final column. Every other column would use your "standard" column delimiter. And yes, it's repeated from the header declaration of what your row delimiter should be.
http://bimlscript.com/Snippet/Details/18
http://bimlscript.com/Snippet/Details/54
Some people juggle geese...
I have a requirement that i want to search for data in xml data type, from the front end i will get firstname,lastname,dob,email all the fields are not mandatory some fields will come as empty or null i want to search according to that if i will get firstname as 'test' and lastname i will get as empty or null
If it is a varchar datatype then i can create query as
FirstName= ISNULL(#firstname, FirstName) or COALESCE(#firstname, FirstName, '') = '')
but in XML doc how can i use this type of query.
xmlDoc.value('(/personalDetails/firstname)[1]','varchar(100)')
Thanks
In SQLServer 2008, I would write something like :
select
xmlDoc.value('(personalDetails/firstname/text())[1]','varchar(100)') as firstname
from
myTable
where
not xmlDoc is null
and xmlDoc.exist('personalDetails/firstname[.!='''']')>0
You can also do a CASE WHEN ... ELSE ... END if you want to return NULL for non existing values
I think you can achieve this using common table expression. PFB the sample code-
DECLARE #XMLData XML
SET #XMLData ='
<STUDENTS>
<STUDENT>
<StudentID>1</StudentID>
<Name>John Smith</Name>
<Marks>200</Marks>
</STUDENT>
<STUDENT>
<StudentID>2</StudentID>
<Name>Mark Johnson</Name>
<Marks>300</Marks>
</STUDENT>
<STUDENT>
<StudentID>3</StudentID>
<Name></Name>
<Marks>400</Marks>
</STUDENT>
</STUDENTS>'
;with cte as(
SELECT StudentID = Node.Data.value('(StudentID)[1]', 'INT')
, [Name] = Node.Data.value('(Name)[1]', 'VARCHAR(MAX)')
, [Marks] = Node.Data.value('(Marks)[1]', 'INT')
FROM #XMLData.nodes('/STUDENTS/STUDENT') Node(Data)
)
Select * from cte where Name=''
Given the SQL...
declare #xmlDoc xml
set #xmlDoc = '<people>
<person PersonID="8" LastName="asdf" />
<person PersonID="26" LastName="rtert" />
<person PersonID="33" LastName="dfgh" />
<person PersonID="514" LastName="ukyy" />
</people>'
What would be the sql to convert that xml into a table of two columns PersonID and LastName?
SELECT T.c.query('.').value('(//#PersonID)[1]', 'int'),
T.c.query('.').value('(//#LastName)[1]', 'varchar(50)')
FROM #xmlDoc.nodes('/people/person') T(c)
select T.X.value('#PersonID', 'int') as PersonID,
T.X.value('#LastName', 'nvarchar(50)') as LastName
from #xmlDoc.nodes('/people/person') as T(X)