碰到一个需求,要对一个表批量修改字段。其中值需要通过拼接sql传参实现。经过参考后实现如下。
begin
declare total int;
declare _companyId int;
declare _lng double;
declare _lat double;
declare _town_name varchar(200);
declare _town_name_full varchar(200);
declare _areaId int;
declare done int default false;
declare v_sqltext varchar(2000);
declare curCompanyList cursor for select t.company_id,t.lng,t.lat from tb_company t where t.town_name is null and t.company_id > 18 ;
declare continue HANDLER for not found set done = true;
set total = 0;
open curCompanyList;
fetch curCompanyList into _companyId,_lng,_lat;
while(not done) do
set total = total + 1;
if _lng is not null then
begin
set v_sqltext = '';
SET v_sqltext = CONCAT('SELECT unique_id,ext_path,name into @uniqueid,@extpath,@name FROM areacity_tz WHERE ST_Intersects(POLYGON,ST_GeomFromText(\'POINT(',_lng,' ',_lat,')\',0)) = 1' );
set @sqltext = v_sqltext;
PREPARE stmt FROM @sqltext;
execute stmt;
deallocate prepare stmt;
update tb_company t set t.town_name = @name,t.town_name_full = @extpath where t.company_id = _companyId ;
end ;
end if;
set done = false;
fetch curCompanyList into _companyId,_lng,_lat;
end while;
close curCompanyList;
end
附参考资料内容乤:
p_cloumns varchar(500),p_tables varchar(100),p_where varchar(4000),p_order varchar(100),p_pageindex int,p_pagesize int,out p_recordcount int,out p_pagecount int
$:begin
declare v_sqlcounts varchar(4000);
declare v_sqlselect varchar(4000);
#拼接查询总记录的SQL语句
set v_sqlcounts = concat('select count(*) into @recordcount from ',p_tables,p_where);
#select v_sqlcounts;leave $;
set @sqlcounts = v_sqlcounts;
prepare stmt from @sqlcounts;
execute stmt;
deallocate prepare stmt;
#获取动态SQL语句返回值
set p_recordcount = @recordcount;
#根据总记录跳数计算出总页数
set p_pagecount = ceiling((p_recordcount+0.0)/p_pagesize);
if p_pageindex <1 then
set p_pageindex = 1;
elseif p_pageindex > p_pagecount and p_pagecount <> 0 then
set p_pageindex = p_pagecount;
end if;
#拼接分页查询记录的动态SQL语句
set v_sqlselect = concat('select ',p_cloumns,' from ',p_tables,p_where,if(p_order is not null,p_order,''),' limit ',(p_pageindex-1)*p_pagesize,' , ',p_pagesize);
#select v_sqlselect;leave $;
set @sqlselect = v_sqlselect;
prepare stmtselect from @sqlselect;
execute stmtselect;
deallocate prepare stmtselect;
end $
复制代码 代码如下:
#拼接查询总记录的SQL语句
set v_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);
set @sqlcounts := v_sqlcounts;
#预处理动态SQL
prepare stmt from @sqlcounts;
#传递动态SQL内参数
set @s1= categoryid;
execute stmt using @s1;
deallocate prepare stmt;
#获取动态SQL语句返回值
set recordcount = @recordcount;
#拼接查询总记录的SQL语句
set v_sqlcounts = concat('select count(*) into @recordcount from ',v_tables,v_where);
set @sqlcounts := v_sqlcounts;
#预处理动态SQL
prepare stmt from @sqlcounts;
#传递动态SQL内参数
set @s1= categoryid; execute stmt using @s1; deallocate prepare stmt;
#获取动态SQL语句返回值
set recordcount = @recordcount;
以上我上再做存储过程分页里用到动态SQL里将查询到的count记录条数通过变量@recordcount放到recordcount里面了。
mysql的IF ELSE和其他数据库的判断有点不一样,简单的判断语句如下。
复制代码 代码如下:
#根据总记录跳数计算出总页数
set pagecount = ceiling((recordcount+0.0)/pagesize);
if pageindex <1 then
set pageindex = 1;
elseif pageindex > pagecount then
set pageindex = pagecount;
else
select pageindex,pagecount;
end if;
#根据总记录跳数计算出总页数 set pagecount = ceiling((recordcount+0.0)/pagesize); if pageindex <1 then set pageindex = 1; elseif pageindex > pagecount then set pageindex = pagecount; else select pageindex,pagecount; end if;