Archive for April 25th, 2011

SQLXML: Fun with SQLXML XQuery – Miscellaneous Functions – Part 1

Just going through some old notes; here are some samples I compiled before that might still be of interest to those of you who are venturing into SQLXML.

The samples below are using miscellaneous numeric and string functions.

Have fun!

Sample XML

SELECT @xml = '
<Employees Dept="IT">
  <Employee Number="1001" Hourly="11.35" FullName="Suzy Jacobs">
  <Employee Number="10" FullName="Bob Willow" ReportsTo="Steve Wong"/>
  <Employee Number="1003" FullName="Tony Carpenter" FName="Tony" LName="Carpenter"/>
  <Employee Number="1004" FullName="Suzy Jacobs" FName="Suzy" LName="Jacobs"/>
  <Staff Number="1003" FName="John"  FullName="John Smith"  LName="Smith" Department="ABC" />
  <MoreStaff Number="1020"  FullName="Ronnie Bates"  FName="Ronnie" LName="Bates" Address="XYZ" Department="ABC" />

Miscellaneous Functions 1

Using string-length, concat, contains, substring, ceiling, floor, round

<br />
-- miscellaneous functions 1
	-- length of "Suzy" 
	-- 4
	@xml.value('string-length((//Employee/FName/text())[1])', 'int') AS 'string-length()',
	-- Suzy===
	@xml.value('concat((//Employee/FName/text())[1], "===")', 'varchar(20)') AS 'concat()',
	-- note that concat is case sensitive
	-- 1
	@xml.value('contains((//Employee/FName/text())[1], "z")', 'bit') AS 'contains()',	
	-- no capital Z in Suzy
	-- 0
	@xml.value('contains((//Employee/FName/text())[1], "Z")', 'bit') AS 'contains()',
	-- 0
	@xml.value('contains((//Employee/FName/text())[1], "x")', 'bit') AS 'contains()',
	-- uz
	@xml.value('substring((//Employee/FName/text())[1], 2,2)', 'varchar(20)') AS 'substring()'


Miscellaneous Functions 2

Using min, max, avg, sum, lower-case, upper-case

-- miscellaneous functions 2
	-- 10
	@xml.value('min(//Employee/@Number)', 'int') AS 'min()',
	-- 1003
	@xml.value('max(//Employee/@Number)', 'int') AS 'max()',
	-- 671.33
	@xml.value('avg(//Employee/@Number)', 'decimal(10,2)') AS 'avg()',
	-- 2014.00
	@xml.value('sum(//Employee/@Number)', 'decimal(10,2)') AS 'sum()',
	-- min/max dont work on strings
	-- NULL
	@xml.value('min(//Employee/@FullName)', 'varchar(30)') AS 'min()',
	-- suzy jacobs
	@xml.value('lower-case((//Employee/@FullName)[1])', 'varchar(30)') AS 'lower-case()',
	@xml.value('upper-case((//Employee/@FullName)[1])', 'varchar(30)') AS 'upper-case()',
	-- value is 11.35
	-- 12
	@xml.value('ceiling((//Employee/@Hourly)[1])', 'int') AS 'ceiling()',
	-- value is 11.35
	-- 11
	@xml.value('floor((//Employee/@Hourly)[1])', 'int') AS 'floor()',
	-- value is 11.35
	-- 11
	@xml.value('round((//Employee/@Hourly)[1])', 'int') AS 'round()'


Getting distinct values

Using distinct-values

				distinct-values( data(//Employee/@FullName) )
			   ') as 'distinct-values()'	


VN:F [1.9.22_1171]
Rating: 10.0/10 (4 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)

Troubleshooting Replication Error: A required privilege is not held by the client

If you ever you encounter this replication issue, the resolution might be fairly simple, however it will require that you toggle your SQL Service accounts.

Have a look at this KB:

The KB suggests the service account might have changed using a tool other than the Configuration Manager (technically a no-no because the SSCM, in addition to changing the service accounts, also performs updates to associated settings like windows registry entries). Try toggling the service accounts in Configuration Manager (for ex, from Administrator, to something Local, to administrator again) and that should flush the old credentials.

Quote from KB:

To resolve the problem, follow these steps:

1. Set the SQL Server Agent service account in SQL Server Configuration Manager to the LocalSystem account.
2. Stop and then start the SQL Server Agent service.
3. Reset the SQL Server Agent service account in SQL Server Configuration Manager back to the original account.
4. Stop and then start the SQL Server Agent service.

Good luck!

VN:F [1.9.22_1171]
Rating: 8.5/10 (2 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)