我有以下插入查询:
insert into stage(
SELECT emp.emp_id,
LISTAGG(DISTINCT(group.group_name), ','),
LISTAGG(DISTINCT(emp.name), ','),
LISTAGG(DISTINCT(emp.date_hired), ',')
FROM employee emp
JOIN employee_group group ON group.id = emp.group_id
WHERE emp.employed = 1
AND emp.type = 3
AND emp.date_hired >= dateadd(day, -2, current_date)
GROUP BY emp.emp_id
);
我想将其添加到存储过程中,在存储过程中,该表将循环遍历具有emp.type
的不同值的表
类似这样的东西(假设类型是早先声明的):
FOR employee_type IN SELECT * FROM employee_types LOOP
EXECUTE 'INSERT into stage( SELECT emp.emp_id, LISTADD(DISTINCT(group.group_name), ','), LISTAGG(DISTINCT(emp.name), ','), LISTAGG(DISTINCT(emp.date_hired), ',') FROM employee emp JOIN employee_group ON group.id = emp.group_id WHERE emp.employed = 1 AND emp.type = ' || quote_literal(employee_type.type) ||' AND emp.date_hired >= dateadd(day, -2, current_date) GROUP BY emp.emp_id );';
每当我尝试运行它时,我都会返回一个错误,提示[Amazon](500310) Invalid operation: query "SELECT 'INSERT ....'" returned 4 columns
我这里缺少什么吗?我看过Redshift文档,这应该可以工作。我不明白为什么它要运行select,或者为什么要抱怨返回4列。