告诉我这段代码为什么在Postgre中报错CREATE FUNCTION calts() RETURNS TRIGGER AS BEGIN UPDATE stu SET tscore=(SELECT SUM(score) FROM grade WHERE grade.sid=NEW.sid) WHERE sid=NEW.sid; RETURN NEW; END; LANGUAGE plpgsql; CREATE TRIGGER tr1 AFTER INSERT ON grade FOR EACH ROW WHEN (NEW.score > 0) EXECUTE PROCEDURE calts();
时间: 2023-05-27 11:04:38 浏览: 97
这段代码在PostgreSQL中报错是因为函数定义中缺少了一个$符号,应该写成:
CREATE FUNCTION calts() RETURNS TRIGGER AS $$
BEGIN
UPDATE stu SET tscore=(SELECT SUM(score) FROM grade WHERE grade.sid=NEW.sid) WHERE sid=NEW.sid;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
另外,在定义触发器时,WHEN子句中使用(NEW.score > 0)这样的条件可以理解为只有当新插入的记录分数大于0时才执行触发器,这个条件是否合理需要根据实际业务需求来决定。
相关问题
如何在postgre中使 update set 可以使用别名
在 PostgreSQL 中,如果你想在 `UPDATE` 语句中使用表的别名,并设置多个列的值,你可以按照以下步骤操作:
首先,给表创建一个别名(alias)。例如,如果你有一个名为 `employees` 的表,你想通过别名 `emp` 更新数据,可以这样做:
```sql
WITH emp AS (
SELECT employee_id, first_name, last_name FROM employees
)
UPDATE emp
SET first_name = 'NewFirstName', last_name = 'NewLastName'
WHERE employee_id = :your_employee_id;
```
在这个例子中,`emp` 是 `employees` 表的别名,然后在 `UPDATE` 语句中可以直接使用这个别名来指定要更新的列。
注意:
1. 别名通常用于简化复杂的查询,使得语句更易读。
2. 当涉及到实际更新数据时,确保别名对应的数据字段与你要更新的实际字段一致,否则可能会导致错误。
3. 将 `:your_employee_id` 替换为你想要更新的具体员工 ID。
select b.[leader_name],[inspected_person],wrong_type,b.Departure_date,count(*) as miss数量 from ( select * from ( select * ,row_number() over(partition by [check_date],[checker],[inspected_person],[secondary_rework],[type],[project],[project_name],[NID],[wrong_type],[reason],[wrong_description],[complaint_status],[date],[create_time] order by [inspected_person] desc) as row from ( SELECT [check_date] ,[checker] ,[inspected_person] ,[secondary_rework] ,[type] ,[project] ,[project_name] ,[NID] ,[wrong_type] ,[reason] ,[wrong_description] ,[complaint_status] ,[date] ,[create_time] ,[AssigneeId] FROM [DV_report].[dbo].[miss] as a left join [User] as b on a.[AssigneeId]=b.id where date>='${start}' and date<='${end}' and complaint_status='申诉后' and wrong_type not in ('无','建议') and inspected_person not in('DL001','DL002') ) c) d where d.row = '1' UNION select * from ( select * ,row_number() over(partition by [check_date],[checker],[inspected_person],[secondary_rework],[type],[project],[project_name],[NID],[wrong_type],[reason],[wrong_description],[complaint_status],[date],[create_time] order by [inspected_person] desc) as row from ( SELECT [check_date] ,[checker] ,[secondary_rework] as [inspected_person] ,'' as [secondary_rework] ,[type] ,[project] ,[project_name] ,[NID] ,[wrong_type] ,[reason] ,[wrong_description] ,[complaint_status] ,[date] ,[create_time] ,[AssigneeId] FROM [DV_report].[dbo].[miss] as a left join [User] as b on a.[AssigneeId]=b.id where date>='${start}' and date<='${end}' and complaint_status='申诉后' and wrong_type = '网络问题' and [reason] = '状态错误' and [secondary_rework] is not null and [secondary_rework] !='' and inspected_person not in('DL001','DL002') ) c) d where d.row = '1' )as a left join [User] as b on a.[AssigneeId]=b.id WHERE b.leader_name IS NOT NULL and b.IsActive = '1' group by b.[leader_name],[inspected_person],wrong_type,b.Departure_date order by [inspected_person] 帮我将这段代码转换为postgre格式语言
SELECT b."leader_name", "inspected_person", "wrong_type", b."Departure_date", count(*) as miss数量
FROM (
SELECT *
FROM (
SELECT *, row_number() OVER(PARTITION BY "check_date","checker","inspected_person","secondary_rework","type","project","project_name","NID","wrong_type","reason","wrong_description","complaint_status","date","create_time" ORDER BY "inspected_person" DESC) AS row
FROM (
SELECT "check_date"
,"checker"
,"inspected_person"
,"secondary_rework"
,"type"
,"project"
,"project_name"
,"NID"
,"wrong_type"
,"reason"
,"wrong_description"
,"complaint_status"
,"date"
,"create_time"
,"AssigneeId"
FROM "DV_report"."dbo"."miss" AS a
LEFT JOIN "User" AS b ON a."AssigneeId"=b.id
WHERE "date">='${start}'
AND "date"<='${end}'
AND "complaint_status"='申诉后'
AND "wrong_type" NOT IN ('无','建议')
AND "inspected_person" NOT IN('DL001','DL002')
) c
) d
WHERE d.row = '1'
UNION
SELECT *
FROM (
SELECT *, row_number() OVER(PARTITION BY "check_date","checker","inspected_person","secondary_rework","type","project","project_name","NID","wrong_type","reason","wrong_description","complaint_status","date","create_time" ORDER BY "inspected_person" DESC) AS row
FROM (
SELECT "check_date"
,"checker"
,"secondary_rework" AS "inspected_person"
,'' AS "secondary_rework"
,"type"
,"project"
,"project_name"
,"NID"
,"wrong_type"
,"reason"
,"wrong_description"
,"complaint_status"
,"date"
,"create_time"
,"AssigneeId"
FROM "DV_report"."dbo"."miss" AS a
LEFT JOIN "User" AS b ON a."AssigneeId"=b.id
WHERE "date">='${start}'
AND "date"<='${end}'
AND "complaint_status"='申诉后'
AND "wrong_type" = '网络问题'
AND "reason" = '状态错误'
AND "secondary_rework" IS NOT NULL
AND "secondary_rework" !=''
AND inspected_person NOT IN('DL001','DL002')
) c
) d
WHERE d.row = '1'
) AS a
LEFT JOIN "User" AS b ON a."AssigneeId"=b.id
WHERE b."leader_name" IS NOT NULL
AND b."IsActive" = '1'
GROUP BY b."leader_name","inspected_person","wrong_type",b."Departure_date"
ORDER BY "inspected_person"
阅读全文