乱码乱a∨中文字幕,在线免费激情视频,亚洲欧美久久夜夜潮,国产在线网址

  1. <sub id="hjl7n"></sub>

    1. <sub id="hjl7n"></sub>

      <legend id="hjl7n"></legend>

      當(dāng)前位置:首頁(yè) >  站長(zhǎng) >  數(shù)據(jù)庫(kù) >  正文

      postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié)

       2021-05-22 16:53  來(lái)源: 腳本之家   我來(lái)投稿 撤稿糾錯(cuò)

        阿里云優(yōu)惠券 先領(lǐng)券再下單

      這篇文章主要介紹了postgresql 存儲(chǔ)函數(shù)調(diào)用變量的3種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧。

      一、假設(shè)有表student,字段分別有id,remark,name等字段。

      二、寫一個(gè)存儲(chǔ)函數(shù),根據(jù)傳過(guò)去的變量ID更新remark的內(nèi)容。

      調(diào)用該存儲(chǔ)函數(shù)格式如下:

      1select update_student(1);

      三、存儲(chǔ)函數(shù)示例如下:

      CREATE OR REPLACE FUNCTION public.update_student(id integer)
       RETURNS text AS
      $BODY$
      declare sql_str_run text;
      BEGIN
      /*
      --method 1
       select 'update student 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
      $BODY$
       LANGUAGE plpgsql VOLATILE

       

      以上三種方法都可以實(shí)現(xiàn)同樣的效果,實(shí)際應(yīng)用中,可以結(jié)合場(chǎng)景來(lái)使用。比較簡(jiǎn)單的情況下直接用method 3。

      比如,表名、字段名本身是變量,那么method 3 就無(wú)法實(shí)現(xiàn),需要根據(jù)method 1或method 2來(lái)實(shí)現(xiàn)。

      method 1或method 2 有什么區(qū)別呢?

      如果需要拼的變量可以直接獲取的,則用method2即可。如果變量本身也是需要需要通過(guò)函數(shù)或語(yǔ)句的計(jì)算來(lái)獲得,一般建議用method 1,先拼成一個(gè)字符串,再調(diào)用execute來(lái)實(shí)現(xiàn)。

      補(bǔ)充:postgresql存儲(chǔ)函數(shù)/存儲(chǔ)過(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; --這是執(zhí)行存儲(chǔ)函數(shù)賦值

      文章來(lái)源:腳本之家

      來(lái)源地址:https://www.jb51.net/article/204911.htm

      申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

      相關(guān)文章

      熱門排行

      信息推薦