How to query a column name that includes a question mark character

479 views
Skip to first unread message

Martin Ingham

unread,
Apr 8, 2011, 7:12:49 AM4/8/11
to nhusers
I haven't been able to find the answer to this in any NHibernate
documentation. I am trying to write an HQL query with named
parameters. Unfortunately one of the column names includes a question
mark character in its name. In the mapping file I have wrapped the
column name in square brackets which works fine for ICriteria
queries. But when constructing the SQL statement NHibernate seems to
interpret the question mark as a position parameter placeholder even
though it's inside the square brackets, so it ends up substituting the
question mark with the parameter i.e. the query winds up containing
this column:
visit0_.[Fixed@p0]
whereas the column should be visit0_.[Fixed?]
Does anyone have any ideas how a question mark polluted column name
should be mapped so it is treated correctly in an HQL query?

Juan Carlos

unread,
Apr 11, 2011, 3:59:23 AM4/11/11
to nhu...@googlegroups.com
Did you try it with [`Fixed?`]

Kind regards

Martin Ingham

unread,
Apr 27, 2011, 7:06:29 AM4/27/11
to nhusers
I did, this does stop the parameter replacement but the SQL generated
is now looking for a column called 'Fixed?' including the single
quotes, which results in an error from the DB server (MS SQL Server
2008 R2). I had tried to get round the problem by requesting the
column be renamed to remove the question mark but this option is
proving to be more difficult than I'd anticipated.

Martin Ingham

unread,
Apr 27, 2011, 11:20:27 AM4/27/11
to nhusers
Sorry, just realized you specified backticks. Tried this also,
doesn't work.

Martin Ingham

unread,
Apr 28, 2011, 9:49:56 AM4/28/11
to nhusers
To clarify, this is the issue:

For this mapping:
<subclass name="Visit"> <property name="IsFixed" column="`Fixed?`" />
</subclass>
Running this query:
return _session.CreateQuery("select visit from Visit
visit").List<Visit>();
Generates this SQL:
select visit0_.[Fixed@p0] as Fixed14_22_ from dbo.tblVisits visit0_
Which results in SqlException Invalid column name 'Fixed@p0'.
This appears to be a bug in NHibernate 3.1, issue has been raised.
http://stackoverflow.com/questions/5805617/escaping-a-question-mark-character-in-a-column-name-in-nhibernate


On Apr 27, 4:20 pm, Martin Ingham <ingham.mar...@gmail.com> wrote:
> Sorry, just realized you specified backticks.  Tried this also,
> doesn't work.
>
> On Apr 27, 12:06 pm, Martin Ingham <ingham.mar...@gmail.com> wrote:
>
> > I did, this does stop the parameter replacement but the SQL generated
> > is now looking for a column called 'Fixed?' including the single
> > quotes, which results in an error from the DB server (MS SQL Server
> > 2008 R2).  I had tried to get round the problem by requesting the
> > column be renamed to remove thequestionmarkbut this option is
> > proving to be more difficult than I'd anticipated.
>
> > On Apr 11, 8:59 am, Juan Carlos <jcsegu...@gmail.com> wrote:
>
> > >    Did you try it with [`Fixed?`]
>
> > > Kind regards
>
> > > El 08/04/2011 13:12, Martin Ingham escribió:
>
> > > > I haven't been able to find the answer to this in any NHibernate
> > > > documentation.  I am trying to write an HQL query with named
> > > > parameters.  Unfortunately one of the column names includes aquestion
> > > >markcharacter in its name.  In the mapping file I have wrapped the
> > > > column name in square brackets which works fine for ICriteria
> > > > queries.  But when constructing the SQL statement NHibernate seems to
> > > > interpret thequestionmarkas a position parameter placeholder even
> > > > though it's inside the square brackets, so it ends up substituting the
> > > >questionmarkwith the parameter i.e. the query winds up containing
> > > > this column:
> > > > visit0_.[Fixed@p0]
> > > > whereas the column should be visit0_.[Fixed?]
> > > > Does anyone have any ideas how aquestionmarkpolluted column name
Reply all
Reply to author
Forward
0 new messages