码迷,mamicode.com
首页 > 数据库 > 详细

postgresql 存储过程动态更新数据

时间:2017-08-20 14:50:47      阅读:252      评论:0      收藏:0      [点我收藏+]

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

-- 目标:动态更新表中数据
-- 老规矩上代码
---------------------------
--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}); -- 得到如下结果:
技术分享
--里面的数据有点不太一样,不影响大家看
--=======================================================================

--不知道大家没有注意到,虽然 gen_time为date类型,但在UPDATE时,只要输入时间格式,
--系统会自动的转成date格式
--这是不是说,时间格式,在传递参数的时候,直接用字符串就OK?
--有待验证,今天放在这里,以后有机会直接用C#访问数据库看会不会报错!
--也希望做过的童鞋一起参与讨论。
--==============================================

 

postgresql 存储过程动态更新数据

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

原文地址:http://www.cnblogs.com/ssqhan/p/7399789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!