This blog post came out of my work with packages for Umbraco CMS, but it's totally applicable even if you're not working with Umbraco. When building packages for Umbraco that works with custom tables you have to keep in mind that CMS supports different databases. We need to make sure that our queries works on both the standard SQL Server and with SQLCE.
I've decided to try to create a list of queries and map out what does and what doesn't work depending on the database type.
Description |
Query-example |
SQL Serv. |
SQLCE |
Regular SELECT |
SELECT * FROM umbracoNode |
x |
x |
SELECT with Subquery in select-statement |
SELECT id |
x |
|
SELECT with Subquery in WHERE-statement |
SELECT id, [text] |
x |
x |
SELECT with Subquery (scalar1) in WHERE |
SELECT id, [text] |
x |
|
SELECT with Inner Join |
SELECT n.id, n.[text] |
x |
x |
SELECT with STUFF-function |
SELECT id,[text], STUFF([text],1,1, '') |
x |
x |
SELECT TOP |
SELECT TOP 5 * FROM cmsContentType |
x |
x |
SELECT with “FOR XML PATH”. |
SELECT TOP 10 id,[text] |
x |
|
Row_Number() / Paging with |
SELECT id,[text], |
x |
|
Paging with |
SELECT id,[text] |
x |
x |
SELECT DISTINCT-feature |
SELECT DISTINCT ContentId from cmsContentVersion ORDER BY ContentId |
x |
x |
SELECT COUNT with DISTINCT3 |
SELECT COUNT(DISTINCT thumbnail) FROM cmsContentType |
x |
|
SELECT with LIKE in Where |
SELECT * |
x |
x |
SELECT with Subquery in FROM-statement. |
SELECT count(contentId) |
x |
x |
SELECT with LEFT OUTER JOIN |
SELECT n.id, n.[text], d.dbType, d.pk FROM umbracoNode as n |
x |
x |
Parameter Alias declared as string |
SELECT id as 'foo' FROM bar |
x |
|
Parameter Alias declared inline |
SELECT id as foo FROM bar |
X |
X |
SELECT with INNER JOIN Subquery in FROM |
SELECT * FROM cmsTemplate as t |
X |
X |
CROSS APPLY |
SELECT id,userName,ugr.Roles FROM umbracoUser u |
X |
X |
1. A scalar query is a query that returns one row consisting of one column.
2. Works on SQL Server 2012+, older versions need the ROW_NUMBER()
3. Another version of the query with a join in the FROM-caluse works on SQL CE: SELECT COUNT(thumbs.total) as Total FROM (SELECT DISTINCT thumbnail as total FROM cmsContentType) as thumbs
I hope that this little table is useful and please, feel free to drop a comment if you have any feedback or suggestions on things I've missed.