有两张表,一张是ab,另一张是cd,
存储过程的功能是如果ab中的字段address不为空,则在cd中的字段sum自动累加1,如果ab有n个address,cd中的sum就累加到n;
向各位请教存储过程的写法并且如何用触发器来触发它。
谢谢啦!

解决方案 »

  1.   

    如果ab中的字段address不为空
    是insert时不为空呢.还是update时不为空?
    感觉你说得不太清楚.
      

  2.   

    ab中的address是insert操作;
    cd中的sum是update操作;功能是在ab中增加address的同时,cd中的sum自动加1.我觉得这个自动累加的功能应该要用存储过程来实现,还请各位帮忙了,50分那,谢谢了!
      

  3.   

    delimiter $$
    create trigger tri_ab before insert
    on ab for each row
    begin
        if new.address is not null then
            update cd
            set iSum=iSum+1 ;
        end if;
    end$$
    delimiter ;
      

  4.   

    谢谢楼上的GG,如果我定义的address是varchar类型的,是不是应该改成
    if new.address!="" then       ?
      

  5.   

    我测试了,结果有问题那ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end if' at line 1
    ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end$$
         delimiter' at line 1
      

  6.   

    如果是为了学习触发器和存储过程的话,无可厚非。
    不过这个例子只要一个select就能得到结果,cd表显得有些多余。
      

  7.   

    create table ab(address varchar(1000) not null);
    create table cd(sum int not null);
    DELIMITER $$DROP PROCEDURE IF EXISTS `test2`.`sp_csdn`$$CREATE PROCEDURE `test2`.`sp_csdn`()
    BEGIN
      select count(*) from ab into @cnt;
      
      if @cnt <= 1 then
        select address from ab into @addr;
        if @addr is not null or @addr != '' then
          update bc set `sum` = `sum` + 1;
        end if;
      else
        update bc set `sum` = `sum` + @cnt;
      end if;
    END$$DELIMITER ;
      

  8.   

    楼上兄弟,sql语句中有 if then  end if吗?
      

  9.   

    sql标准好像是没有的.
    但不同的dbms产商都自行扩展.所以很多dbms都支持.详细请看官方手册
      

  10.   

    你是在MYSQL版问的问题。回答当然用MYSQL的语法了。
      

  11.   

    sql里面有case when then end