I upgraded from NH 4.0.3 to NH 4.1.0 and started getting invalid SQL errors. I'm unable to reproduce the error: it seems to happen randomly (but multiple times per day), so it's most likely related to caching of the query. I've never gotten this error with 4.0.3. I have downgraded to 4.0.3 until the issue is fixed.
Here's the stack trace:
As it says, the problem is
which is not valid SQL. If you remove @p2 from there it's fine.
The query is very much dynamically generated from multiple expressions. Here's the C# code for the part causing the error:
I got similar SQL errors from multiple other queries, not just this one.
SQL Server 2012, Windows Server 2012 R2 64-bit