先把語句貼出來:
create definer=`root`@`localhost` procedure `tracks_in`(in in_title varchar(100),in in_artist
varchar(100),in in_album varchar(100),in in_track int,in in_year int,in in_length int,in in_size varchar
(30),in in_last_modified varchar(30),in in_path varchar(500),in in_filename varchar(250))
begin
declare id_artist int default 0;
declare id_album int default 0;
declare id_path int default 0;
select a_id into id_artist from artist where a_artist=in_artist;
if(id_artist=0) then
insert into artist(a_artist) values(in_artist);
select last_insert_id() into id_artist;
end if;
select a_id into id_album from album where a_album=in_album;
if(id_album=0) then
insert into album(a_album,a_year) values(in_album,in_year);
select last_insert_id() into id_album;
end if;
select p_id into id_path from path where p_path=in_path;
if(id_path=0) then
insert into path(p_path) values(in_path);
select last_insert_id() into id_path;
end if;
insert into tracks(t_title,t_artist,t_album,t_track,t_length,t_size,t_last_modified,t_path,t_filename)
values
(in_title,id_artist,id_album,in_track,in_length,in_size,in_last_modified,id_path,in_filename);
end
這是三張表的關係:
接下來解釋下,輸入引數很多。。。其中有3個需要判斷,拿path舉例子吧。先定義個id_path初始為0,然後在path表裡搜尋下有沒和in_path一樣的記錄,有的話返回id給id_path,沒有的話id_path當然還是0.是0時往path表裡插入in_path,然後用last_insert_id()獲得剛插入的id,關於last_insert_id()可去看mysql手冊 我在那找的。
MySql建立乙個儲存過程
mysql 儲存過程是從 mysql 5.0 新功能。儲存過程的長處有一籮筐。只是最基本的還是執行效率和sql 封裝。特別是 sql 封裝功能,假設沒有儲存過程,在外部程式訪問資料庫時 比如 php 要組織非常多 sql 語句。特別是業務邏輯複雜的時候,一大堆的 sql 和條件夾雜在 php 中,讓...
乙個儲存過程
create or replace package abc zys is procedure daily census end abc zys 建乙個包,包中有儲存過程daily census。不涉及任何引數。create or replace package body abc zys is pro...
乙個儲存過程
首先是建立儲存過程.drop procedure if exists externalcalltocomplete delimiter create procedure externalcalltocomplete begin drop table if exists temp1 create ta...