您好我在为postgres SQL创建函数时遇到了问题,我的脚本如下。
只是为了解释:person_details表的主键只是id,person_id是同一person_details表中的不同字段。
我们的想法是调用该函数并将其用作(对于多个电子邮件和ID号码),搜索的某些电子邮件和ID来自同一帐户,这就是DISTINCT的用途:
EXECUTE delete_accounts(('abc@email.com','bcd@email.com'),(1231342321,1224235343))
以下是错误:
错误:用户"或其附近的语法错误 SQL状态:42601 性格:840
我知道在数据库中调用表用户有一些问题,但更新用户'给出了同样的错误 - 我不认为这是我的问题。抱歉,如果还有其他错误 - 我习惯了mySQL,这是第一次使用postgres SQL
如果有帮助 - 我在pgadmin3中使用它
CREATE OR REPLACE FUNCTION delete_accounts(emailList TEXT, personID BIGINT)
RETURNS INTEGER AS $$
DECLARE
personDetailsID BIGINT;
userIDlist BIGINT;
appID TEXT;
BEGIN
--find all person_details ids using the person_id or email given
personDetailsID = (SELECT DISTINCT id FROM person_details WHERE email IN $1 OR person_id IN $2);
--set the register_status in the people found in personDetailsID to closed
UPDATE person_details AS p
SET p.onboarding_status = 'CLOSED'
WHERE p.id IN personDetailsID;
--Each person set up can have multiple users - find the user ids using the user_person_details linking table
userIDlist = (SELECT upd.user_id FROM user_person_details AS upd WHERE upd.person_details_id IN personDetailsID);
--update the enabled field for the user to 'f'
UPDATE user as u
SET u.enabled = 'f'
WHERE u.id in userIDlist;
$$ LANGUAGE plpgsql;
答案 0 :(得分:1)
问题是user
在SQL中是reserved keyword(有关列表,请参阅here)。
如果您确实需要将其用作对象名称,则必须对其进行双引号("user"
),以便将其识别为标识符而非关键字。
但最好避免使用此类标识符。