插入临时表在mysql存储过程中不起作用

时间:2018-11-05 02:54:58

标签: mysql

我具有以下存储过程:

create temporary table logData (table_name varchar(50), column_name varchar(50), changed_date date, changed_by char(10), old_value varchar(50), new_value varchar(50))

insert into logData SELECT table_name, column_name, changed_date, changed_by, old_value, new_value FROM log WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate

UNION

SELECT "reservation_flight", column_name, changed_date, changed_by, old_value, new_value FROM reservation_flight_log WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate

UNION

SELECT "ticket", column_name, changed_date, changed_by, old_value, new_value FROM ticket_log WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate

UNION

SELECT "passenger_record", column_name, changed_date, changed_by, old_value, new_value FROM passenger_record_log WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate

select * from logData order by changed_date;

在上面的sql select语句empId中,startDateendDate是此存储过程的输入参数。这显示错误请帮助?谢谢!

1 个答案:

答案 0 :(得分:0)

我相信您缺少两个分号:一个在第一个语句的末尾,另一个在前一行的末尾。您可以像下面这样重写它:

CREATE temporary TABLE logData (table_name   varchar(50), 
                                column_name  varchar(50), 
                                changed_date date, 
                                changed_by   char(10), 
                                old_value    varchar(50), 
                                new_value    varchar(50));

INSERT INTO logData 
   SELECT table_name, column_name, changed_date, changed_by, old_value, new_value 
   FROM log 
   WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate
UNION
   SELECT "reservation_flight", column_name, changed_date, changed_by, old_value, new_value 
   FROM reservation_flight_log 
   WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate
UNION
   SELECT "ticket", column_name, changed_date, changed_by, old_value, new_value 
   FROM ticket_log 
   WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate
UNION
   SELECT "passenger_record", column_name, changed_date, changed_by, old_value, new_value
   FROM passenger_record_log
   WHERE changed_by=empId AND changed_date BETWEEN startDate AND endDate;

SELECT * FROM logData ORDER BY changed_date;