mysql 不支持 表备份形式的 select into ,只能into 到一个文本文件中。
在存储过程中可以用:
select count(1) into TotalRecord from t_zhangdan;
select @TotalRecord;
将select 结果 into到一个变量中。但是,写法不同。如下:
CREATE DEFINER=`root`@`%` PROCEDURE `Proce_CommonPaging`(
in TableName varchar(50), #表名
in ReFieldsStr varchar(200), #字段名(全部字段为*)
in WhereString varchar(500), #条件语句(不用加where)
in OrderString varchar(200), #排序字段(必须!支持多字段不用加order by)
in PageSize int, #每页多少条记录
in PageIndex int, #指定当前为第几页
out TotalRecord int #返回总记录数
in TableName varchar(50), #表名
in ReFieldsStr varchar(200), #字段名(全部字段为*)
in WhereString varchar(500), #条件语句(不用加where)
in OrderString varchar(200), #排序字段(必须!支持多字段不用加order by)
in PageSize int, #每页多少条记录
in PageIndex int, #指定当前为第几页
out TotalRecord int #返回总记录数
)
BEGIN
Declare TotalCountSql varchar(500);
SET @TotalCountSql= 'select count(1) into TotalRecord from t_zhangdan;';
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
DEALLOCATE PREPARE stmtC;
select @TotalRecord;
BEGIN
Declare TotalCountSql varchar(500);
SET @TotalCountSql= 'select count(1) into TotalRecord from t_zhangdan;';
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
DEALLOCATE PREPARE stmtC;
select @TotalRecord;
Error Code: 1327. Undeclared variable: TotalRecord
将上面的改一下就行了:
SET @TotalCountSql= CONCAT('select count(1) into @TotalRecord1 from ' ,TableName);#总记录数语句
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
set TotalRecord = @TotalRecord1;
DEALLOCATE PREPARE stmtC;
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
set TotalRecord = @TotalRecord1;
DEALLOCATE PREPARE stmtC;
或者:
SET @TotalCountSql= CONCAT('select username from ' ,TableName);#总记录数语句
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
set TotalRecord = FOUND_ROWS();
PREPARE stmtC FROM @TotalCountSql;
EXECUTE stmtC;
set TotalRecord = FOUND_ROWS();