goland使用mysql返回存储过程的使用

bobohume · · 2133 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

网上找了很多关于goland使用mysql返回存储过程带返回值的, golandgit上的mysql不支持存储过程在一个exec里面,只能一个exec,另一个query才能取到结果,这样就不能再高并发的情况下保证select取到的结果正确,为此实验了好多方法,最后只有这种方法,具体看图 goland mysql执行存储过程不支持多个返回集,用query在多个返回值的时候回取不到结果集 代码如下: rows, _ := db.Query(fmt.Sprintf("call usp_activeAccount('%s','%s')", "t22ss33t111", "123456")) for rows.Next() { var result string var accountid int rows.Scan(&result, &accountid) fmt.Println(result) fmt.Println(accountid) } 这样是可以取到结果集,但是必须usp_activeAccount这个存储过程 select返回只能有一个,不支持多个select结果集,具体看我的存储过程: ```sql CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_activeAccount`(in userid varchar(50), in password varchar(32)) begin set @accountid = -1; set @result = '0000'; if @result = '0000' and exists(select 1 from tbl_account A where A.accountName = userid) then set @result = '0002'; end if; if @result = '0000' then -- 开始事务 -- BEGIN if not exists(select @accountid:= A.accountid from tbl_account A where A.accountName = userid) then -- 由于go语言只支持一个select,所以只能用exist解决 -- select @accountid:= A.accountid from tbl_account A where A.accountName = userid -- if found_rows() = 0 then -- 开始插入帐号信息 insert into tbl_account(accountName,password) select userid,MD5(password); if row_count() = 1 then set @accountid = @@IDENTITY; else set @result = '0003'; end if; end if; if @result = '0000' THEN commit; else rollback; end if; end if; select @result, @accountid; end ```

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2133 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传