QUOTE_IDENT 函數以雙引號字符串的形式返回指定字符串,以便此字符串可用作 SQL 語句中的標識符。
在 sql 語句中,大寫,全部會變成小寫,如果想保留大寫,需要加 雙引號
基礎表
create table student ( id integer not null primary key , name varchar(255), sex varchar );
存儲過程(存過)
select * from student; create table teacher as select * from student; ---------------11111111111111111 CREATE extension "uuid-ossp"; SELECT uuid_generate_v4 ( ); --立即執行存儲過程 DO $$ DECLARE v_idx INTEGER := 1; BEGIN while v_idx < 300000 loop v_idx = v_idx + 1; INSERT INTO "public"."student" ( "id", "name" ) VALUES ( uuid_generate_v4 ( ), 'bobo' ); END loop; END $$; ---------------22222222222222222 create or replace function count_student1() returns integer as $$ declare pgSqlScript text; counts integer; begin pgSqlScript:='select count("name") from "student"'; execute pgSqlScript into counts; return counts; end; $$ language plpgsql; select count_student1(); ---------------333333333333333 create or replace function count_student2(tableName text, columnName text) returns text as $$ declare pgSqlScript text; counts integer; begin pgSqlScript:='select count(' || quote_ident(columnName) || ') from ' || quote_ident(tableName); return pgSqlScript; end; $$ language plpgsql; select count_student2('student','name'); ---------------4444444444444444 drop function count_student3; create or replace function count_student3(tableName text, columnName text) returns integer as $$ declare pgSqlScript text; counts integer; begin pgSqlScript:='select count(' || quote_ident(columnName) || ') from ' || quote_ident(tableName); execute pgSqlScript into counts; if counts > 100 then return counts; else return 0; end if; end; $$ language plpgsql; select count_student3('student','name'); ---------------555555555555555 create or replace function count_student5() returns integer as $$ declare pgSqlScript text; myid integer; myname varchar; begin pgSqlScript:='select id,name from student order by "id" asc '; execute pgSqlScript into myid,myname;-- 可以同時賦多個值,但只會塞入最后一行數據 raise notice 'result is %' , myname; --打印語句 return myid; end; $$ language plpgsql; select count_student5(); select id,name from student order by id; delete from teacher; select * from teacher; select * from student; insert into teacher select * from student; update teacher T1 set name = T2.name from student T2 where T1.id = T2.id;
游標使用
CREATE OR REPLACE FUNCTION cursor_demo() RETURNS refcursor AS --返回一個游標 $BODY$ declare --定義變量及游標 unbound_refcursor refcursor; --游標 t_accid varchar; --變量 t_accid2 int; --變量 begin --函數開始 open unbound_refcursor for execute 'select name from cities_bak'; --打開游標 並注入要搜索的字段的記錄 loop --開始循環 fetch unbound_refcursor into t_accid; --將游標指定的值賦值給變量 if found then --任意的邏輯 raise notice '%-',t_accid; else exit; end if; end loop; --結束循環 close unbound_refcursor; --關閉游標 raise notice 'the end of msg...'; --打印消息 return unbound_refcursor; --為函數返回一個游標 exception when others then --拋出異常 raise exception 'error-----(%)',sqlerrm;--字符“%”是后面要顯示的數據的占位符 end; --結束 $BODY$ LANGUAGE plpgsql; --規定語言 select cursor_demo(); --調用
總結一下:
定義變量是在begin前
變量賦值時使用 :=
select 中賦值使用into
1、存儲過程(FUNCITON)變量可以直接用 || 拼接。上面沒有列出,下面給個栗子:
create or replace function f_getNewID(myTableName text,myFeildName text) returns integer as $$ declare mysql text; myID integer; begin mysql:='select max('|| $2 || ' ) from '||$1; execute mysql into myID using myFeildName,myTableName; if myID is null or myID=0 then return 1; else return myID+1; end if; end; $$ language plpgsql;
2、存儲過程的對象不可以直接用變量,要用 quote_ident(objVar)
3、$1 $2是 FUNCTION 參數的順序,如1中的 $1 $2交換,USING 后面的不換 結果 :select max(myTableName) from myFeildname
4、注意:SQL語句中的大寫全部會變成小寫,要想大寫存大,必須要用雙引號。
Postgresql 存儲過程(plpgsql)兩層for循環的操作==>https://www.jb51.net/article/204229.htm