MySQL存储过程中为什么这样写无法将记录数赋值给变量? declare nMax int;
select nMaxId = count(*) from 表; -- 无法
=======================================================
想类似的在MsSQL中就可以: declare @nMax int
select @nMax = count(*) from 表
select nMaxId = count(*) from 表; -- 无法
=======================================================
想类似的在MsSQL中就可以: declare @nMax int
select @nMax = count(*) from 表
declare nMax int;
select nMaxId := count(*) from 表;
declare nMax int;
select nMaxId := count(*) from 表;
这个我直接在workbench中好像不行:set @nMax = 0;
select count(*) into @nMax from tb;
select @nMax; -- 还是0
select @nMax := count(domain) as j from trade_domain_setting;
select @nMax;+-------------------------------------------------------------------+
测试了:ok!
说明在这个地址:http://www.cnblogs.com/qixuejia/archive/2010/12/21/1913203.html
应改为:set @nMaxId = 0;
select count(*) into @nMaxID from tb limit 1;select ... into语句
select col_name[,...] into var_name[,...] table_expr
这个select语法把选定的列直接存储到变量。因此,只有单一的行可以被取回。
查出来的结果是一个结果集,所以不能赋值给变量。
以下几种方式可以给变量赋值:
-- 方式 1
DECLARE cnt INT DEFAULT 0;
select count(*) into cnt from test_tbl;
select cnt;-- 方式 2
set @cnt = (select count(*) from test_tbl);
select @cnt;-- 方式 3
select count(*) into @cnt1 from test_tbl;
select @cnt1;-- 多个列的情况下似乎只能用 into 方式
select max(status), avg(status) into @max, @avg from test_tbl;
select @max, @avg;
用into 的好
:=这种在函数里面使用会报错。