向表中添加一个字段,前提是不知道该表是否存在,sql语句该怎么写呢?

解决方案 »

  1.   

    没有这样的SQL语句,先判断,再修改
      

  2.   

    可以从information_schema.TABLES里查询但一般不建议,这只要初始化时检查一下就可以了。
      

  3.   

    5以上可以从information_schema.TABLES中查询存在某表
      

  4.   

    对,MYSQL目前不支持匿名块(POSTGRESQL9、ORACLE、SQLSERVER2005支持)
      

  5.   


    DROP PROCEDURE IF EXISTS 'PROC_ADD_TPROTOCOL';
    CREATE PROCEDURE `PROC_ADD_TPROTOCOL`()begin
       if exists(SELECT table_name FROM information_schema.TABLES WHERE table_name ='tprotocol') then
          alter table tprotocol add Lx64Length int(4) not null default '0';
          alter table tprotocol add Lx64Content mediumblob null;
       end if;
    end;这样可以吗
      

  6.   

    可以,也可以
    SELECT count(*) into @tt FROM information_schema.TABLES WHERE table_name ='tprotocol';
    if @tt>1 then
    你的语句
    end if
      

  7.   

    可以先show tables like 'xxxx'; 然后根据结果的FOUND_ROWS()判断表是否存在。