Note that there are some explanatory texts on larger screens.

plurals
  1. POSubSonic 2.2 SqlQuery object generates very different sql for WHERE...IN statement for SQL Server 2008 and SQL Server 2005
    primarykey
    data
    text
    <p>Does anybody have an idea why SubSonic 2.2 SubSonic.SqlQuery object would be generating very different sql for the same C# code when running against SQL Server 2005 or SQL Server 2008? </p> <p>I have a site that's been running for a while on SubSonic 2.2/SQL Server 2005. I just upgraded the DB to mssql 2008 and am encountering the following error: </p> <blockquote> <p>SqlException (0x80131904): Incorrect syntax near the keyword 'AND'</p> </blockquote> <p>I've dumped the SqlQuery.ToString() at the point of failure and noticed the following differences between running the exact same codebase on SQL Server 2005 and SQL Server 2008. Here is the source code:</p> <pre><code>SubSonic.SqlQuery q = new Select() .From(Views.VwSearchIndexQuery2Mtx) .Paged(pageNumber, maximumRows) .Where(VwSearchIndexQuery2Mtx.Columns.SearchIndexQueryId) .In( new Select(SearchIndexQueryGroupMap.Columns.SearchIndexQueryId) .From(Tables.SearchIndexQueryGroupMap) .Where(SearchIndexQueryGroupMap.Columns.SearchIndexQueryGroupId) .IsEqualTo(searchIndexQueryGroupId)); </code></pre> <p>And the auto-generated sql for SQL Server 2005 is:</p> <pre><code> SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY CreatedOn DESC ) AS Row , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchTerms] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[IndustryId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[IndustryName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[DaysMonitored] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Incidents] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Relevance] , [dbo].[Vw_SearchIndexQuery2_Mtx].[CreatedOn] , [dbo].[Vw_SearchIndexQuery2_Mtx].[CreatedBy] , [dbo].[Vw_SearchIndexQuery2_Mtx].[ModifiedOn] , [dbo].[Vw_SearchIndexQuery2_Mtx].[ModifiedBy] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Deleted] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryTypeId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryTypeName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[LastUpdatedTime] FROM [dbo].[Vw_SearchIndexQuery2_Mtx] WHERE [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] IN (SELECT [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryId] FROM [dbo].[SearchIndexQueryGroup_Map] WHERE [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryGroupId] = @SearchIndexQueryGroupId0 ) ) AS PagedResults WHERE Row &gt;= 1 AND Row &lt;= 20 </code></pre> <p>The auto-generated sql for SQL Server 2008:</p> <pre><code>DECLARE @Page int DECLARE @PageSize int SET @Page = 1 SET @PageSize = 20 SET NOCOUNT ON -- create a temp table to hold order ids DECLARE @TempTable TABLE (IndexId int identity, _keyID Int) -- insert the table ids and row numbers into the memory table INSERT INTO @TempTable ( _keyID ) SELECT [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] FROM [dbo].[Vw_SearchIndexQuery2_Mtx] WHERE [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] IN (SELECT [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryId] FROM [dbo].[SearchIndexQueryGroup_Map] WHERE [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryGroupId] = @SearchIndexQueryGroupId0 ) /* it's at this next AND where the error is thrown */ AND [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] IN (SELECT [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryId] FROM [dbo].[SearchIndexQueryGroup_Map] AND [dbo].[SearchIndexQueryGroup_Map].[SearchIndexQueryGroupId] = @SearchIndexQueryGroupId0 ) ORDER BY CreatedOn DESC -- select only those rows belonging to the proper page SELECT [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchTerms] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[IndustryId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[IndustryName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[DaysMonitored] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Incidents] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Relevance] , [dbo].[Vw_SearchIndexQuery2_Mtx].[CreatedOn] , [dbo].[Vw_SearchIndexQuery2_Mtx].[CreatedBy] , [dbo].[Vw_SearchIndexQuery2_Mtx].[ModifiedOn] , [dbo].[Vw_SearchIndexQuery2_Mtx].[ModifiedBy] , [dbo].[Vw_SearchIndexQuery2_Mtx].[Deleted] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryTypeId] , [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryTypeName] , [dbo].[Vw_SearchIndexQuery2_Mtx].[LastUpdatedTime] FROM [dbo].[Vw_SearchIndexQuery2_Mtx] INNER JOIN @TempTable t ON [dbo].[Vw_SearchIndexQuery2_Mtx].[SearchIndexQueryId] = t._keyID WHERE t.IndexId BETWEEN ((@Page - 1) * @PageSize + 1) AND (@Page * @PageSize) </code></pre> <p>I know why the error is happening - the sql is invalid by the AND that I've commented above. I just can't figure out why SubSonic is generating invalid SQL after it worked on SQL Server 2008. You'll see that for SQL Server 2008 it uses a temp table and it also looks like it's repeating the WHERE...IN sub-query. I thought maybe it was the ISO Compatibility Level since the upgraded DB was set for 100. So I've tested with setting it to both 90 &amp; 80 and SubSonic generates the same sql as in each case. (And BTW, the code generated for SQL Server 2005 which uses "select rownumber() over... as row" executes fine against SQL Server 2008.)</p> <p>Does anyone have any ideas why this is happening and how to track it down?</p> <p>Many thanks,</p> <p>Terry</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload