存储过程样例



#-------in参数:条件---------------------
drop procedure if exists selectByID;
CREATE PROCEDURE `selectByID`(in _id int)
begin
select * from zt_bug where id=_id;
end;


call selectByID(10083);
#---------------------------------------




#------in out-----------------------
drop procedure if exists selectByID;
CREATE PROCEDURE `selectByID`(in _id int,out _put varchar(30))
begin
select title  into _put from zt_bug where id=_id;
end;


call selectByID(10083,@value);


select @value as '返回值';
#---------------------------------------




#-------in参数:列名 + 条件---------------------
CREATE PROCEDURE `testabc`(in _id int, in _col varchar(20))
begin
declare strSQL varchar(200);
declare sqlRun varchar(200);


set @strSQL = concat( 'select  ',_col, '  from zt_bug where id=',_id);
prepare sqlRun from @strSQL;
execute sqlRun;
end;


call testabc(10082,'title' )
#---------------------------------------




#-------in参数:表名 + 列名 + 条件---------------------
CREATE PROCEDURE `testabc`(in _tableName varchar(100), in _colName varchar(20),in _id int)
begin
declare strSQL varchar(200);
declare sqlRun varchar(200);
set @strSQL = concat( 'select  ',_colName , '  from ', _tableName,' where id=',_id);
prepare sqlRun from @strSQL;
execute sqlRun;
end;


call testabc('zt_bug','id',10082 )
#---------------------------------------




#-------out参数:sql---------------------
CREATE DEFINER=`root`@`localhost` PROCEDURE `testabc2`(out _value varchar(200))
begin
declare strSQL varchar(200);
declare sqlRun varchar(200);


select count(*) into _value from zt_bug;
end;


call testabc2(@name);
select @name;
#---------------------------------------




#-------out参数:concat ---------------------------
drop procedure if exists testOut;
CREATE PROCEDURE `testOut`(in _in varchar(20),out `_value` varchar(200))
begin
# set @stmt = concat('select  count(*)  into  @_total from information_schema.columns  where table_name=\'zt_bug\'');
set @stmt = concat('select  count(*)  into  @_total from information_schema.columns  where table_name="zt_bug"');
prepare sqlRun from @stmt;
execute sqlRun;
set _value =@_total;
DEALLOCATE PREPARE sqlRun;
end;


call testOut('tmp',@myvalue);
select @myvalue;
#---------------------------------------




#-----查询返回值------------------------
drop procedure if exists addBugCol;
create procedure addBugCol( 
in _tableName varchar(50),
in _columnName varchar(50)
)
begin
declare strSQL varchar(200);
declare sqlRun varchar(500);


select count(*)  into @_put  from INFORMATION_SCHEMA.COLUMNS where table_name =_tableName and column_name= _columnName ;
  select @_put;
  
end;


call addBugCol('zt_bugcus','id' );
#---------------------------------------




#-------查询构造的SQL---------------------------
drop procedure if exists testSQLSyntax;
create procedure testSQLSyntax( 
in _tableName varchar(50),
in _columnName varchar(50)
)
begin
declare strSQL varchar(200);
declare sqlRun varchar(500);
set @strSQL = "";
set @strSQL = concat('alter table  ', _tableName ,'  add column ', _columnName , ' varchar(50)' );
select @strSQL;
end;


call testSQLSyntax('zt_bugcus' ,'myCol' );
#---------------------------------------




#-------在表中不存在某列则增加---------------------------
drop procedure if exists colIsExist;
create procedure colIsExist( 
in _tableName varchar(50),
in _columnName varchar(50)
)
begin
declare strSQL varchar(200);
declare sqlRun varchar(500);


select count(*)  into @colName from INFORMATION_SCHEMA.COLUMNS where table_name =_tableName and column_name= _columnName ;


if (0 = @colName) then
set @strSQL = "";
set @strSQL = concat('alter table  ', _tableName ,'  add column ', _columnName , ' varchar(50)' );
prepare sqlRun from @strSQL;
execute sqlRun;
end if;
end;


call colIsExist('zt_bugcus','myNewCol' );


desc zt_bugcus
#---------------------------------------




#---传递关键字作为参数-----------
drop procedure if exists addBugCol;
create procedure addBugCol( 
in _tableName varchar(50),
in _columnName varchar(50)
)
begin
declare strSQL varchar(200);
declare sqlRun varchar(500);


select count(*)  into @_rowCount  from INFORMATION_SCHEMA.COLUMNS where table_name =_tableName and column_name= _columnName ;




if (0 = @_rowCount )then
set @strSQL = "";
set @strSQL = concat('alter table  ', _tableName ,'  add column ', _columnName , '  varchar(50)' );
prepare sqlRun from @strSQL;
execute sqlRun;
end if;
end;


#因为analyze为关键字,无法传递,需要如下操作
call addBugCol('zt_bugcus','`analyze`' );








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜕变之痛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值