mysql存储过程默认值
你磨枯可以这样写
exec
Insert_course
@course_no='02005',@course_name='新新课',@course_score='2'
或者不带参数名
exec
Insert_course
'02005'旦游顷,'新新课'
因为只要一个参数带参数名称则其后面所有的参模陆数都必须带上参数名。
‘贰’ mysql存储过程的基本用法有哪些
mysql存储过程的基本用法有哪些
在外部程序访问数据库时(例如 php),要组织很多 SQL 语句。
特别是业务逻辑复杂的时候,一大堆的 SQL 和条件夹杂在 PHP 代码中,让人不寒而栗。现在有了 MySQL 存储过程,业务逻辑可以封装存储过程中,这样不仅容易维护,而且执行效率也高。
一、MySQL 创建存储过程
"pr_add" 是个简单的 MySQL 存储过程,这个MySQL 存储过程有两个 int 类型的输入参数 "a"、"b",返回这两个参数的和。
复制代码 代码如下:
drop procere if exists pr_add;
计算两个数之和
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
declare c int;
if a is null then
set a = 0;
end if;
if b is null then
set b = 0;
end if;
set c = a + b;
select c as sum;
/*
return c;
不能在 MySQL 存储过程中使用。return 只能出现在函数中。
*/
end;
二、调用 MySQL 存储过程
复制代码 代码如下:
call pr_add(10, 20);
执行 MySQL 存储过程,存储过程参数为 MySQL 用户变量。
复制代码 代码如下:
set @a = 10;
set @b = 20;
call pr_add(@a, @b);
三、MySQL 存储过程特点
创建 MySQL 存储过程的简单语法为:
复制代码 代码如下:
create procere 存储过程名字()
(
[in|out|inout] 参数 datatype
)
begin
MySQL 语句;
end;
MySQL 存储过程参数如果不显式指定"in"、"out"、"inout",则默认为"in"。习惯上,对于是"in" 的参数,我们都不会显式指定。
1. MySQL 存储过程名字后面的"()"是必须的,即使没有一个参数,也需要"()"
2. MySQL 存储过程参数,不能在参数名称前加"@",如:"@a int"。下面的创建存储过程语法在 MySQL 中是错误的(在 SQL Server 中是正确的)。 MySQL 存储过程中的变量,不需要在变量名字前加"@",虽然 MySQL 客户端用户变量要加个"@"。
复制代码 代码如下:
create procere pr_add
(
@a int, -- 错误
b int -- 正确
)
3. MySQL 存储过程的参数不能指定默认值。
4. MySQL 存储过程不需要在 procere body 前面加 "as"。而 SQL Server 存储过程必须加 "as" 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
as -- 错误,MySQL 不需要 "as"
begin
mysql statement ...;
end;
5. 如果 MySQL 存储过程中包含多条 MySQL 语句,则需要 begin end 关键字。
复制代码 代码如下:
create procere pr_add
(
a int,
b int
)
begin
mysql statement 1 ...;
mysql statement 2 ...;
end;
6. MySQL 存储过程中的每条语句的末尾,都要加上分号 ";"
复制代码 代码如下:
...
declare c int;
if a is null then
set a = 0;
end if;
...
end;
7. MySQL 存储过程中的注释。
复制代码 代码如下:
/*
这是个
多行 MySQL 注释。
*/
declare c int; -- 这是单行 MySQL 注释 (注意 -- 后至少要有一个空格)
if a is null then # 这也是个单行 MySQL 注释
set a = 0;
end if;
‘叁’ mysql存储过程中为什么可以定义一个int类型变量的默认值为false
false跟0等价,false实际上就是0
‘肆’ mysql 存储过程
你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难
有看不懂的地方一起讨论 :
begin
declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;
declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if;
create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;
set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod order by ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;
if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;
until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;
UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;
end
‘伍’ mysql 存储过程中变量的定义与赋值操作
一、变量的定义
mysql中变量定义用declare来定义一局部变量,该变量的使用范围只能在begin...end
块中使用,变量必须定义在复合语句的开头,并且是在其它语句之前,也可以同时申明多个变量,如果需要,可以使用default赋默认值。
定义一个变量语法如下:
declare
var_name[,...]
type[default
value]看一个变量定义实例
declare
last
date;二、mysql存储过程变量赋值
变量的赋值可直接赋值与查询赋值来操作,直接赋值可以用set来操作,可以是常量或表达式如果下
复制代码
代码如下:
set
var_name=
[,var_name
expr]...给上面的last变量赋值方法如下
set
last
=
date_sub(
current_date(),interval
1
month);下面看通过查询给变量赋值,要求查询返回的结果必须为一行,具体操作如下
select
col
into
var_name[,...]
table_expr我们来通过查询给v_pay赋值。
create
function
get
_cost(p_custid
int,p_eff
datetime)
return
decimal(5,2)
deterministic
reads
sql
data
begin
declare
v_pay
decimail(5,2);
select
ifnull(
sum(pay.amount),0)
into
vpay
from
payment
where
pay.payd<=p_eff
and
pay.custid=pid
reutrn
v_rent
+
v_over
-
v_pay;
end
$$
好了,这篇简单的存储过程中变量的定义赋值教程就到这里了,下面我们会接着讲关于myql存储过程的条件的定义与处理。
以下是其它网友的补充
在MySQL的存储过程中,可以使用变量,它用于保存处理过程中的值。
定义变量使用DECLARE语句,语法格式如下:
DECLARE
var_name[,...]
type
[DEFAULT
value]
其中,var_name为变量名称,type为MySQL支持的任何数据类型,可选项[DEFAULT
value]为变量指定默认值。一次可以定义多个同类型的变量,各变量名称之间以逗号“,”隔开。
定义与使用变量时需要注意以下几点:
◆
DECLARE语句必须用在DEGIN…END语句块中,并且必须出现在DEGIN…END语句块的最前面,即出现在其他语句之前。
◆
DECLARE定义的变量的作用范围仅限于DECLARE语句所在的DEGIN…END块内及嵌套在该块内的其他DEGIN…END块。
◆
存储过程中的变量名不区分大小写。
定义后的变量采用SET语句进行赋值,语法格式如下:
SET
var_name
=
expr
[,var_name
=
expr]
...
其中,var_name为变量名,expr为值或者返回值的表达式,可以使任何MySQL支持的返回值的表达式。一次可以为多个变量赋值,多个“变量名=值”对之间以逗号“,”隔开。
例如:
复制代码
代码如下:
begin
declare
no
varchar(20);
declare
title
varchar(30);
set
no='101010',title='存储过程中定义变量与赋值';
end
提示:存储过程中所有的关键字也是不区分大小写的,如BEGIN可以写出begin。
‘陆’ mysql 存储过程
.关于MySQL的存储过程
存储过程是数据库存储的一个重要的功能,但是MySQL在5.0以前并不支持存储过程,这使得MySQL在应用上大打折扣。好在MySQL 5.0终于开始已经支持存储过程,这样即可以大大提高数据库的处理速度,同时也可以提高数据库编程的灵活性。
MySQL存储过程的创建
(1).格式
MySQL存储过程创建的格式:CREATE PROCEDURE过程名([过程参数[,...]])
[特性...]过程体
这里先举个例子:
mysql>DELIMITER//
mysql>CREATEPROCEDUREproc1(OUTsint)
->BEGIN
->SELECTCOUNT(*)INTOsFROMuser;
->END
->//
mysql>DELIMITER;
- 注:
(1)这里需要注意的是DELIMITER //和DELIMITER ;两句,DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。
(2)存储过程根据需要可能会有输入、输出、输入输出参数,这里有一个输出参数s,类型是int型,如果有多个参数用","分割开。
(3)过程体的开始与结束使用BEGIN与END进行标识。
‘柒’ mysql为什么要指定存储过程特性
MySQL的存储过程需要与MSSQL或Oracle的存储行亏过程相比。如没有显式指定,会隐含地指滚液定一系列特性(characteristic)的默认档备神值来创建存储过程。
‘捌’ mysql存储过程,参数默认值如何写法
以下只是模启老个例子
CREATE procere test
@n1 char(10),
@n2 char(10),
@n3 char(10),
@n4 char(10),
@n5 char(10),
@n6 char(10),
@n7 char(10),
@n8 datetime,
@n9 datetime,
@n10 int
as
select * --------这旁吵部分请将传入的参旦升数带入查询的条件中
from table1 t1,table2 t2,table3 t3
where deldate between @n8 and @n9
and t1.ponum = t2.ponum
and t2.partnum = t3.partnum
and t3.layer =@n1
GO
‘玖’ mysql 存储过程如何声明变量
DECLARE var_name[,...] type [DEFAULT value]
这个语句被用来声明局部变量。
要给变量提供一个默认值,请包含一个DEFAULT子句。
值可以被指定为一个表达式,不需要为一个常数。
如果没有DEFAULT子句,初始值为NULL。
局部变量的作用范围在它被声明的BEGIN ... END块内。
它可以被用在嵌套的块中,除了那些用相同名字声明变量的块。
‘拾’ mysql存储过程不等于怎么写
一、多数指令是相同的,包括创建和修正存储过程的指令。
二、很多细微的指令有不同,具体如下(不仅):
1 mysql支持enum,和set类型,sql server不支持
2 mysql不支持nchar,nvarchar,ntext类型
3 mysql的递增语句是AUTO_INCREMENT,而mssql是identity(1,1)
MYSQL:create table basic(id int key auto_increment,name varchar(20));
MSSQL: create table basic(id int identity(1,1) , name varchar(20))
4 msms默认到处表创建语句的默认值表示是((0)),而在mysql里面是不允许带两括号的
5 mysql需要为表指定存储类型
6 mssql识别符是[],[type]表示他区别于关键字(可选用来包含表名、字段名),但是mysql却是 `(重音符,也就是按键1左边的那个符号)
7 mssql支持getdate()方法获取当前时间日期,但是mysql里面可以分日期类型和时间类型,获取当前日期是cur_date(),当前完整时间是 now()函数
8 mssql不支持replace into 语句,但是在最新的sql20008里面,也支持merge语法
9 mysql支持insert into table1 set t1 = ‘’, t2 = ‘’ ,但是mssql不支持这样写
10 mysql插入多行支持这样写 insert into tabl1 values (1,1), (1,1), (1,1), (1,1), (1,1), (1,1), (1,1) MSSQL不支持
11 mssql不支持limit语句,是非常遗憾的,只能用top 取代limt 0,N,row_number() over()函数取代limit N,M
12 mysql在创建表时要为每个表指定一个存储引擎类型,而mssql只支持一种存储引擎
13 mysql不支持默认值为当前时间的datetime类型(mssql很容易做到),在mysql里面是用timestamp类型
14 mssql里面检查是否有这个表再删除,需要这样:
if
exists (select * from dbo.sysobjects where id =
object_id(N'uc_newpm') and OBJECTPROPERTY(id, N'IsUserTable')
= 1)
但是在mysql里面只需要 DROP TABLE IF EXISTS cdb_forums;
15 mysql支持无符号型的整数,那么比不支持无符号型的mssql就能多出一倍的最大数存储
16 mysql不支持在mssql里面使用非常方便的varchar(max)类型,这个类型在mssql里面既可做一般数据存储,也可以做blob数据存储
17
mysql创建非聚集索引只需要在创建表的时候指定为key就行,比如:KEY displayorder (fid,displayorder)
在mssql里面必须要:create unique nonclustered index
index_uc_protectedmembers_username_appid on dbo.uc_protectedmembers
(username asc,appid asc)
18 mysql text字段类型不允许有默认值
19mysql的一个表的总共字段长度不超过65XXX。
20一个很表面的区别就是mysql的安装特别简单,而且文件大小才110M(非安装版),相比微软这个庞然大物,安装进度来说简直就是.....
21mysql的管理工具有几个比较好的,mysql_front,和官方那个套件,不过都没有SSMS的使用方便,这是mysql很大的一个缺点。
22mysql的存储过程只是出现在最新的版本中,稳定性和性能可能不如mssql。
23 同样的负载压力,mysql要消耗更少的CPU和内存,mssql的确是很耗资源。
24php连接mysql和mssql的方式都差不多,只需要将函数的mysql替换成mssql即可。
25mysql支持date,time,year类型,mssql到2008才支持date和time。
26变量赋值
MYsql:变量赋值SELECT @min_price:=MIN(price),@max_price:=MAX(price) FROM shop
MSsql:变量赋值SELECT @min_price=MIN(price),@max_price=MAX(price) FROM shop
三、总的来说,如果是简单的存储过程代码,要修改,不会太难。如果是复杂的系统,要移植会很难很难。