少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          1、首先建表:
          create table tababin(
          id int not null auto_increment,
          name varchar(100),
          constraint pk primary key(id)
          )

          2、拷貝一張相同的表:
          create table tababin1 like tababin;

          3.建立主鍵自增觸發器:
          create trigger triabin before insert on tababin for each ROW
          begin
          set @new=new.id;
          end

          4、插入記錄:
          insert into tababin (name) values ('abin1')
          insert into tababin (name) values ('abin2')
          insert into tababin (name) values ('abin3')

          5‘編寫存儲過程(帶游標和LOOP循環的存儲過程):
          CREATE  PROCEDURE pabin()
          begin
          declare id,status int ;
          declare name varchar(100);
          declare mycur cursor for select * from tababin;
          declare continue handler for not found set status=1;
          open mycur;
          set status=0;
          loopLabel:loop
          fetch mycur into id,name;
          if status=0 then
          if id is not null then
          if name is not null then
          insert into tababin1 values (id,name);
          end if;
          end if;
          end if;
          if status =1 then
          leave loopLabel;
          end if;
          end loop;
          close mycur;
          end

          6、測試存儲過程:
          call pabin()


          結果:tababin1表里面新增了數據。
          posted on 2012-10-18 10:21 abin 閱讀(457) 評論(0)  編輯  收藏 所屬分類: mysql
          主站蜘蛛池模板: 秦皇岛市| 阿拉尔市| 若羌县| 南昌市| 涡阳县| 乌拉特前旗| 昌都县| 巨野县| 综艺| 莱州市| 隆回县| 和田县| 如东县| 休宁县| 资溪县| 凤阳县| 绥中县| 高阳县| 塔城市| 炉霍县| 焦作市| 浑源县| 云南省| 富裕县| 定兴县| 射洪县| 华安县| 台中市| 红河县| 伊金霍洛旗| 天镇县| 栾川县| 安岳县| 贵溪市| 宁强县| 大洼县| 七台河市| 阆中市| 洛南县| 民勤县| 鸡泽县|