hibernate Ö´ÐÐsql ²éѯ³ö×ÜÊý
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class GetCountForPage extends HibernateDaoSupport{
public List getCountForPage(final String sql){
List list = getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Connection CurConn =session.connection();
PreparedStatement ps = CurConn.prepareStatement(sql);
ResultSet result=ps.executeQuery();
result.next();
int sum=result.getInt(1);
List list=new ArrayList();
list.add(sum);
result.close();
ps.close();
CurConn.close();
return list;
}
});
return list;
}
}
Ö´ÐÐsql £ºselect count(1) from yourtable; ²éѯ³ö×ÜÊý¿
Ïà¹ØÎĵµ£º
--1. ´´½¨±í£¬Ìí¼Ó²âÊÔÊý¾Ý
CREATE TABLE tb(id int, [value] varchar(10))
INSERT tb SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'
--SELECT * from tb
/**//*
id value
----------- ----------
1 aa
1 ......
Ò»¡¢SQL ServerÖÐÊý¾ÝÐеĴ洢·½Ê½
ÔÚSQL ServerÖдæ·ÅÊý¾ÝµÄÎļþ»áÒÔ8KBµÄ´óС·ÖÒ³¡£Ã¿Ò»Ò³¿ÉÒÔÊÇÊý¾Ý¡¢Ë÷ÒýÒÔ¼°ÆäËûSQL ServerÊý¾Ý¿âÐèҪΪÆäά»¤Êý¾ÝÎļþµÄÊý¾ÝÀàÐÍ¡£´ó¶àÊýÒ³ÊÇÊý¾ÝÒ³»òË÷ÒýÒ³¡£Ò³ÊÇSQL Server¶Á¡¢Ð´Êý¾ÝÎļþµÄµ¥Ôª¡£Ã¿Ò»Ò³Ö»°üÀ¨Ò»¸ö¶ÔÏóµÄÊý¾Ý»òË÷ÒýÐÅÏ¢£¬ËùÒÔÔÚÿһ¸öÊý¾Ý ......
distinctÕâ¸ö¹Ø¼ü×ÖÓÃÀ´¹ýÂ˵ô¶àÓàµÄÖظ´¼Ç¼ֻ±£ÁôÒ»Ìõ£¬µ«ÍùÍùÖ»ÓÃËüÀ´·µ»Ø²»Öظ´¼Ç¼µÄÌõÊý£¬¶ø²»ÊÇÓÃËüÀ´·µ»Ø²»ÖؼǼµÄËùÓÐÖµ¡£ÆäÔÒòÊÇdistinctÖ»ÓÐÓöþÖØÑ»·²éѯÀ´½â¾ö£¬¶øÕâÑù¶ÔÓÚÒ»¸öÊý¾ÝÁ¿·Ç³£´óµÄÕ¾À´Ëµ£¬ÎÞÒÉÊÇ»áÖ±½ÓÓ°Ï쵽ЧÂʵġ£
ÏÂÃæÏÈÀ´¿´¿´Àý×Ó£º
table±í
×Ö¶Î1 ×Ö¶ ......
×÷ÕߣºÇñÑó
QQ£º1964477
ËäÈ»ÔÚhibernateÖÐÓÐshow_sqlÑ¡Ïµ«ÊÇÏÔʾ³öÀ´µÄÓï¾ä´ó¶àÀàËÆ
select * from xxx where value=?
µ«ÊÇÓÐʱºòÎÒÃÇÐèÒªµÃµ½ÍêÕûµÄSQLÓï¾ä£¬Ôõô°ìÄØ£¿Ê¹ÓÃP6SPY¾Í¿ÉÒÔÍê³ÉÕâ¸öÈÎÎñ
p6spyÊÇÒ»¸ö¿ªÔ´Èí¼þ£¬Ëü¿ÉÒÔ¸ú ......
sql server ÁÙʱ±íµÄʹÓà ÒÔ¼°¸øÁÙʱ±í²åÈëÖ÷¼üÁÐ
--ijʱ¼ä¶ÎÄÚij×é·þÎñÆ÷µÄÏû·ÑÅÅÐÐ
CREATE PROCEDURE biling_order_consume
@begin_date datetime,
@end_date datetime
AS
select identity(int,1,1) as ID,charge_credit into #tmp from crm_biling
where @begin_date <= convert(datetime,convert( ......