Modify XML in SQL field stored as Varbinary - sql-server

My XML is stored as varbinary in a field.
SELECT cast (inboxXml as xml) FROM globalDB.Inbox WHERE inboxCId = '207435-N'
I would like to update one attribute (below). However the error is "Cannot call methods on varbinary(max)." I tried different ways to cast it, but I cannot find it.
thank you,
;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema')
UPDATE globalDB.Inbox
SET inboxXml.modify('replace value of (//ReceiveDeliveryHeader/DocumentID/ID/#accountingEntity[.="ABC"])[1] with "ZZZ"')
WHERE inboxCId = '207435-N'

The first question is: Why are you storing your XML within a VARBINARY column?
This is slow, clumsy and erronous...
The second thing is: .modify() will work against a real native XML only. Neither inboxXml.modify() nor CAST(inboxXml AS XML).modify() will work...
This is one more reason to change your column's type to XML...
Try this:
DECLARE #tbl TABLE(ID INT IDENTITY,YourXml VARBINARY(MAX));
DECLARE #SomeXML XML='<root><someNode someAttr="test">content</someNode></root>';
INSERT INTO #tbl VALUES(CAST(#SomeXML AS VARBINARY(MAX)));
--this works
SELECT ID
,YourXml
,CAST(YourXml AS XML)
FROM #tbl
WHERE ID=1;
--but this is not allowed
UPDATE #tbl SET CAST(YourXml AS XML).modify('replace value of (/root/someNode/#someAttr)[1] with "blah"')
WHERE ID=1
--What you can do:
DECLARE #intermediateXML XML= (SELECT CAST(YourXml AS XML) FROM #tbl WHERE ID=1);
SET #intermediateXML.modify('replace value of (/root/someNode/#someAttr)[1] with "blah"');
UPDATE #tbl SET YourXml=CAST(#intermediateXML AS VARBINARY(MAX)) WHERE ID=1;
--voila!
SELECT ID
,YourXml
,CAST(YourXml AS XML)
FROM #tbl
WHERE ID=1;

Related

How to check if a value is included by a list in an effective way in sql 2008?

I would like to use something like an .Include function in SQL Server 2008, but I could not find the correct syntax for it. I have a sql query like below:
--#values has to be varchar list and start & end with comma
declare #values varchar(max) = ',7,34,37,74,85,'
select (case when #values like '%,' + m.Id + ',%' then m.Name else null end)
from #myTable m
So the logic is, if ID of a record matches with one of the numbers in #values list, I would like to see its name in the output list. This query is working fine, but I would like to find a more professional way to handle it, maybe like:
case when #values.Include(m.Id) then m.Name else null end
Any advice would be appreciated. Thanks.
The fastest method to split a delimited string is using xquery in my experience.
Ex:
DECLARE #values VARCHAR(50), #XML XML
SET #values = ',7,34,37,74,85,'
SET #XML = cast(('<X>'+replace(#values,',' ,'</X><X>')+'</X>') as xml)
SELECT N.value('.', 'VARCHAR(255)') as value FROM #XML.nodes('X') as T(N)
declare #table table (id varchar(5))
insert into #table(id)
values ('7')
select *
from #table y
where exists (SELECT 1 FROM #XML.nodes('X') as T(N) where N.value('.', 'VARCHAR(255)') = y.id)
If you are calling this code from an application, you might want to consider using Table-Valued Parameters and a stored procedure to do this.
First, you would need to create a table type to use with the procedure:
create type dbo.Ids_udt as table (Id int not null);
go
Then, create the procedure:
create procedure dbo.get_names_from_list (
#Ids as dbo.Ids_udt readonly
) as
begin;
set nocount, xact_abort on;
select t.Name
from t
inner join #Ids i
on t.Id = i.Id
end;
go
Then, assemble and pass the list of Ids to the stored procedure using a DataTable added as a SqlParameter using SqlDbType.Structured.
Table Valued Parameter Reference:
SQL Server 2008 Table-Valued Parameters and C# Custom Iterators: A Match Made In Heaven! - Leonard Lobel
Table Value Parameter Use With C# - Jignesh Trivedi
Using Table-Valued Parameters in SQL Server and .NET - Erland Sommarskog
Maximizing Performance with Table-Valued Parameters - Dan Guzman
Maximizing throughput with tvp - sqlcat
How to use TVPs with Entity Framework 4.1 and CodeFirst
Assuming that the data/list is not required to be structered as a comma separated list you could either use IN, EXISTS or SOME / ANY
If it is unavoidable you could use JiggsJedi way but since you asked for a fast way you should try to store the data in a way that in can be processed faster and does not require additional work to be queried.
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
Drop table #Temp
Create table #Temp (ID INt ,Name varchar(5))
INSERT into #Temp
SELECT 7,'AA' Union all
SELECT 34,'BA' Union all
SELECT 37,'CA' Union all
SELECT 74,'DA' Union all
SELECT 85,'TA'
DECLARE #values varchar(max) = ',,,,,,7,,34,,,74,85,,,,' --If extra commas are added in starting or end or in between of string it could handle
SET #values=','+#values+','
SELECT #values= LEFT(STUFF(#values,1,1,''),LEN(#values)-2)
DECLARE #SelectValuesIn TABLE(Value INT)
INSERT INTO #SelectValuesIn
SELECT Split.a.value('.', 'VARCHAR(100)') AS Data
FROM
(
SELECT
CAST ('<M>' + REPLACE(#values, ',', '</M><M>') + '</M>' AS XML) AS Data
) AS A CROSS APPLY Data.nodes ('/M') AS Split(a);
SELECT * FROM #Temp WHERE ID IN(SELECT Value from #SelectValuesIn)

How to avoid bulk data duplicate values when insert in to a table in SQL Server 2012

The problem im trying to solve is about avoiding duplicate data getting into my table. I'm using xml to send bulk data to a stored procedure. The procedure I wrote works with 100, 200 records. But when it comes to 20000 of them there is a time out exception.
This is the stored procedure:
DECLARE #TEMP TABLE (Page_No varchar(MAX))
DECLARE #TEMP2 TABLE (Page_No varchar(MAX))
INSERT INTO #TEMP(Page_No)
SELECT
CAST(CC.query('data(PageId)') AS NVARCHAR(MAX)) AS Page_No
FROM
#XML.nodes('DocumentElement/CusipsFile') AS tt(CC)
INSERT INTO #TEMP2(Page_No)
SELECT Page_No
FROM tbl_Cusips_Pages
INSERT INTO tbl_Cusips_Pages(Page_No, Download_Status)
SELECT Page_No, 'False'
FROM #TEMP
WHERE Page_No NOT IN (SELECT Page_No FROM #TEMP
INTERSECT
SELECT Page_No FROM #TEMP2)
How can I solve this? Is there a better way to write this procedure?
As was already suggested, NVARCHAR(MAX) column/variable is very slow and has limited options. If you can change it, it would help a lot.
MERGE tbl_Cusips_Pages
USING (
SELECT
CAST(CC.query('data(PageId)') AS NVARCHAR(4000))
FROM
#XML.nodes('DocumentElement/CusipsFile') AS tt(CC)
) AS source (Page_No)
ON tbl_Cusips_Pages.Page_No = source.Page_No
WHEN NOT MATCHED BY TARGET
THEN INSERT (Page_No, Download_Status)
VALUES (source.Page_No, 'false')
Anyway, your query is not that bad either, just put the queries directly into the third one (TEMP2 one for sure) instead of inserting the data into the table variables. Table variables are quite slow in comparison.
Replace last INSERT Statement with following Script, I have replace IN Clause With NOT EXISTS that may help you for better performance.
DECLARE #CommanPageNo TABLE (Page_No varchar(MAX))
INSERT INTO #CommanPageNo SELECT Page_No FROM #TEMP
INTERSECT
SELECT Page_No FROM #TEMP2
INSERT INTO tbl_Cusips_Pages(Page_No, Download_Status)
SELECT Page_No, 'False'
FROM #TEMP
WHERE NOT EXISTS (SELECT 1 FROM #CommanPageNo WHERE Page_No=#CommanPageNo.Page_No)

How can I update multiple XML fields with MSSQL?

Table Name : TBL_CLIENTS
Table Field : XMLDATA
<REPORT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DESCRIPTION="TestClient" FILENUM="1234">
<!--<REPORT DESCRIPTION="TestClient" FILENUM="1234">-->
<TRACKING>
<FIRSTNAME>Bobby</FIRSTNAME>
<LASTNAME>Butcher</LASTNAME>
</TRACKING>
</REPORT>
I want to change both the FIRSTNAME and LASTNAME. Is there anyway I can do it in a single query? The only way I can figure it out is using two queries.
UPDATE TBL_CLIENTS
SET [XMLDATA].modify('replace value of (/REPORT/TRACKING/FIRSTNAME/text()) [1] with ("Franny")')
WHERE ORDERID = 5
and
UPDATE TBL_CLIENTS
SET [XMLDATA].modify('replace value of (/REPORT/TRACKING/LASTNAME/text())[1] with ("Farmer")')
WHERE ORDERID = 5
It's not possible - per MSDN, replace has to work on a single instance of an XML node - but you could avoid doing two UPDATEs on the table this way:
DECLARE #doc xml = '<REPORT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DESCRIPTION="TestClient" FILENUM="1234">
<!--<REPORT DESCRIPTION="TestClient" FILENUM="1234">-->
<TRACKING>
<FIRSTNAME>Bobby</FIRSTNAME>
<LASTNAME>Butcher</LASTNAME>
</TRACKING>
</REPORT>';
DECLARE #t table (xmldata xml);
insert #t (xmldata) values (#doc);
-- grab the XML data from the table for manipulation...
DECLARE #xmlData xml;
SELECT #xmlData = xmldata FROM #t;
set #xmlData.modify('replace value of (/REPORT/TRACKING/FIRSTNAME/text()) [1] with ("Franny")')
set #xmlData.modify('replace value of (/REPORT/TRACKING/LASTNAME/text()) [1] with ("Farmer")')
-- now we only need to do one update on the table itself.
UPDATE #t
SET [XMLDATA] = #xmlData
SELECT * FROM #t;
I know this isn't exactly what you were asking for, but it should result in less locking time and better performance on the table - if that's something you're aiming for.

Sql Xquery How to Replace text in Update Query

Table is named as MasterTable
Columns
ID type BIGINT,
Name type VARCHAR(200) (stores xml type data for some reasons)
Name contains data structured as
<en-US>SomeEnglishText</en-US><it-IT>SomeItalicText</it-IT>
When I need to Update the Master Table then at that time I Need to cast the Varchar to xml then conditionally update / replace the value part of particular tag i.e either en-US / it-IT.
Also there are chances that No data/tags are there in Name column so I think at the time of Inserting data it would Insert empty tag elements in the table like <en-US></en-US><it-IT></it-IT>, so the update query must handle empty value in tag elements namely en-US/it-IT.
I am trying to do it like following update query.
DECLARE #Str VARCHAR(200)
SET #Str = 'Test Text'
UPDATE [MasterTable]
SET [Name] = cast([MasterTable].[Name] as xml).modify('replace value of (en-US/text())[1] with sql:variable("#Str")')
WHERE [ID]=18
I getting following error when running the query
Illegal use of xml data type method 'modify'. A non-mutator method is expected in this context.
You can not assign from a xml.modify. Modify works on the variable/column directly. You can also not use modify on a cast.
You can extract the name to a xml variable, modify the xml and then put it back to the table.
declare #str varchar(200) = 'Test'
declare #xml xml
select #xml = cast(Name as xml)
from MasterTable
where ID = 18
set #xml.modify('replace value of (en-US/text())[1] with sql:variable("#Str")')
update MasterTable
set Name = cast(#xml as varchar(200))
where ID = 18
If you need this to work over more than one row at a time you can use a table variable with columns id and name where data type for name is xml instead of the #xml variable.
declare #str varchar(200) = 'Test Text'
declare #T table (ID int, Name xml)
insert into #T
select ID, cast(Name as xml)
from MasterTable
where Name is not null
update #T
set Name.modify('replace value of (en-US/text())[1] with sql:variable("#Str")')
update MasterTable
set Name = cast(T.Name as varchar(200))
from #T as T
where MasterTable.ID = T.ID

SQL Server: Output an XML field as tabular data using a stored procedure

I am using a table with an XML data field to store the audit trails of all other tables in the database.
That means the same XML field has various XML information. For example my table has two records with XML data like this:
1st record:
<client>
<name>xyz</name>
<ssn>432-54-4231</ssn>
</client>
2nd record:
<emp>
<name>abc</name>
<sal>5000</sal>
</emp>
These are the two sample formats and just two records. The table actually has many more XML formats in the same field and many records in each format.
Now my problem is that upon query I need these XML formats to be converted into tabular result sets.
What are the options for me? It would be a regular task to query this table and generate reports from it. I want to create a stored procedure to which I can pass that I need to query "<emp>" or "<client>", then my stored procedure should return tabular data.
does this help?
INSERT INTO #t (data) SELECT '
<client>
<name>xyz</name>
<ssn>432-54-4231</ssn>
</client>'
INSERT INTO #t (data) SELECT '
<emp>
<name>abc</name>
<sal>5000</sal>
</emp>'
DECLARE #el VARCHAR(20)
SELECT #el = 'client'
SELECT
x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
x.value('.','VARCHAR(20)') AS ColumnValue
FROM #t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("#el")]') a (x)
/*
ColumnName ColumnValue
-------------------- --------------------
client xyz432-54-4231
*/
SELECT #el = 'emp'
SELECT
x.value('local-name(.)', 'VARCHAR(20)') AS ColumnName,
x.value('.','VARCHAR(20)') AS ColumnValue
FROM #t
CROSS APPLY data.nodes('/*[local-name(.)=sql:variable("#el")]') a (x)
/*
ColumnName ColumnValue
-------------------- --------------------
emp abc5000
*/
Neither xyz432-54-4231 nor abc5000 is valid XML.
You can try to select only one particular format with a like statement, f.e.:
select *
from YourTable
where YourColumn like '[a-z][a-z][a-z][0-9][0-9][0-9][0-9]'
This would match 3 letters followed by 4 numbers.
A better option is probably to add an extra column to the table, where you save the type of the logging. Then you can use that column to select all "emp" or "client" rows.
An option would be to create a series of views that present the aduit table, per type in the relations that you're execpting
for example
select
c.value('name','nvarchar(50)') as name,
c.value('ssn', 'nvarchar(20)') as ssn
from yourtable
cross apply yourxmlcolumn.nodes('/client') as t(c)
you could then follow the same pattern for the emp
you could also create a view (or computed column) to identify each xml type like this:
select yourxmlcolumn.value('local-name(/*[1])', 'varchar(100)') as objectType
from yourtable
Use open xml method
DECLARE #idoc int
EXEC sp_xml_preparedocument #idoc OUTPUT, #xmldoc
SELECT * into #test
FROM OPENXML (#idoc, 'xmlfilepath',2)
WITH (Name varchar(50),ssn varchar(20)
)
EXEC sp_xml_removedocument #idoc
after you get the data in the #test
and you can manipulate this.
you may be put the diff data in diff xml file.

Resources