列的Postgres错误参数

时间:2019-01-22 16:42:19

标签: postgresql

我是Postgres DB的新手,我正在尝试编写一个函数(如存储过程)以将用户插入我的数据库。我知道我肯定会遗漏一些明显的东西,但是在Postgres说{ error: column "p_organizationsid" does not exist }的地方显示的代码中,我总是遇到错误。这是似乎引起问题的代码段。

CREATE OR REPLACE FUNCTION userupsertget(
    p_id uuid,
    p_username character varying,
    p_organizationid uuid,
    p_organizationsid character varying,
    p_lastname character varying,
    p_firstname character varying,
    p_middleinitial character varying,
    p_emailaddress character varying,
    p_sid character varying,
    p_languageid integer,
    p_forcepasswordreset bit,
    p_salt character varying,
    p_hash character varying,
    p_statusid uuid,
    p_createdby uuid,
    p_modifiedby uuid,
    p_lastsessionuguid uuid,
    p_roleids uuid[],
    p_applicationid uuid)
    RETURNS SETOF users 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$

BEGIN
    DO
        $do$
        DECLARE establishedOrgID uuid;
        BEGIN

        SELECT org.id FROM organization AS org 
        WHERE org.sid = p_organizationsid 
            OR org.id = p_organizationid;

        IF EXISTS (SELECT 1 FROM users WHERE id = p_id) THEN 
           --update statement;
            UPDATE users
            SET username = p_username,
                lastname = p_lastname,
                firstname = p_firstname,
                middleinitial = p_middleinitial,
                emailaddress = p_emailaddress,
                sid = p_sid,
                languageid = p_languageid,
                forcepasswordreset = p_forcepasswordreset,
                statusid = p_statusid,
                datemodified = current_timestamp,
                createdby = p_createdby,
                modifiedby = p_modifiedby,
                lastsessionguid = p_lastsessionguid
            WHERE id = p_id;

            IF EXISTS (SELECT 1 FROM users WHERE id = p_id AND statusid <> p_statuid) THEN
                UPDATE users SET statusid = p_statusid, datestatusmodified = current_timestamp WHERE id = p_id;
            END IF;

        ELSE    

            INSERT INTO users (id, organizationid, username, lastname, firstname, middleinitial, 
                                      emailaddress, sid, languageid, forcepasswordreset, statusid, datecreated,
                                      datemodified, createdby, modifiedby, lastsessionguid)
            SELECT p_id, establishedOrgID, p_username, p_lastname, p_firstname, p_middleinitial, p_emailaddress, p_sid,
               p_languageid, p_forcepasswordreset, p_statusid, current_timestamp, current_timestamp,
               p_createdby, p_modifiedby, p_lastsessionuguid
            FROM users 
            WHERE id = p_id;

--          DECLARE newid uuid := LASTVAL();

            INSERT INTO users (id, userid, roleid, applicationid, startdate, enddate, createdby, modifiedBy)
            SELECT LASTVAL(), roleid, p_applicationid, current_timestamp, current_timestamp, p_createdby, p_modifiedby 
            FROM UNNEST (p_roleids) AS roleids;

        END IF;
        END
        $do$;

    RETURN QUERY
        SELECT usr.*, org.uselogin 
        FROM users AS usr
               INNER JOIN organization AS org On org.id = usr.organizationid
        WHERE id = p_id;
END; 

$BODY$;

1 个答案:

答案 0 :(得分:1)

您可以在PostgreSQL文档中看到有关创建函数的信息:https://www.postgresql.org/docs/9.1/sql-createfunction.html

如果将参数名称替换为$n,其中n是函数参数的编号,那么您会获得更好的运气。

CREATE FUNCTION add(integer, integer) RETURNS integer
    AS 'select $1 + $2;'
    LANGUAGE SQL
    IMMUTABLE
    RETURNS NULL ON NULL INPUT;

或...

CREATE FUNCTION check_password(uname TEXT, pass TEXT)
RETURNS BOOLEAN AS $$
DECLARE passed BOOLEAN;
BEGIN
        SELECT  (pwd = $2) INTO passed
        FROM    pwds
        WHERE   username = $1;

        RETURN passed;
END;
$$  LANGUAGE plpgsql
    SECURITY DEFINER
    -- Set a secure search_path: trusted schema(s), then 'pg_temp'.
    SET search_path = admin, pg_temp;

现在,如果希望继续使用参数名称,则应将其声明为别名:

CREATE or REPLACE FUNCTION data_ctl(opcao char, fdata date, fhora time) RETURNS char(10) AS $$
DECLARE
    opcao ALIAS FOR $1;
    vdata ALIAS FOR $2;
    vhora ALIAS FOR $3;
    retorno char(10);
BEGIN
    IF opcao = 'I' THEN 
    insert into datas (data, hora) values (vdata, vhora);
        retorno := 'INSERT';
    END IF;
    IF opcao = 'U' THEN 
    update datas set data = vdata, hora = vhora where data='1995-11-01';
        retorno := 'UPDATE';
    END IF;
    IF opcao = 'D' THEN 
    delete from datas where data = vdata;
        retorno := 'DELETE';
    ELSE
        retorno := 'NENHUMA';
    END IF; 
    RETURN retorno;
END;
$$
LANGUAGE plpgsql;

--select data_ctl('I','1996-11-01', '08:15');
select data_ctl('U','1997-11-01','06:36');
select data_ctl('U','1997-11-01','06:36');