html tool

2012年4月23日星期一

动态添加列

[code]--mysql 5.1

begin
declare col_name varchar(10);
declare i int;
set i=1;
while i<3  do
  select concat('year',i ) into col_name;
set @test=concat('alter table totl_week add column ',col_name,' int not null default 0');
prepare xx from @test;
execute xx;
  deallocate prepare xx;

set i=i+1;
 end while;
end

来源参考:
http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#sqlps
eg参考:
http://topic.csdn.net/u/20100604/15/f469cc54-c42e-4301-b948-288eb9ed5b54.html
[popexizhi: mysql在动态添加表单结构时使用到了预处理,不是sql的默认定了,是吗?这个还有待go。不过想了一下,不同级别的处理是情理中的事,数据处理和数据的结构处理对应思考来说也不是一个层面上的,应该分开。嘻嘻,feel,是不是可以类比元语言的自身修改呢?嘻嘻,不知道,等lisp的学习再回答吧!]

没有评论:

发表评论