分类: Mysql/postgreSQL
2008-11-29 14:52:32
动态插入数据的存储过程,(注意四个单引号表示一个一引号):
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertUser`(in name2 varchar(50),in password2 varchar(32),in age2 int,in adddate2 datetime)
begin
DECLARE stmt varchar(2000);
set @sqlstr=concat('insert into users(name,password,age,adddate) values(');
set @sqlstr=concat(@sqlstr,'''',name2,'''',',','''',password2,'''',',',age2,',','''',adddate2,'''',')');
prepare stmt from @sqlstr;
execute stmt;
end;
()
另一种带参数的形式:
在mysql存储过程中动态执行sql文
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET @a = 3;
mysql> SET @b = 4;
mysql> EXECUTE stmt1 USING @a, @b;
mysql> SET @a = 'select * from table1 ';
mysql> SET @b = 'where column1>10 limit 1';
mysql> SET @a =
concat(@a,@b);
mysql> PREPARE stmt1 FROM
@a;
mysql> EXECUTE stmt1;