简体   繁体   English

sql:使用动态列和值更新动态表

[英]sql: update dynamic table with dynamic columns and values

target: i wanna create a function which can update table,and the condition(in where) is dynamic .as following: 目标:我想创建一个可以更新表的函数,并且条件(在哪里)是动态的。如下:

create or replace function f_update(
    tablename          text,      --the table name updated
    update_fields      text,      --fields and values as json 
                                  --[{"fieldName":"id","fieldValue":1},
                                  --{"fieldName":"name","fieldValue":"admin"},
                                  --{"fieldName":"salary,"fieldValue":6000.00}]
    condition_fields   text,      --condition: json like above
    out return_value   text       --return value
) as $$
declare
    ex_sql             text;
    recs               record;
begin
    ex_sql:='update '||quote_ident(tablename)||' set ';
    --set values to the column
    for recs in select * from json_to_recordset(update_fields::json) as x( field_name text,field_value text) loop
        if json_typeof(recs.field_value) ='numeric' then 
            ex_sql:=ex_sql|| recs.field_name || '=' ||  recs.field_value ||',';
        else 
            ex_sql:=ex_sql|| recs.field_name || '='''|| recs.field_value || ''',';
        end if;
    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql)-1);

    --setting conditions 
    ex_sql:=ex_sql||' where 1=1';
    for recs in select * from json_to_recordset(condition_fields::json) as x( field_name text,field_value text) loop
        if json_typeof(recs.field_value) ='numeric' then 
            ex_sql:=ex_sql|| ' and ' || recs.field_name || '=' ||  recs.field_value ||',';
        else 
            ex_sql:=ex_sql|| ' and ' || recs.field_name || '='''|| recs.field_value || ''',';
        end if;
    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql)-1);
    return_value:=ex_sql;
end;
$$ language plpgsql;

--of cource ,it cannot pass! -当然,它不能通过! --the question: --how to set value to the columns? -问题:-如何为列设置值? --because the table is dynamic,so the type of the column is unknown! -因为表格是动态的,所以列的类型是未知的!

i changed the function: 我改变了功能:

create or replace function f_update(
    tablename          text,          
    update_feilds      text,          
    condition_feilds   text,       
    out return_value   text        
) as $$
declare
    ex_sql             text;
    recs               record;
begin
    ex_sql:='update '||quote_ident(tablename)||' set ';
    for recs in select * from json_to_recordset(update_feilds::json) as x( feild_name text,feild_value text) loop
        if json_typeof(to_json(recs.feild_value)) ='number' then 
            ex_sql:=ex_sql|| recs.feild_name || '=' ||  recs.feild_value ||',';
        else 
            ex_sql:=ex_sql|| recs.feild_name || '='''|| recs.feild_value || ''',';
        end if;
    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql));


    ex_sql:=ex_sql||' where 1=1';
    for recs in select * from json_to_recordset(condition_feilds::json) as x( feild_name text,feild_value text) loop
        if json_typeof(to_json(recs.feild_value)) ='number' then 
            ex_sql:=ex_sql|| ' and ' || recs.feild_name || '=' ||  recs.feild_value ||',';
        else 
            ex_sql:=ex_sql|| ' and ' || recs.feild_name || '='''|| recs.feild_value || ''',';
        end if;
    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql));
    return_value:=ex_sql;
end;
$$ language plpgsql;

after some testing: i pass the parameters like : tableName:'test' update_feilds '[{"field_name":"name","field_value":"ssqhan"},{"field_name":"salary","field_value":5200.00}]' condition_feilds '[{"field_name":"id,"field_value":1}]' 经过一些测试:我通过了参数:tableName:'test'update_feilds'[{“ field_name”:“ name”,“ field_value”:“ ssqhan”},{“ field_name”:“ salary”,“ field_value”:5200.00 }]'condition_feilds'[{“ field_name”:“ id,” field_value“:1}]'

and i got the sql string as: update test set name='ssqhan',salary='5200.00' where 1=1 and id='1' 和我得到的SQL字符串为:更新测试集名称=“ ssqhan”,工资=“ 5200.00”,其中1 = 1和ID =“ 1”

however,this is not what i mean: id int name text, salay number! 但是,这不是我的意思:id int名称文本,salay号! sql should be like: update test set name='ssqhan',salary=5200.00 where 1=1 and id=1, that's what i want. sql应该像:更新测试集名称=“ ssqhan”,工资= 5200.00其中1 = 1和id = 1,这就是我想要的。

i got another code like that: 我得到了另一个这样的代码:

create or replace function f_update_all(
    tablename          text,      
    update_feilds      text,       
    condition_feilds   text,       
    out return_value   text        
) as $$
declare
    ex_sql             text;
    recs               record;
    _key               text ;
    _value             text;
begin
    ex_sql:='update '||quote_ident(tablename)||' set ';
    --setting values for updated table
    for recs in select * from json_array_elements(update_feilds::json)   loop
        _key   := recs.value ->> 'feild_name';
        _value := recs.value ->> 'feild_value' ;


        if json_typeof(recs.value -> 'feild_value') ='number' then 
            ex_sql:=ex_sql|| _key || '=' ||  _value ||',';
        else 
            ex_sql:=ex_sql|| _key || '='''||  (recs.value ->> 'feild_value')  || ''',';
        end if;


    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql));

    --setting condition in where 
     ex_sql:=ex_sql||' where 1=1';
    for recs in select * from  json_array_elements(condition_feilds::json)  loop
        _key   := recs.value ->> 'feild_name';
        _value := recs.value ->> 'feild_value' ;

         if json_typeof(recs.value -> 'feild_value') ='number' then 
            ex_sql:=ex_sql|| ' and ' || _key || '=' ||  _value ||',';
        else 
            ex_sql:=ex_sql|| ' and ' || _key || '='''||  (recs.value ->> 'feild_value') || ''',';
        end if;
    end loop;
    ex_sql:= substring(ex_sql from 0 for length(ex_sql));
    return_value:=ex_sql;
end;
$$ language plpgsql;

when i pass the parameters as : 'test' ,'[{"feild_name":"name","feild_value":"ssqhan"},{"feild_name":"salary","feild_value":5200.00}]' ,'[{"feild_name":"id","feild_value":2}]' 当我将参数传递为:'test'时,'[{“ feild_name”:“ name”,“ feild_value”:“ ssqhan”},{“ feild_name”:“ salary”,“ feild_value”:5200.00}]'',' [{“ feild_name”:“ id”,“ feild_value”:2}]'

the return sql like: update test set name='ssqhan',salary=5200.00 where 1=1 and id=2 返回SQL,如:update test set name ='ssqhan',salary = 5200.00 where 1 = 1 and id = 2

Yeah,this what i want ,this may work when the datatype is text or number, but if the data type is composited? 是的,这就是我想要的,当数据类型是文本或数字时,这可能有效,但是如果数据类型是复合的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM