if(@newtag<>'',(insert into A(text,tag) values('test',@newtag)),(insert into A(text,tag) values('test',concat(@tag,'01'))));@newtag和@tag分别是两个查询得到的结果,是没有什么问题的,上面判断@newtag=''为真我就执行第一个插入语句,否则我就插入地2个,但我执行的时候这里有错误,是不是不可以这样写,如果不可以这样写我应该怎么改呢?高手指教!

解决方案 »

  1.   

    在存储过程中:
    if @newtag<>'' then
    insert into A(text,tag) values('test',@newtag))
    else 
    insert into A(text,tag) values('test',concat(@tag,'01'))
    end if
      

  2.   

    晕死了!那么存储过程要怎么写啊?我是在LINUX下的,下面是我自己试写的,不行!CREATE FUNCTION fun_insertRow(inpass varchar(200)) RETURNS bool
    BEGIN
    select tag from A where text = inpass into @tag;
    set @newtag='';
    select tag+1 from A where tag like concat(@tag,'__') order by tag desc limit 1 into @newtag;
    if @newtag <>'' then
    insert into A(text,tag) values('test',@newtag));return true;
    else
    insert into A(text,tag) values('test',concat(@tag,'01'));return false;
    end if;
    END
      

  3.   

    我是放到一个txt文本里面的,文本内容就是上面的,然后在终端执行:
    mysql> source /root/Desktop/ietm_proc.txt结果就是:
    ERROR 1064 (42000): 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 '' at line 3
    Query OK, 0 rows affected (0.00 sec)Query OK, 1 row affected (0.00 sec)ERROR 1064 (42000): 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 'if @newtag <>'' then
    insert into treeview(nodetext,tag) values('test',@newtag))' at line 1
    ERROR 1064 (42000): 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 'return true' at line 1
    ERROR 1064 (42000): 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 'else
    insert into treeview(nodetext,tag) values('test',concat(@tag,'01'))' at line 1
    ERROR 1064 (42000): 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 'return false' at line 1
    ERROR 1064 (42000): 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 (42000): 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' at line 1
      

  4.   

    I advise that you should read the official document more seriously.
    Maybe this is what you expected.delimiter ||
    CREATE procedure sp_insertRow(
    inpass varchar(200)
    )
    BEGIN
      select tag from A where `text` = inpass into @tag;
      set @newtag='';
      set @s=concat('select tag+1 from A where tag like ''',@tag,'__'' order by tag desc limit 1 into @newtag');
      prepare s1 from @s;
      execute s1;
      drop prepare s1;
      set @s = NULL;
      if @newtag <>'' then
        insert into A(`text`,tag) values('test',@newtag);
        select true;
      else
        insert into A(`text`,tag) values('test',concat(@tag,'01'));
        select false;
      end if;
      set @newtag = NULL;
      set @tag = NULL;
    END||
    delimiter ;
      

  5.   

    谢了楼,学习 mysql的if else都用不来啊。 end if后要加;.- -!