mysql 程式編寫執行個體

來源:互聯網
上載者:User

標籤:end   test   new   etc   資訊   else   read   select   更新   

DELIMITER $$

drop trigger if exists `updata_depart_lead_name`;
-- 建立觸發器 --
-- 功能:在更新部門的領導編號是,自動填寫部門領導名字 --
create trigger `updata_depart_lead_name` before update on `department`
for each row BEGIN
set @lead_name = ‘‘;

if new.lead_id is null or new.lead_id = ‘‘ then
set @lead_name = null;
ELSE
select name into @lead_name from staff where id = new.lead_id;
end IF;

set new.lead_name = @lead_name;
END

$$

————————————————————————————————————————————————————————————————————————————————

delimiter $$

drop trigger if exists `insert_depart_lead_name`;
-- 建立觸發器 --
-- 功能:在新增部門時,自動填寫部門領導的名字 --
create trigger `insert_depart_lead_name` BEFORE insert on `department`
for each row BEGIN
set @lead_name = ‘‘;
if new.lead_id is null or new.lead_id = ‘‘ THEN
set @lead_name = null;
ELSE
select name into @lead_name from staff where id = new.lead_id;
end if;
set new.lead_name = @lead_name;
end

$$

—————————————————————————————————————————————————————————————————————————————————————

delimiter $$

drop trigger if EXISTS `remove_depart_lead_on_delete_staff`;
-- 在刪除staff時,清空department中領導為該staff的領導資訊 --
create trigger `remove_depart_lead_on_delete_staff` after DELETE on `staff`
for each row BEGIN
update department set lead_id = null, lead_name = null where lead_id = old.id;
end
$$

————————————————————————————————————————————————————————————————————————————————————————

delimiter $$

drop trigger if exists `trigger_cursor`;

create trigger `trigger_cursor` after insert on `staff`
for each row BEGIN
declare staff_id int ;
DECLARE done int default false;
DECLARE cur_test CURSOR FOR
select id from staff ;
declare CONTINUE HANDLER FOR not FOUND set done = true;
open cur_test;
read_loop:LOOP
fetch cur_test into staff_id;
if done then
LEAVE read_loop;
ELSE
insert into message values(null, ‘1‘);
end if;
end loop;
END
$$

 

mysql 程式編寫執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.