如何在函数的UPDATE或SELECT语句中使用动态列名? [英] How to use dynamic column names in an UPDATE or SELECT statement in a function?

查看:153
本文介绍了如何在函数的UPDATE或SELECT语句中使用动态列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PostgreSQL 9.1中,PL/pgSQL给出了一个查询:

In PostgreSQL 9.1, PL/pgSQL, given a query:

select fk_list.relname from ...

,其中relnamename类型(例如"table_name").

where relname is of type name (e.g., "table_name").

如何获取"relname"的适当值,该值可以直接在UPDATE语句中用作:

How do you get the appropriate value for "relname" that can be used directly in an UPDATE statement as:

Update <relname> set ...

在PL/pgSQL脚本中?

within the PL/pgSQL script?

quote_ident(r.relname)用作:

Update quote_ident(r.relname) Set ...

失败:

<(>或附近的语法错误(( LINE 55:更新quote_ident(r.relname)....

syntax error at or near "(" LINE 55: UPDATE quote_ident(r.relname) ....

我正在使用的完整代码:

The complete code I am working with:

CREATE FUNCTION merge_children_of_icd9 (ocicd9 text, 
                      ocdesc text, ncicd9 text, ncdesc text)
RETURNS void AS $BODY$
DECLARE
  r RECORD;
BEGIN
  FOR r IN
    WITH fk_actions ( code, action ) AS (
      VALUES ('a', 'error'), 
             ('r', 'restrict'), 
             ('c', 'cascade'),
             ('n', 'set null'),
             ('d', 'set default')
    ), fk_list AS (
      SELECT pg_constraint.oid AS fkoid, conrelid, confrelid::regclass AS parentid,
             conname, relname, nspname,
             fk_actions_update.action AS update_action,
             fk_actions_delete.action AS delete_action,
             conkey AS key_cols
      FROM pg_constraint
      JOIN pg_class ON conrelid = pg_class.oid
      JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
      JOIN fk_actions AS fk_actions_update ON confupdtype = fk_actions_update.code
      JOIN fk_actions AS fk_actions_delete ON confdeltype = fk_actions_delete.code
      WHERE contype = 'f'
    ), fk_attributes AS (
      SELECT fkoid, conrelid, attname, attnum
      FROM fk_list
      JOIN pg_attribute ON conrelid = attrelid AND attnum = ANY(key_cols)
      ORDER BY fkoid, attnum
    ), fk_cols_list AS (
      SELECT fkoid, array_agg(attname) AS cols_list
      FROM fk_attributes
      GROUP BY fkoid
    )
    SELECT fk_list.fkoid, fk_list.conrelid, fk_list.parentid, fk_list.conname, 
           fk_list.relname, fk_cols_list.cols_list
    FROM fk_list
    JOIN fk_cols_list USING (fkoid)
    WHERE parentid = 'icd9'::regclass
  LOOP
    RAISE NOTICE 'now in loop. relname is %', quote_ident(r.relname);
    RAISE NOTICE 'cols_list[1] is %', quote_ident(r.cols_list[1]);
    RAISE NOTICE 'cols_list[2] is %', quote_ident(r.cols_list[2]);
    RAISE NOTICE 'now doing update';

    UPDATE quote_ident(r.relname) SET r.cols_list[1] = ncicd9, r.cols_list[2] = ncdesc
    WHERE r.cols_list[1] = ocicd9 AND r.cols_list[2] = ocdesc;

    RAISE NOTICE 'finished update'; 
  END LOOP;     
  RETURN;
END $BODY$ LANGUAGE plpgsql VOLATILE;   

-- select merge_children_of_icd9('', 'aodm type 2', '', 'aodm, type 2'); 

我敢肯定这种事情经常发生,但是我似乎无法使用PostgreSQL找到类似的东西.有更好的方法吗?

I'm sure this kind of thing is done often, but I can't seem to find anything like it using PostgreSQL. Is there a better way?

推荐答案

在PL/pgSQL中的UPDATE语句中,表名必须作为文字给出.如果要动态设置表名和列,则应使用

In an UPDATE statement in PL/pgSQL, the table name has to be given as a literal. If you want to dynamically set the table name and the columns, you should use the EXECUTE command and paste the query string together:

EXECUTE 'UPDATE ' || quote_ident(r.relname) ||
       ' SET ' || quote_ident(r.cols_list[1]) || ' = $1, ' || 
                  quote_ident(r.cols_list[2]) || ' = $2' ||
       ' WHERE ' || quote_ident(r.cols_list[1]) || ' = $3 AND ' ||
                    quote_ident(r.cols_list[2]) || ' = $4'
USING ncicd9, ncdesc, ocicd9, ocdesc;

USING子句只能用于替换数据值,如上所示.

The USING clause can only be used for substituting data values, as shown above.

这篇关于如何在函数的UPDATE或SELECT语句中使用动态列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆