如何在 PostgreSQL8 上使用临时序列和 SELECT 创建 SQL 函数

发布于 2024-08-29 15:05:26 字数 4854 浏览 6 评论 0原文

我在 postgreSQL 8.3 或 8.4 中遇到这个问题:

我创建了一个脚本,它(重新)创建三个临时序列,然后使用它们转置一些表以获得一行。

现在我想将其放入一个函数中,该函数返回已创建的类型(我已经进行了选择,然后从创建的表中获取了字段来构建自定义类型)。这是脚本...问题是,pgsql 说关系“q”不存在并且不使用该函数执行此 SQL 成功。 .. 我应该猜测序列创建不能在 SQL 函数内完成吗?谢谢!!

CREATE OR REPLACE FUNCTION myNewFunction(int) RETURNS SETOF myExistingType AS $$
DROP SEQUENCE IF EXISTS p;
CREATE TEMP sequence p;
DROP SEQUENCE IF EXISTS q;
CREATE TEMP sequence q;
DROP SEQUENCE IF EXISTS r;
CREATE TEMP sequence r;
SELECT
    a.ad_field1,
    a.ad_field2,
    a.ad_field3,
    ...
    a.consecutive
    children.*,
    people.*,
    reffinancieras.*,
    CAST(character varying 'test' as text)
--INTO test_table
FROM 
    pit_adquiriente AS a,

    (SELECT 
        MAX(consulta.n1) as nombrehijo1, 
        MAX(consulta.f1) as fechahijo1, 
        MAX(consulta.n2) as nombrehijo2, 
        MAX(consulta.f2) as fechahijo2,
        MAX(consulta.n3) as nombrehijo3, 
        MAX(consulta.f3) as fechahijo3, 
        MAX(consulta.n4) as nombrehijo4, 
        MAX(consulta.f4) as fechahijo4
    FROM (SELECT adquirientepadre, nextval('q') as consecutivo,
        CASE WHEN currval('q')=1 THEN nombrehijo END as n1,
        CASE WHEN currval('q')=1 THEN fechanacimiento END as f1,
        CASE WHEN currval('q')=2 THEN nombrehijo END as n2 ,
        CASE WHEN currval('q')=2 THEN fechanacimiento END as f2 ,
        CASE WHEN currval('q')=3 THEN nombrehijo END as n3 ,
        CASE WHEN currval('q')=3 THEN fechanacimiento  END as f3,
        CASE WHEN currval('q')=4 THEN nombrehijo END as n4 ,
        CASE WHEN currval('q')=4 THEN fechanacimiento  END as f4 
        FROM pit_adq_hijo
        WHERE adquirientepadre = $1) 
    AS consulta) AS children,

    (SELECT 
        MAX(consulta.nomapeacc1) as nombreapellidoacc1,
        MAX(consulta.identacc1) as idacc1, 
        MAX(consulta.tipoid1) as tipoidacc1,
        MAX(consulta.nomapeacc2) as nombreapellidoacc2,
        MAX(consulta.identacc2) as idacc2, 
        MAX(consulta.tipoid2) as tipoidacc2,
        MAX(consulta.nomapeacc3) as nombreapellidoacc3,
        MAX(consulta.identacc3) as idacc3,
        MAX(consulta.tipoid3) as tipoidacc3,
        MAX(consulta.nomapeacc4) as nombreapellidoacc4,
        MAX(consulta.identacc4) as idacc4,
        MAX(consulta.tipoid4) as tipoidacc4,
        MAX(consulta.nomapeacc5) as nombreapellidoacc5,
        MAX(consulta.identacc5) as idacc5,
        MAX(consulta.tipoid5) as tipoidacc5
    FROM (SELECT codigo_adquiriente, nextval('p') as consecutivo,
        CASE WHEN currval('p')=1 THEN nombre_apellidos END as nomapeacc1,
        CASE WHEN currval('p')=1 THEN identificacion END as identacc1,
        CASE WHEN currval('p')=1 THEN tipo_identificacion END as tipoid1 ,
        CASE WHEN currval('p')=2 THEN nombre_apellidos END as nomapeacc2,
        CASE WHEN currval('p')=2 THEN identificacion END as identacc2,
        CASE WHEN currval('p')=2 THEN tipo_identificacion END as tipoid2 ,
        CASE WHEN currval('p')=3 THEN nombre_apellidos END as nomapeacc3,
        CASE WHEN currval('p')=3 THEN identificacion END as identacc3,
        CASE WHEN currval('p')=3 THEN tipo_identificacion END as tipoid3 ,
        CASE WHEN currval('p')=4 THEN nombre_apellidos END as nomapeacc4,
        CASE WHEN currval('p')=4 THEN identificacion END as identacc4,
        CASE WHEN currval('p')=4 THEN tipo_identificacion END as tipoid4 ,
        CASE WHEN currval('p')=5 THEN nombre_apellidos END as nomapeacc5,
        CASE WHEN currval('p')=5 THEN identificacion END as identacc5,
        CASE WHEN currval('p')=5 THEN tipo_identificacion END as tipoid5 

        FROM pit_people
        WHERE codigo_adquiriente = $1)
    AS consulta) AS people,

    (SELECT 
        MAX(consulta.entfin1) as entidadreff1, 
        MAX(consulta.prod1) as productoreff1, 
        MAX(consulta.num1) as numeroreff1, 
        MAX(consulta.suc1) as sucursalreff1,

        MAX(consulta.entfin2) as entidadreff2, 
        MAX(consulta.prod2) as productoreff2, 
        MAX(consulta.num2) as numeroreff2, 
        MAX(consulta.suc2) as sucursalreff2
    FROM (SELECT codigo_adquiriente, nextval('r') as consecutive,
        CASE WHEN currval('r')=1 THEN entity END as entfin1,
        CASE WHEN currval('r')=1 THEN something END as prod1,
        CASE WHEN currval('r')=1 THEN number_something END as num1 ,
        CASE WHEN currval('r')=1 THEN place_thing END as suc1 ,
        CASE WHEN currval('r')=2 THEN entity END as entfin2,
        CASE WHEN currval('r')=2 THEN something  END as prod2,
        CASE WHEN currval('r')=2 THEN number_something END as num2 ,
        CASE WHEN currval('r')=2 THEN place_thing END as suc2 
        FROM pit_money
        WHERE codigo_adquiriente = $1) 
    AS consulta) AS reffinancieras
WHERE a.consecutive = $1
$$ LANGUAGE SQL;

I have this issue in either postgreSQL 8.3 or 8.4:

I've created a script which (re)creates three temp sequences and then uses them to transpose some tables in order to get a single row.

Now I want to put it in a FUNCTION which returns a type already created (I've done a select into and then I took the fields from the created table to build the custom type). Here is the script... the thing is, pgsql says that the relation 'q' does not exist and the execution of this SQL w/out the function is successful... should I guess that the sequence creation can't be done inside a SQL function? Thanks!!

CREATE OR REPLACE FUNCTION myNewFunction(int) RETURNS SETOF myExistingType AS $
DROP SEQUENCE IF EXISTS p;
CREATE TEMP sequence p;
DROP SEQUENCE IF EXISTS q;
CREATE TEMP sequence q;
DROP SEQUENCE IF EXISTS r;
CREATE TEMP sequence r;
SELECT
    a.ad_field1,
    a.ad_field2,
    a.ad_field3,
    ...
    a.consecutive
    children.*,
    people.*,
    reffinancieras.*,
    CAST(character varying 'test' as text)
--INTO test_table
FROM 
    pit_adquiriente AS a,

    (SELECT 
        MAX(consulta.n1) as nombrehijo1, 
        MAX(consulta.f1) as fechahijo1, 
        MAX(consulta.n2) as nombrehijo2, 
        MAX(consulta.f2) as fechahijo2,
        MAX(consulta.n3) as nombrehijo3, 
        MAX(consulta.f3) as fechahijo3, 
        MAX(consulta.n4) as nombrehijo4, 
        MAX(consulta.f4) as fechahijo4
    FROM (SELECT adquirientepadre, nextval('q') as consecutivo,
        CASE WHEN currval('q')=1 THEN nombrehijo END as n1,
        CASE WHEN currval('q')=1 THEN fechanacimiento END as f1,
        CASE WHEN currval('q')=2 THEN nombrehijo END as n2 ,
        CASE WHEN currval('q')=2 THEN fechanacimiento END as f2 ,
        CASE WHEN currval('q')=3 THEN nombrehijo END as n3 ,
        CASE WHEN currval('q')=3 THEN fechanacimiento  END as f3,
        CASE WHEN currval('q')=4 THEN nombrehijo END as n4 ,
        CASE WHEN currval('q')=4 THEN fechanacimiento  END as f4 
        FROM pit_adq_hijo
        WHERE adquirientepadre = $1) 
    AS consulta) AS children,

    (SELECT 
        MAX(consulta.nomapeacc1) as nombreapellidoacc1,
        MAX(consulta.identacc1) as idacc1, 
        MAX(consulta.tipoid1) as tipoidacc1,
        MAX(consulta.nomapeacc2) as nombreapellidoacc2,
        MAX(consulta.identacc2) as idacc2, 
        MAX(consulta.tipoid2) as tipoidacc2,
        MAX(consulta.nomapeacc3) as nombreapellidoacc3,
        MAX(consulta.identacc3) as idacc3,
        MAX(consulta.tipoid3) as tipoidacc3,
        MAX(consulta.nomapeacc4) as nombreapellidoacc4,
        MAX(consulta.identacc4) as idacc4,
        MAX(consulta.tipoid4) as tipoidacc4,
        MAX(consulta.nomapeacc5) as nombreapellidoacc5,
        MAX(consulta.identacc5) as idacc5,
        MAX(consulta.tipoid5) as tipoidacc5
    FROM (SELECT codigo_adquiriente, nextval('p') as consecutivo,
        CASE WHEN currval('p')=1 THEN nombre_apellidos END as nomapeacc1,
        CASE WHEN currval('p')=1 THEN identificacion END as identacc1,
        CASE WHEN currval('p')=1 THEN tipo_identificacion END as tipoid1 ,
        CASE WHEN currval('p')=2 THEN nombre_apellidos END as nomapeacc2,
        CASE WHEN currval('p')=2 THEN identificacion END as identacc2,
        CASE WHEN currval('p')=2 THEN tipo_identificacion END as tipoid2 ,
        CASE WHEN currval('p')=3 THEN nombre_apellidos END as nomapeacc3,
        CASE WHEN currval('p')=3 THEN identificacion END as identacc3,
        CASE WHEN currval('p')=3 THEN tipo_identificacion END as tipoid3 ,
        CASE WHEN currval('p')=4 THEN nombre_apellidos END as nomapeacc4,
        CASE WHEN currval('p')=4 THEN identificacion END as identacc4,
        CASE WHEN currval('p')=4 THEN tipo_identificacion END as tipoid4 ,
        CASE WHEN currval('p')=5 THEN nombre_apellidos END as nomapeacc5,
        CASE WHEN currval('p')=5 THEN identificacion END as identacc5,
        CASE WHEN currval('p')=5 THEN tipo_identificacion END as tipoid5 

        FROM pit_people
        WHERE codigo_adquiriente = $1)
    AS consulta) AS people,

    (SELECT 
        MAX(consulta.entfin1) as entidadreff1, 
        MAX(consulta.prod1) as productoreff1, 
        MAX(consulta.num1) as numeroreff1, 
        MAX(consulta.suc1) as sucursalreff1,

        MAX(consulta.entfin2) as entidadreff2, 
        MAX(consulta.prod2) as productoreff2, 
        MAX(consulta.num2) as numeroreff2, 
        MAX(consulta.suc2) as sucursalreff2
    FROM (SELECT codigo_adquiriente, nextval('r') as consecutive,
        CASE WHEN currval('r')=1 THEN entity END as entfin1,
        CASE WHEN currval('r')=1 THEN something END as prod1,
        CASE WHEN currval('r')=1 THEN number_something END as num1 ,
        CASE WHEN currval('r')=1 THEN place_thing END as suc1 ,
        CASE WHEN currval('r')=2 THEN entity END as entfin2,
        CASE WHEN currval('r')=2 THEN something  END as prod2,
        CASE WHEN currval('r')=2 THEN number_something END as num2 ,
        CASE WHEN currval('r')=2 THEN place_thing END as suc2 
        FROM pit_money
        WHERE codigo_adquiriente = $1) 
    AS consulta) AS reffinancieras
WHERE a.consecutive = $1
$ LANGUAGE SQL;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-09-05 15:05:26

一般来说,我会使用数组和generate_series来做这样的事情。

SELECT CASE WHEN i=1 THEN nombre_apellidos END as nomapeacc1, ...
FROM pit_people p
CROSS JOIN generate_series(1,5) s(i)

但我认为你真正想要的是做一个交叉表。
http://www.postgresql.org/docs/8.3/static/tablefunc。 html

Generally I'll use arrays and generate_series to do stuff like this.

SELECT CASE WHEN i=1 THEN nombre_apellidos END as nomapeacc1, ...
FROM pit_people p
CROSS JOIN generate_series(1,5) s(i)

But what I think you really want is to do a crosstab.
http://www.postgresql.org/docs/8.3/static/tablefunc.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文