当前位置:Gxlcms > 数据库问题 > postgresql 存储过程动态更新数据

postgresql 存储过程动态更新数据

时间:2021-07-01 10:21:17 帮助过:144人阅读

目标:动态更新表中数据 -- 老规矩上代码
---------------------------
--tablename 表名
--feildname 字段名数组
--feildvalue 字段值数组
--returnvalue 返回值
create or replace function f_update ( tablename text, condition text, feildname text[], feildvalue text[], out returnvalue text ) as $$ declare mysql text; myid integer; myresult integer; items text; counts integer; i integer; begin counts:=array_length(feildname,1); mysql:=update ||quote_ident(tablename)|| set ; for i in 1..counts loop mysql:= mysql||quote_ident(feildname[i])||=‘‘‘||feildvalue[i]||‘‘‘,; end loop; mysql:=substring(mysql from 1 for (char_length(mysql)-1)) || where 1=1 ||condition; execute mysql; GET DIAGNOSTICS myresult:= ROW_COUNT; if myresult<>0 then returnvalue={"success":"执行更新||mysql||成功!"}; else returnvalue={"success":"执行更新||mysql||失败!"}; end if; end; $$ language plpgsql; -- 实际操作 create table test(id integer,name text,gen_time date,out returnvalue); insert into test(id,name,gen_time) values(1,office,2017-08-19); select f_update(test, and id=1,{name,gen_time},{ssqhan,2017-08-20}); -- 得到如下结果:
技术分享
  1. <span style="color: #008080;">--里面的数据有点不太一样,不影响大家看<br>--=======================================================================<br><br>--<span style="color: #ff0000;">不知道大家没有注意到,虽然 gen_time为date类型,但在UPDATE时,只要输入时间格式,</span><br>--<span style="color: #ff0000;">系统会自动的转成date格式</span><br>--<span style="color: #ff0000;">这是不是说,时间格式,在传递参数的时候,直接用字符串就OK?</span><br>--有待验证,今天放在这里,以后有机会直接用C#访问数据库看会不会报错!<br>--也希望做过的童鞋一起参与讨论。<br>--==============================================<br><br></span>

 

postgresql 存储过程动态更新数据

标签:com   test   注意   类型   class   text   sub   时间格式   c#   

人气教程排行