標籤:mysql 儲存 sql mysql預存程序 預存程序
</pre></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋體, PMingLiU, serif; line-height: 21px;"><span style="font-size:18px;"><span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋體, PMingLiU, serif; line-height: 37.894737243652344px;">封裝sql:</span></span><!--EndFragment--></div><div style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋體, PMingLiU, serif; line-height: 21px;"><span style="font-size:18px;"><span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋體, PMingLiU, serif; line-height: 37.894737243652344px;"></span></span><pre name="code" class="sql">create procedure p1()beginselect * from t_news;end $ //mysql預存程序 簡單一實例
顯示預存程序資訊: \G (橫向表格採用縱向表格輸出)
delimiter $ 改變執行符號,直到mysql碰到$ 開始執行語句命令 set names 解決mysql亂碼問題 但mysql重啟後又還原到以前字元集狀態
call 預存程序名字 () 調用預存程序
參數:
<span style="font-size:18px;">create procedure p2(n int) #含參beginselect * from t_category where cid > n;end $ </span>
控制結構:
<span style="font-size:18px;">create procedure p3(n int, j char(1)) #含參begin if j='h' then #與其他語言不同 必須加then 注意符號= 不是== select * from t_category where cid > n;elseselect * from t_category where cid <n; end if;end $ </span>
計算1....n的和:
create procedure p4(n smallint) #含參begin declare i int;declare s int;set s = 0;set i = 1;while i<=n doset s =s+i;set i=i+1;end while;select s;end $
<img src="" alt="" style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', Tahoma, Helvetica, sans-serif, 'Microsoft YaHei', Georgia, Helvetica, Arial, sans-serif, 宋體, PMingLiU, serif; background-color: rgb(255, 255, 255);" />
預存程序和函數的區別:
名稱不同 :預存程序:procedure 函數function 預存程序沒有返回值