?

這篇文章主要介紹了postgresql 存儲函數調用變量的存儲3種方法小結,具有很好的函數參考價(jià)值,希望對大家有所幫助。調用的種一起跟隨小編過(guò)來(lái)看看吧。變量
一、小結假設有(you)表student,存儲字段ヽ(′▽?zhuān)?ノ分別有id,函數remark,name等字段。
二、調用的種寫(xiě)一個(gè)存(cun)儲函數,變量根據傳過(guò)去的小結變量ID更新remark的內容。(′_ゝ`)
調用該存儲函數格式如下:
1select update_student(1);
三、存儲存儲函數示例如下:
CREAT??E OR REPLACE FUNCTION public.update_student(id intege??r)
RETURNS text AS
$BODY$
declare sql_str_run text;
BEGIN
/*
--method 1
select 'update studeヽ(′▽?zhuān)?ノnt set remark ='''|| now(??) ||''' where student.id = '|| $1 into sql_str_run ;
execute sql_str_run;
--method 2
execute 'update student set remark =now() where student.id=$1' using $1;
*/
--method 3
update student set remark =now() where student.id=$1;
return 'update is 函數ok(′ω`)'?? ;
end
$BO??DY$
LANGUAGE plpgsql VOLATIヾ(^-^)ノLE
以??上三種方法都可以實(shí)現同樣的效果,實(shí)際應用中,調用的種可以結合場(chǎng)景來(lái)使用。變??量比較簡(jiǎn)單的小結情況下直接用method 3。
method 1或method 2 有什么區別呢?
如果需要拼的變量可以直接獲取的,則用method2即可(ke)。如果變量本身也是需要需要通過(guò)函數或語(yǔ)句的計算來(lái)獲得,一般建議用method 1,先拼成一個(gè)字符串,再調用execute來(lái)實(shí)現。
補(╯‵□′)╯充:postgresql存儲函數/存儲過(guò)程用sql語(yǔ)句來(lái)(???)給變量賦值
--定義變量
1a numeric;
方式一:
1select sqla into a from table1 where b = '1' ; --這是sql語(yǔ)句賦值
sql1:= 'select a from table1 where b = ' '1' ' ';
execute sql1 into a; --這是執行存儲函數賦值
文章來(lái)源:腳本之家
來(lái)源地址:https://www.jb51.net/article/204911.htm
友情鏈接:
鄭州新財網(wǎng)絡(luò )科技有限公司新疆哈密洋財網(wǎng)絡(luò )科技有限公司
© 2013-2025.Company name All rights reserved.網(wǎng)站地圖 天津九安特機電工程有限公司-More Templates