Background:
I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete some values while leaving the other values alone. I am designing this database/table/column so maybe I could use attributes or elements/nodes, the choice is ultimately mine. The one constraint is that I have to allow for customized name/value pairs. The pairs will always be strings, but I cannot limit the names or values to any known set (like only allowing "Last Name", "First Name", and "Favorite Pet" because the user may want to keep track of "Favorite Color", etc. in the future). My Progress:
Say I have a (simplified) table: - CREATE TABLE ExampleXML(
-
ID NVARCHAR(20) NOT NULL,
-
Dat XML NULL,
-
CONSTRAINT PK_ExampleXML PRIMARY KEY (ID ASC)
-
)
and I have some records: - INSERT INTO ExampleXML (ID,Dat) VALUES ('1', '<info FavoriteColor="green" FavoriteWebsite="thescripts" Sex="Unknown" />')
-
INSERT INTO ExampleXML (ID,Dat) VALUES ('2', '<info FavoriteColor="red" FavoriteWebsite="thescripts" Sex="F" />')
-
INSERT INTO ExampleXML (ID,Dat) VALUES ('3', '<info FavoriteColor="red" FavoriteWebsite="thescripts" FavoriteSandwich="PB n J" Sex="F" />')
-
INSERT INTO ExampleXML (ID,Dat) VALUES ('4', '<info FavoriteWebsite="thescripts" FavoriteSandwich="Ham n Swiss" Sex="F" />')
-
And I made a stored Procedure to do the "merge": - CREATE PROCEDURE MergeXML
-
@VarID NVARCHAR(20),
-
@UpdateXML XML
-
AS
-
BEGIN
-
DECLARE @sqlCommands XML
-
SELECT @sqlCommands =
-
@UpdateXML.query('
-
<commands>
-
{
-
for $X in /info/@*
-
return <command>{concat("UPDATE ExampleXML SET Dat.modify('delete /info/@", local-name($X), "') WHERE ID='", sql:variable("@VarID"),"'")}</command>
-
}
-
</commands>
-
')
-
--DEBUGGING
-
PRINT CONVERT (NVARCHAR(2000), @sqlCommands)
-
-
--shred the <commands><command>... into a result/cursor
-
DECLARE stepper CURSOR FORWARD_ONLY READ_ONLY FOR
-
SELECT
-
CAST(CommandTable.CommandColumn.query('text()') AS NVARCHAR(2000))
-
FROM
-
@sqlCommands.nodes('commands/command') AS CommandTable(CommandColumn);
-
OPEN stepper
-
-
--EXEC the commands from the result/cursor
-
DECLARE @cmd NVARCHAR(2000)
-
FETCH NEXT FROM stepper INTO @cmd
-
WHILE @@FETCH_STATUS = 0 BEGIN
-
--DEBUGGING
-
PRINT 'cmd: ' + @cmd
-
EXEC (@cmd)
-
FETCH NEXT FROM stepper INTO @cmd
-
END
-
CLOSE stepper
-
DEALLOCATE stepper
-
-
--Build commands to insert given attributes
-
SELECT @sqlCommands =
-
@UpdateXML.query('
-
<commands>
-
{
-
for $X in /info/@*
-
return <command>{concat("UPDATE ExampleXML SET Dat.modify('insert (attribute ",local-name($X),"{"",data($X),""}) into (/info[1])') WHERE ID='", sql:variable("@VarID"), "'")}</command>
-
}
-
</commands>
-
')
-
--DEBUGGING
-
PRINT CONVERT (NVARCHAR(2000), @sqlCommands)
-
-
--shred the <commands><command>... into a result/cursor
-
DECLARE stepper CURSOR FORWARD_ONLY READ_ONLY FOR
-
SELECT
-
CAST(CommandTable.CommandColumn.query('text()') AS NVARCHAR(2000))
-
FROM
-
@sqlCommands.nodes('commands/command') AS CommandTable(CommandColumn);
-
OPEN stepper
-
-
--EXEC the commands from the result/cursor
-
FETCH NEXT FROM stepper INTO @cmd
-
WHILE @@FETCH_STATUS = 0 BEGIN
-
--DEBUGGING
-
PRINT 'cmd: ' + @cmd
-
EXEC (@cmd)
-
FETCH NEXT FROM stepper INTO @cmd
-
END
-
CLOSE stepper
-
DEALLOCATE stepper
-
-
END
-
Now, if I execute my stored procedure: - EXEC MergeXML '1', '<info FavoritePet="dog" Sex="Male" />'
Indeed it will merge "FavoritePet=do g" into ID=1 as well as change "Sex=Male" instead of "Unknown" and it leaves the existing "FavoriteCo lor" and "FavoriteWebsit e" attributes alone.
My question is:
Is there a better/faster way to do this? 100+ lines of a stored procedure to do this just seems awfully extreme to me. I am so new to XQuery and XPath et al. that I am very dangerous and I have a feeling that there should be a much more elegant solution but I cannot find anything on the Web.
As I stated, I am designing this database to handle user-selectable (i.e. unknown) fields - things like "Favorite Sausage" could be added in the future or who knows what. I am not stuck on using XML attributes, maybe I should use XML elements instead like: - <info>
-
<FavoriteColor>red</FavoriteColor>
-
<Sex>Male</Sex>
-
</info>
I am also considering a three-column table (ID, FieldName, FieldValue) but I am trying to get this XML idea to work. Given that I am so new at this, maybe I am trying to fit a square peg (the feature of supporting unknown future variables) into a round hole (using XML and attributes or elements in SQL Server 2005). I can still use a rational-database “three-column” method for storage (one record/row for each user-supplied parameter and keyed from the same ID) but pass the parameters via XML. I am guessing that this would make the stored procedure much smaller and probably faster but I am not sure how much faster.
0 3983 Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ken Varn |
last post by:
Sometimes when I try to close my managed C++ application, the following
dialog displays in Win 2000 Pro:
The title of the dialog is "Server Busy".
The message is "This action cannot be completed because the other program is
busy. Choose 'Switch to' to activate the busy program and correct the
problem."
I don't know why this is displayed. I would prefer to disable the display
of this message if possible. My app needs to be able to...
|
by: DarkSpy |
last post by:
many c++ compilers including "gcc" have not implemented the "export"
keyword, but the comeau compilers made it (just i knew).
i want to know about: is it too difficult to implement "export"
keyword? if it is, i know the history that is without "export" keyword
of C++ compilers with five years (sorry about my poor english :-) ),
in five years still have no some idea to implement it ?
my project and many C++ programmers need this important...
|
by: Markus Elfring |
last post by:
How much are you interested that the attribute "style" will offer the
same or similar capabilities like they are provided by the element
"style" in the element "head"?
Do you want that the document "Syntax of CSS rules in HTML's "style"
attribute" (http://www.w3.org/TR/css-style-attr) will become an
official recommendation in the near future?
Best regards,
Markus Elfring
|
by: John Baker |
last post by:
HI;
I feel like a fool..I put CUTE FTP in my last request for help --it should have been CUTE
PDF! I have FTP on my mind because I have been working on a web based application, and
somehow my brain got stuck on it..sorry!
Hi:
I need some help writing code that will "print" a report using Cute PDF.
|
by: Gomaw Beoyr |
last post by:
Two question about the "partial classes" (in the next wersion of
..NET).
Question 1
==========
Will partial classes (in the next version of C#) have to be
declared "partial" in ALL places.
I.e. do we have to need to write:
| |
by: John Smith |
last post by:
I have a project that consists of about a dozen translation
units. I use a command line compiler and it occured to me that I
could simplify compiling the project by #include(ing) them in a
header that looks something like this:
/* project.h */
#include "aaa.c"
#include "bbb.c"
#include "ccc.c"
....
|
by: eman1000 |
last post by:
I was recently looking at the prototype library
(http://prototype.conio.net/) and I noticed the author used the
following syntax:
Object.extend(MyObj.prototype, {
my_meth1: function(){},
my_meth2: function(){}
});
to define new methods on the MyObj prototype object. Object.extend
|
by: John Nagle |
last post by:
Here's a wierd problem:
I have a little test case for M2Crypto, which just opens up SSL connections to
web servers and reads their certificates. This works fine.
But if I execute
socket.setdefaulttimeout(5.0)
so that the sockets don't wait too long if there's no SSL server, I get
|
by: Elvis123 |
last post by:
I want to "merge" the results of two queries:
select DATE,CUSTOMER_ID from DB1
select DATE,CUSTOMER_ID from DB2
Then I want to be able to count the number of customers for each date in the "merged" result.
What is the best way to solve this?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |