--这个看了?
---------------------------------------
EXECUTE 'UPDATE tbl SET '
|| quote_ident(colname)
|| ' = '
|| quote_literal(newvalue)
|| ' WHERE key = '
|| quote_literal(keyvalue);
--=============================
--好吧, 我改
------------------------------------------------------
drop function f_getNewID(text,text);
create or replace function f_getNewID(myTableName text,myFeildName text) returns integer as $$
declare
mysql text;
myID integer;
begin
mysql:='select max('
|| quote_ident(myFeildName)
||') from '
|| quote_ident(myTableName);
execute mysql into myID;
--using myTableName,myFeildName;
if myID is null or myID=0 then return 1;
else return myID+1;
end if;
end;
$$ language plpgsql;
--==============================
--漂亮,成功了!
--But Why?
--注意 对象(表名、字段名等)是不可以直接用变量的,要用 quote_ident()
-------------------------------------------------------
postgres=# select f_getnewid('department','ID');
--错误: 字段 "ID" 不存在
--第1行select max("ID") from department
^
--查询: select max("ID") from department
--背景: 在EXECUTE的第10行的PL/pgSQL函数f_getnewid(text,text)
--===============================
--什么情况,ID怎么会有双引号,引号,号,号???
----------------------------------------------------------
--这里要感谢大神:权宗亮@飞象数据
--改成这样:
postgres=# select f_getnewid('department','id');
f_getnewid
------------
2
(1 行记录)
----终于成功了!大小写还有区别吗??? --but why? --当在命令行输入
CREATE TABLE role(
ID INT PRIMARY KEY NOT NULL,
r_name VARCHAR(50) NOT NULL,
r_paretnID INT NOT NULL DEFAULT
);
--结果在pgAdmin里看到的却是小写的