【问题标题】:Can I use 'LINES STARTING BY' and 'LINES TERMINATED BY' on the same query in MySQL?我可以在 MySQL 的同一个查询中使用“LINES STARTING BY”和“LINES TERMINATED BY”吗?
【发布时间】:2020-07-15 11:59:22
【问题描述】:

我正在研究一个使用两个准备好的语句(@aux1@aux2)将一些数据复制到文本文件中的过程。然后复制信息所在的表,获取所有删除的行。

问题是当我使用call copyIntoFile() 执行该过程时出现错误。

程序

delimiter !!
drop procedure if exists copyIntoFile !!
create procedure copyIntoFile()

begin
    declare path varchar(255);
    set path = concat("'C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/", curdate(), ".txt'");

    set @aux1 = concat("select * from movie_modification where modified = true into outfile ", path, 
                        " fields terminated by ';' lines starting by 'Edit: ' lines terminated by '\n';");
    prepare stmt1 from @aux1;
    execute stmt1;
    deallocate prepare stmt1;

    set @aux2 = concat("select * from movie_modification where modified = false into outfile ", path, 
                        " fields terminated by ';' lines starting by 'New: ' lines terminated by '\n';");
    prepare stmt2 from @aux2;
    execute stmt2;
    deallocate prepare stmt2;

    delete from movie_modification;
end !!
delimiter ;

错误(执行过程时)

错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的“由 ''' 终止的行附近使用正确的语法

如您所见,错误发生在lines terminated by 附近(第 1 行)。所以现在我想知道是否可以在lines starting by 之后使用lines terminated by,或者在每个查询中是否只接受其中一个语句。

怎么了?

【问题讨论】:

    标签: mysql sql select stored-procedures prepared-statement


    【解决方案1】:

    我认为你需要替换这个:

    select ...
    into outfile ...
    fields terminated by ';' lines starting by 'Edit: ' lines terminated by '\n'
    

    与:

    select ...
    into outfile ...
    fields terminated by ';' lines starting by 'Edit: ' terminated by '\n'
    

    即:lines 关键字不应重复。

    【讨论】:

      猜你喜欢
      • 2017-12-13
      • 2019-12-15
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多