-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: CreateSqlQuery(..) and AddScalar
PostPosted: Thu Sep 27, 2007 5:32 am 
Newbie

Joined: Wed Mar 14, 2007 8:40 am
Posts: 2
Hey all,

I'm trying to create a native sql query to get a summary of all hits for a company in a specified month. The table has the columns (ID, Company, Date).

I'm trying the following:


Code:
IList list = _session.CreateSQLQuery(
                    String.Format(
                            "SELECT Company, count(*) " +
                            "FROM History" +
                            "WHERE     (DATENAME(m, [Date]) = '{0}') AND (DATENAME(yy, [Date]) = '{1}') " +
                            "GROUP BY Company "
                           , month, year))
                    .AddScalar("Company", NHibernateUtil.String)
                    .AddScalar("Hits", NHibernateUtil.Int32)
                    .List();


I'm getting the exception "IndexOutOfRangeException: Hits". That means that somehow the scalar for hits cannot be resolved. The query works fine when I leave the second AddScalar("Hits", ..) out.

Does anyone know how I can get hold of the count(*)? Any help is greatly appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 28, 2007 5:27 am 
Newbie

Joined: Wed Mar 14, 2007 8:40 am
Posts: 2
Hi Folks,

solved my problem. In case anyone else is stuck in there as well, that's my solution: I simply forgot to set the alias for the count:

Code:
IList list = _session.CreateSQLQuery(
                    String.Format(
                            "SELECT Company, count(*) as Hits" +
                            "FROM History" +
                            "WHERE     (DATENAME(m, [Date]) = '{0}') AND (DATENAME(yy, [Date]) = '{1}') " +
                            "GROUP BY Company "
                           , month, year))
                    .AddScalar("Company", NHibernateUtil.String)
                    .AddScalar("Hits", NHibernateUtil.Int32)
                    .List();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.