|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, March 04, 2010 4:01 AM
Points: 4,
Visits: 11
|
|
Hi Jacob,
this is really a good article, very clear and friendly one.
Please tell me, where should I write this code to get the required results from XQuery?
In XML file? in some editor? processor?
Just for example, in order to run C# code I should write it in .cs file extenstion and build it with .NET compiler.... or JavaScript in .js file extention and run it in some browser... etc.
Thank you,
Yair
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
Hi Yair, All these are TSQL examples and hence you can run them in SQL Server Management Studio.
regards Jacob
.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, December 20, 2010 6:44 AM
Points: 28,
Visits: 26
|
|
Nice article.
Would it also be possible to look at the same processes where the XML data is arranged like...
<Employees> <Employee> <name>Jacob</name> <city>NY</city> <Team>SQL Server</team> </Employee> <Employee> <name>Steve</name> <city/> <Team>SQL Server</team> </Employee> <Employee> <name>Bob</name> <city>CA</city> <Team/> </Employee> </Employees> Which include blank elements. Have already been caught out with data like this needing insert/modify depending if a value exists
Thanks Andy
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, December 15, 2011 11:29 AM
Points: 1,
Visits: 7
|
|
In example 10 where you delete the “city” attribute where the “team” is “SQL Server” is there any way to pass in the “SQL Server” as a variable instead of using a constant?
Thanks, Marlin
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
mg (7/22/2009) In example 10 where you delete the “city” attribute where the “team” is “SQL Server” is there any way to pass in the “SQL Server” as a variable instead of using a constant?
Thanks, Marlin
Marlin, You can use a variable as given in the following example
DECLARE @x XML SELECT @x = ' <Employees> <Employee name="Jacob" city="NY" Team="SQL Server"/> <Employee name="Steve" city="FL" Team="SQL Server"/> <Employee name="Bob" city = "CA" Team="ASP.NET"/> </Employees>'
DECLARE @Team VARCHAR(20) SELECT @Team = 'SQL Server'
SET @x.modify(' delete (Employees/Employee[@Team=sql:variable("@Team")]/@city) ') SELECT @x /* <Employees> <Employee name="Jacob" Team="SQL Server" /> <Employee name="Steve" Team="SQL Server" /> <Employee name="Bob" city="CA" Team="ASP.NET" /> </Employees> */
.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
andrew.sims (7/22/2009)
Nice article. Would it also be possible to look at the same processes where the XML data is arranged like... <Employees> <Employee> <name>Jacob</name> <city>NY</city> <Team>SQL Server</team> </Employee> <Employee> <name>Steve</name> <city/> <Team>SQL Server</team> </Employee> <Employee> <name>Bob</name> <city>CA</city> <Team/> </Employee> </Employees> Which include blank elements. Have already been caught out with data like this needing insert/modify depending if a value exists Thanks Andy Hi Andy, It is possible by modifying the Xquery expression slightly, as given in the following example
DECLARE @x XML SELECT @x = ' <employees> <employee> <name>Jacob</name> <city>NY</city> <team>SQL Server</team> </employee> <employee> <name>Steve</name> <city>NY</city> <team>ASP.NET</team> </employee> </employees>'
DECLARE @Team VARCHAR(20) SELECT @Team = 'SQL Server'
SET @x.modify(' delete (employees/employee[team=sql:variable("@Team")]/city) ') SELECT @x /* <employees> <employee> <name>Jacob</name> <team>SQL Server</team> </employee> <employee> <name>Steve</name> <city>NY</city> <team>ASP.NET</team> </employee> </employees> */
.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, December 20, 2010 6:44 AM
Points: 28,
Visits: 26
|
|
Your a star !!! Just realy getting into this XML thing with SQL server, so examples like this are perfect. Andy
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
Glad to know it helped. I have written a series of XQuery tutorials at my blog site, in case you need further help.
regards Jacob
.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, March 04, 2010 4:01 AM
Points: 4,
Visits: 11
|
|
Hi Jacob,
It did work for me using the SQL query as you advised.
Look, according to your example, we are working against an XML variable, what about working against physical XML file? It's possible?
Yair
|
|
|
|