1. 程式人生 > >Mysql建立儲存過程

Mysql建立儲存過程

!!mysql中單雙反引號區別 反引號 `

反引號一般在Esc鍵的下方,和~在一起。它是為了區分MySQL的保留字與普通字元而引入的符號。
create table desc 報錯
create table `desc` 成功
一般我們建表時都會將表名,庫名都加上反引號來保證語句的執行度。

單引號 ’ 和雙引號 “

在標準 SQL 中,字串使用的是單引號。
如果字串本身也包括單引號,則使用兩個單引號(注意,不是雙引號,字串中的雙引號不需要另外轉義)。
MySQL對 SQL 的擴充套件,允許使用單引號和雙引號兩種。

1.更改結束符 delimiter $

2.建立儲存過程 CREATE DEFINER=killcat

@% PROCEDURE insert_agent_daysalary( IN i_user_id INT, IN i_is_tester TINYINT, IN i_username VARCHAR(20), IN i_sparent_id INT, IN i_parent VARCHAR(20), IN i_forefathers VARCHAR(1024), IN i_sdate DATE, IN i_daysalary DECIMAL(11,2), IN i_turnover DECIMAL(20,6), IN i_team_turnover DECIMAL(20,6), IN i_daysalary_percentage DECIMAL(4,1), IN i_commission DECIMAL(20,6), IN i_team_commission DECIMAL(20,6), IN i_bet_commission DECIMAL(20,6), IN i_team_bet_commission DECIMAL(20,6) ) BEGIN INSERT INTO chunqiupr
.user_daysalary SET user_id=i_user_id, is_tester=i_is_tester, username=i_username, sparent_id=i_sparent_id, parent=i_parent, forefathers=i_forefathers, sdate=i_sdate, daysalary=i_daysalary, created_at=now(), updated_at=now(), turnover=i_turnover, team_turnover=i_team_turnover, daysalary_percentage=i_daysalary_percentage, commission
=i_commission, team_commission=i_team_commission, bet_commission=i_bet_commission, team_bet_commission=i_team_bet_commission on DUPLICATE KEY UPDATE user_id=i_user_id, is_tester=i_is_tester, username=i_username, sparent_id=i_sparent_id, parent=i_parent, forefathers=i_forefathers, sdate=i_sdate, daysalary=i_daysalary, turnover=i_turnover, team_turnover=i_team_turnover, daysalary_percentage=i_daysalary_percentage, commission=i_commission, team_commission=i_team_commission, bet_commission=i_bet_commission, team_bet_commission=i_team_bet_commission; END;$

3.改回結束符 delimiter ;

4.查詢儲存過程是否建立成功 select ‘insert_agent_daysalary’ from mysql.proc where db = ‘chunqiupr’ and type = ‘PROCEDURE’; select name from mysql.proc where db = ‘chunqiupr’ and type = ‘PROCEDURE’;

5.刪除儲存過程 drop PROCEDURE insert_agent_daysalary;