SELECT
**FIELDS**
AS [text()] --Stops the XMLPATH line rendering output as XML
FROM #temp
WHERE **CONDITIONS**
FOR XML PATH('')
This doesn't work in SQL server 2000 (unsupported).
I have tried using FOR XML RAW but it returns loads of useless information. i.e:
<row text x0028 x0029="blah, blah"> <row text x0028 x0029="blah">
The above code currently returns a concatenated string(made up of multiple columns of varying types) from every single row in the table.
How can I achieve this in SQL Server 2000?
Concatenation in SQL Server 2000:
declare #s varchar(8000);
set #s = '';
select #s = #s + field1 + field2 + field3
from #temp
where ...
order by ...;
select #s;
The XML datatype isn't supported in 2000, it was introduced in 2005.
SQL 2000 did introduce the FOR XML, but it only supported RAW, AUTO, and EXPLICIT
You've to define some text or character inside FOR XML PATH(' ') like,
FOR XML PATH('abc'). You can also use FOR XML RAW('abc') for outputting raw XML.
Related
I am currently struggling to find a solution to the following problem.
I have a result set of VARBINARY values - for example:
QAAAAAAAAAE=
QAAAAAAAAAQ=
these results Need to be packed into a XML Element delimited by a single space (to siginify an array of values). Example of how the result should look:
<results>QAAAAAAAAAE= QAAAAAAAAAQ=</results>
The issue I am having while using XML PATH is that I cannot combine a ' ' (varchar) and the result varbinary field. If I add the ' ' before the result and then convert to varbinary the result is incorrect due to having converted the space as well. Here is an example of what I have attempted thus far:
(
STUFF((SELECT DISTINCT ' ' + CONVERT( VARBINARY, id)
FROM results
FOR XML PATH('ns2:children')
),1,1,'')
),
You should not deal with XML on string level. This might have various side effects...
You can try this:
I fill a table with some values in a VARBINARY column:
DECLARE #table TABLE(SomeData VARBINARY(MAX));
INSERT INTO #table VALUES(0x12AF)
,(CAST('test' AS VARBINARY(MAX)))
,(CAST(GETDATE() AS VARBINARY(MAX)));
SELECT *
FROM #table;
The result
SomeData
0x12AF
0x74657374
0x0000A81100AD9F69
If you get this as XML, the conversion to base64 is done implicitly
SELECT * FROM #table FOR XML PATH('result')
the result
<result>
<SomeData>Eq8=</SomeData>
</result>
<result>
<SomeData>dGVzdA==</SomeData>
</result>
<result>
<SomeData>AACoEQCtn2k=</SomeData>
</result>
Now there is XQuery-function data() to your rescue. It will automatically concatenate all sub-values separated by a blank:
SELECT(
SELECT *
FROM #table
FOR XML PATH('result'),TYPE
).query('data(/result/SomeData)') AS result
FOR XML PATH('results')
The result is
<results>
<result>Eq8= dGVzdA== AACoEQCtn2k=</result>
</results>
Pls. try short of SQL Command which could help u to achieve the above result :
SELECT REPLACE(T.Data, '</results><results>', ' ')
FROM
(
SELECT STUFF(
(
SELECT DATA [results]
FROM <table_name> FOR XML PATH('')
), 1, 1, '<') [Data]
) T;
Result :
<results>QAAAAAAAAAE= QAAAAAAAAAQ=</results>
If I understand what do you want?! A way to get <results>QAAAAAAAAAE= QAAAAAAAAAQ=</results> as result is:
select ltrim((
select ' '+cast(id as varchar(50))
from t
group by id
for xml path(''))) results
for xml path('');
SQL Fiddle Demo
I want to get the actual text instead of ??? result .
using sql - mssql 2005
I already tried collation and it didn't work
truncate table EPGXML
INSERT INTO EPGXML
( Data
)
SELECT CONVERT(XML,BulkColumn,2)
FROM OPENROWSET(BULK N'C:\test\test.xml', SINGLE_BLOB) O;
DECLARE #Data XML;
SELECT #Data = Data FROM EPGXML ;
select distinct filenode.value('category[1]', 'VARCHAR(300)')
FROM #Data.nodes('tv/programme ') files ( filenode )
where filenode.value('category[1]', 'VARCHAR(300)') is not null
Are you trying to read unicode text? Use NVARCHAR instead of VARCHAR for unicode support.
I have the following SQL insert statement.
insert into [dbo].[Lookup] (XMLField)
select
'<root>' +
'<SQL>' + SQLQueryToEscape + '</SQL>' +
'</root>' as CustomData
from dbo.CustomView
My dilemma is that the SQLQueryToEscape has characters in it that aren't XML safe. I need to escape the string. What is the best way to do this in tSQL?
Use FOR XML to generate proper XML instead of concatenating strings!
insert into [dbo].[Lookup] (XMLField)
select SQLQueryToEscape
from dbo.CustomView
for xml path ('SQL'), root('root')
I store an XML string in a column in my SQL Server database. I'm trying to run an UPDATE statement that will make a certain field into Upper Case. I can do that easily during a SELECT but can't get it to work with an update.
This SELECT statement works
select Upper(XmlTest.value('(/CodeFiveReport/Owner/#UnitNumber)[1]', 'varchar(10)')) as UnitNumber
from uploadreport
But I want to update the XML as that permanently
Update table Set XmlString.Modify('replace value of (/Root/Node/#Field)[1] with ?
upper-case was added to SQL Server in SQL Server 2008, so there's no good solution prior to that unless you want to use cursors. The following works in SQL Server 2008:
declare #xml xml
set #xml = N'<CodeFiveReport><Owner UnitNumber="Mixed"/></CodeFiveReport>'
select T1.C1.value('upper-case((/CodeFiveReport/Owner/#UnitNumber)[1])', 'varchar(10)') from #xml.nodes('/') T1(C1)
SET #xml.modify('
replace value of (/CodeFiveReport/Owner/#UnitNumber)[1]
with xs:string(upper-case((/CodeFiveReport/Owner/#UnitNumber)[1]))
')
select #xml
Use the XQuery function upper-case
I have a hashset that I want to serialize to a SQL Server table. When serialized hashset looks like this...
<InstallerContactIds>
<int>153771</int>
<int>209572</int>
</InstallerContactIds>
I am using the following to insert the XML into the table...
INSERT INTO dbo.cv_AssessorActionPlanInstallers
SELECT #AssessorActionPlanId, InstallerId
From OPENXML (#XmlDocumentHandle, 'AssessorActionPlan/InstallerContactIds', 2)
With
(
InstallerId int 'int'
)
However, I only get the first id inserted into the table. Is it possible to insert all ids in this case?
I am using SQL Server 2005
You didn't specify what version of SQL you are using, but if you are on 2005 or newer, you can use the new XML data type.
declare #xml xml
select #xml = '
<InstallerContactIds>
<int>153771</int>
<int>209572</int>
</InstallerContactIds>'
select
x.i.value('.', 'int')
from
#xml.nodes('/InstallerContactIds/int') as x(i)
If you are stuck using OpenXML, it should work if you do this:
INSERT INTO dbo.cv_AssessorActionPlanInstallers
SELECT #AssessorActionPlanId, InstallerId
From OPENXML (#XmlDocumentHandle, 'AssessorActionPlan/InstallerContactIds/int', 2)
With
(
InstallerId int '.'
)