mysql存储过程null
‘壹’ mysql存储过程算成绩总分,sql语句没错,为什么总成绩为null
题主还是完善一下问题,语句没有错,只能说语法检查没有错,并不代表逻辑上没有错
建议你还是按照逻辑逐一检查排查问题
祝好运,望采纳
‘贰’ mysql存储过程问题 存储过程中有这样一条语句,例INSERT INTO tbl1 values(null,xxx,xxx);其中tbl1是一个
这种情况需要用到预处理。将表名作为变量传递进来,如叫v_tablename
set @sql = concat('INSERT INTO ', v_tablename,' values(null, xxx, xxx)');
prepare s from @sql;
EXECUTE s ;
DEALLOCATE PREPARE s;
这样表名那里就可以作为变量了。
‘叁’ 关于mysql 的存储过程
mysql> delimiter //
这个作用是把;变成//,以后的语句遇到//就结束了,遇到;不结束,下面就可以按你的想法写了
 
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
    -> BEGIN
    ->   SELECT COUNT(*) INTO param1 FROM t;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)
 
mysql> delimiter ; 这里把双引号改回来
声明参数要在说明是 输入还是输出函数 in\out
给函数变量赋值用
set @a=10;
例子:
drop procere if exists pr_param_in;
create procere pr_param_in
(
   in id int -- in 类型的 MySQL 存储过程参数
)
begin
   if (id is not null) then
      set id = id + 1;
   end if;
   select id as id_inner;
end;
set @id = 10;
call pr_param_in(@id);
select @id as id_out;
mysql> call pr_param_in(@id);
‘肆’ 关于mysql存储过程的问题
下面是一个最简单的MySQL存储过程,实现两个数相加
delimiter $$
create procere proc_add(in a int,in 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;
end$$
delimiter ;
需要特别注意的是 
1. declare语句只能放在存储过程的开始位置,放在后面就会报错 
2. if 语句的后面必须有then,但是不需要begin,在if结束时需要end if 
3. 判断是否为NULL倒是和MSSQL一样都有IS NULL 
4. delimiter是定界符的意思在结束的end后面要添加定界符 
5. end if之后必须跟分号,否则语法错误 
下面是一个较常见的场景,判断表中某列是否存在某值,如果存在执行某操作 
delimiter $$
create procere proc_add_book(in $bookName varchar(200),in $price float)
begin
    declare $existsFlag int default 0;
    select bookId into $existsFlag from book where bookName = $bookName limit 1;
    if bookId > 0 then
    #if not exists (select * from book where bookNumber = $bookName) then
        insert into book(bookNumber,price) values($bookName,$price);
    end if;
end$$
delimiter ;
需要注意的是不能用if exists;exists可以在where后面或者在create object是使用,但是在if语句中不可以使用,只能用变通的方法。
while语句也需要注意,下面是一个while的简单应用:
delimiter $$
create procere proc_add_books_looply(in $bookName varchar(200),in $price float,in $insertTimes INT)
begin
    while $insertTimes>0 do
    insert into book (bookName,price) values($bookName,$price);
    end while;
end$$
delimiter ;
可以看到while后面跟条件,条件后面要跟一个do,在while循环体结束之后需要end while并以分号结束。
以上是一些简单的总结,希望有用。
‘伍’ mysql存储过程 返回变量为什么为NULL 如以下列子~@a为什么SELECT @a;返回NULL
my understanding is "select into " is going to a temp table, not a integer, try this,
SELECT param1=COUNT(*) FROM t;
‘陆’ mysql 写的存储过程不知道为什么返回值 查看出是null
你直接call后就有返回值了吧,select @name的时候@name参数已经被销毁了
‘柒’ 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存储过程返回值null
老实说我是写MSSQL的,语法有不同。
所以,我想问一下
DECLARE OFF INT default 0;
里的OFF和
SELECT c.stat INTO OFF FROM competition c WHERE c.id = cid;
里的OFF是一样的么?
一个是变量一个是临时表吧。。。
‘玖’ 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存储过程问题
那要看一下你这个total存储过程是怎么定义的, 如果@t是output参数,需要用关键字out, 以下是一个例子:
CREATE PROCEDURE sp_add(a int, b int,out c int)
begin
 
 set c=a+ b;
 
end;
调用过程:
call sp_add (1,2,@a);
select @a;
