PostgreSQL stored procedures dynamically update data

Source: Internet
Author: User
Tags postgresql

--goal: Dynamically update data in a table--the usual code.
---------------------------
--tablename Table Name
--feildname Field an array group
--feildvalue field Value Array
--returnvalue return value
Create or Replace functionf_update (tablenametext, Conditiontext, Feildnametext[], Feildvaluetext[], out ReturnValuetext) as $$DeclareMySQLtext; myIDinteger; Myresultinteger; Itemstext; Countsinteger; Iinteger;begincounts:=Array_length (Feildname,1); MySQL:='Update'||Quote_ident (tablename)||'Set'; forIinch 1.. Counts loop MySQL:=Mysql||Quote_ident (feildname[I])||'=" "||Feildvalue[I]||" ",'; EndLoop; MySQL:=substring(MySQL from 1 for(Char_length (MySQL)-1))|| 'where 1=1'||condition; ExecuteMySQL; GET Diagnostics Myresult:=Row_count; ifMyresult<>0 ThenReturnValue='{"Success": "Performing the update'||Mysql||'Success! "}'; ElseReturnValue='{"Success": "Performing the update'||Mysql||'failed! "}'; End if;End; $$ language plpgsql;--actual OperationCreate TableTest (IDinteger, nametext, Gen_time date,out returnvalue);Insert intoTest (Id,name,gen_time)Values(1,'Office','2017-08-19');SelectF_update ('Test','and id=1','{Name,gen_time}','{ssqhan,2017-08-20}');--The following results are obtained:
--The data inside is a bit different, it doesn't affect everyone.
--=======================================================================

-- I don't know if you're not aware that although gen_time is a date type, when you enter the time format at Update ,
-- The system automatically turns into date format
--does this mean that the time format, when passing parameters, is OK with the string directly?
-To be verified, put here today, and then have the opportunity to access the database directly in C # to see if it will error!
--also want to be involved in the discussion of children's shoes.
--==============================================

PostgreSQL stored procedures dynamically update data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.