mysql procedure返回多数据集

返回多数据集写法:
第一种

DROP PROCEDURE IF EXISTS `p_query_user` ;
DELIMITER //
    CREATE PROCEDURE p_query_user(  p_inout_user_id  INT   )
    begin
    declare c varchar(100) default ‘0‘;
         SELECT * FROM T_SD_USER WHERE USER_ID = p_inout_user_id ;
         SET p_inout_user_id =2222;
end //
 DELIMITER ;
set @uId = 3000 ;
CALL p_query_user(@uId );
SELECT @uId ;

第二种

mysql>delimiter //
mysql>drop procedure if exists proc;
mysql>create procedure proc()
begin
create temporary table tmp1 like t;
insert into tmp1 select * from t;
create temporary table tmp2 like t;
insert into tmp2 select * from t;
end;
//
mysql>delimiter ;
mysql>select * from tmp1;
mysql>select * from tmp2; 

注:使用临时表在session级一直可用,再次调用存储过程需要先DROP临时表。

hibernate call

@Test
    public void test0() {
        try {

            String procedureSql = "CALL p_query_user( :userId)";
            SQLQuery sqlQuery = t.getSession().createSQLQuery(procedureSql)
                    .addEntity(TSdUser.class);
            Object userId = 3000;
            sqlQuery.setParameter("userId", userId);
            List<TSdUser> result = sqlQuery.list();
            for (TSdUser tSdUser : result) {
                System.out.println(tSdUser);
            }
            System.out.println(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

结果如下:

Hibernate: CALL p_query_user( ?)TSdUser [userSysMlb=100.0, userUseredMlb=633137.0, userDoDiscoverCount=0, userDoCommonsSayCount=0, createTime=2014-05-08 12:20:35.0, lastActivateSystemMsgTime=2014-07-17 12:10:00.0, lastActivateSusCommentMsgTime=2014-05-08 12:20:35.0, lastActivateNegativeMsgTime=2014-06-17 10:27:37.0, lastActivatePraiseMsgTime=2014-05-08 12:20:35.0, lastActiveTime=2014-08-11 18:45:52.0, priMsgTime=2014-08-09 14:30:27.0, lockTime=null, userMark=null, userSubMark=null, userRemark=null, userInfo=null, userLoginIp=127.0.0.1, userLogginPhoneCode=13724770797, userRegisterInfo=null, userRealName=null, userRegisterPhoneCode=13724770797, userRegistrationCity=null, userPhoneCity=湛江, userDeviceType=Android, userLoginQqId=1, userLoginWeiXinId=null, userLoginSinaBlogId=null, userWeiXinOpenId=111, cteateType=1, restaurantWeiXinOpenId=222, weiXinSubscribeTime=2014-05-17 13:29:28.0, weiXinSubscribeStatus=SUBSCRIBE, weiXinUnSubscribeTime=null, userId=3000, userName=MJorcen, userTotalMlb=1.1111111E7, userDoAttentionCount=0, userReAttentionCount=0, userDoPraiseCount=0, userRePraiseCount=11, userDoNegativeCount=0, userReNegativeCount=6, userDoShowDetailCount=38, userDoGetVoucherCount=5, userDoUsedVoucherCount=2, userPhone=13724770797, userGender=G, userGrade=0, userHeader=null]

或者:

    @Test
    public void test1() {
        try {

            String procedureSql = "CALL p_query_user(?)";
            Connection connection = t.getSession().connection();
            Object userId = 3000;
            java.sql.CallableStatement cs = connection
                    .prepareCall(procedureSql);
            cs.setInt(1, 3000);

            cs.registerOutParameter("p_inout_user_id", Types.INTEGER);
            cs.execute();
            int patams1 = cs.getInt(1);
            ResultSet resultSet = cs.getResultSet();
            while (resultSet.next()) {
                System.out.println(resultSet.getInt("USER_ID"));
            }
            System.out.println(patams1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

一个网上的例子;

public String callFirstProcedures(final long qyid, final int pdys) {
    String procedureSql = "{call get_qyaqdcpdjb(?,?,?)}";
    Object pj = jdbcTemplate.execute(procedureSql,
            new CallableStatementCallback() {
                public Object doInCallableStatement(CallableStatement cs)
                        throws SQLException, DataAccessException {
                    cs.setLong(1, qyid);
                    cs.setInt(2, pdys);
                    //你需要注册一个输出参数
                    cs.registerOutParameter(3, Types.VARCHAR);
                    cs.execute();
                    return cs.getString(3);
                }
            });
    return HtmsUtils.formatObject(pj);
}

mysql procedure返回多数据集

时间: 2024-10-28 11:26:37

mysql procedure返回多数据集的相关文章

jdbc 处理mysql procedure返回的多个结果集

1:测试数据库表user mysql> desc user$$ +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_in

OpenLayers添加点【php请求MySQL数据库返回GeoJSON数据】

php请求MySQL数据库返回GeoJSON数据的实现方法请参见: http://www.cnblogs.com/marost/p/6234514.html OpenLayers[v3.19.1-dist]添加GeoJSON数据源图层 <!doctype html> <html lang="en"> <head> <link rel='stylesheet' href='ol.css'> <style> #map { heig

python mysql 查询返回字典结构

cur = self.conn.cursor(MySQLdb.cursors.DictCursor)加上MySQLdb.cursors.DictCursor可以返回字典结构{列名:值} class MYSQL(): def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def __GetConnect(self): """ 得到连接信

PHP 把返回的数据集转换成Tree树

/** * 把返回的数据集转换成Tree * @access public * @param array $list 要转换的数据集 * @param string $pid parent标记字段 * @param string $level level标记字段 * @return array */ public static function list_to_tree($list, $pk='cat_id',$pid = 'parent_id',$child = '_child',$root=

关于spring jdbc 查询mysql数据库时,数据集字段名有别名返回的不是别名而是原始字段名

select语句字段含有别名 eg:select xm fullname from t_user; JdbcTemplate查询返回的结果集 xm '张三' 'xxx' 'xxxx' 期待结果是: fullname '张三' 'xxx' 'xxx' 解决办法:在数据库链接url后面跟上?useOldAliasMetadataBehavior=true

mybatis调用mysql存储过程返回结果集

存储过程中经常需要返回结果集. Mysql 中直接用 select 即可返回结果集.而 oracle 则需要使用游标来返回结果集.这一点 Mysql 相对比较方便,如下代码即可实现输出结果集: 存储过程定义: DELIMITER $$ DROP procedure IF EXISTS pro_sql_data1 $$ CREATE procedure pro_sql_data1(in sear_name varchar(2000)) BEGIN if sear_name is not null

python 调用mysql存储过程返回结果集

存储过程: delimiter | create procedure get_product_info(in imid int(10),int iuser varchar(20)) begin select * from tb_test where mid = imid and user = iuser; end; | delimiter ; python调用: import mysql.connector cnx = mysql.connector.connect( user='test',p

mybatis调用mysql存储过程返回多结果集(完整)

最近,有个开发提了个需求,希望中间件支持调用mysql存储过程时支持多结果集返回,因为某些原因我们使用了不少的存储过程,很多复杂的逻辑目前来看交互非常的多,所以从当前的现状来说,这个需求还是蛮合理的.中午抽空特地搜了下,整合完整示例如下: 1.创建测试存储过程 delimiter $$ CREATE PROCEDURE sp_multi_resultset(IN p_operator_company_no int, IN p_operator_no int, OUT p_error_code v

mysql中返回当前时间的函数或者常量

引用:http://blog.sina.com.cn/s/blog_6d39dc6f0100m7eo.html 1.1 获得当前日期+时间(date + time)函数:now() 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数: current_timestamp()   current_timestamp localtime()   localtime localtimestamp()   localtimestamp     这些日期时间函数,都等同于 now(