此解决方案使用动态 SQL 并写入 Windows 文件系统。
Select Now() Into @Now; # get DateTime
Set @Now = Replace(@Now, ':', '-'); # format DateTime for the filesystem
# set Destination filename using formatted DateTime
Set @FilNam = Concat("'", 'd:/Folder/qrySelectToFile ', @Now, '.TXT', "'");
#Select @FilNam; # debug
# build query to execute, one clause at a time, for encoding purposes
Set @Sel = 'Select * From vewViewName Into OutFile ' ; # Select From Into
Set @FldsTrm = ' Fields Terminated By ","' ; # Fields Terminated
Set @FldsEnc = Concat(" Enclosed By '", Char(34 Using utf8), "'") ; # Enclosed. 'Using' clause is to avoid a blob of 34 spaces.
Set @LinsTrm = " Lines Terminated By '\\r\\n'" ; # Lines Terminated
Set @Qry = Concat(@Sel, @FilNam, @FldsTrm, @FldsEnc, @LinsTrm, ';'); # all together now
Prepare Stmt From @Qry; # Prepare
Execute Stmt ; # Execute
Deallocate Prepare Stmt ; # Done
MySQL INTO OUTFILE 的其他答案会覆盖现有文件吗?