How MySQL exports files with date formats

Source: Internet
Author: User

A netizen asked in MySQL How to use only SQL statements to export files with a date format. Feel a little meaning, and then try it. The export file uses select INTO OUTFILE but the value behind OUTFILE cannot use variables, so it can only be implemented using dynamic SQL statements. Where table user is the test table. The specific statements are as follows

SET @SqlScript = CONCAT ("selectinto'/tmp/sql_out_", Date_format (Now (), '%y%m%d% h%i%s'), ". txt '  from 'user';");
rows Affected (0.01 sec)
PREPARE  from @SqlScript;
rows Affected (0.00 sec)
Statement prepared
EXECUTE Sql_text;
rows Affected (0.00 sec)

There are several points to note about select INTO outfile, and you must ensure that the current account has write access to the corresponding directory. Otherwise, the permissions issue "can ' t create/write to file ' xxxxx ' (Errcode:13-permission denied)" will appear

In addition to the SQL syntax and knowledge points for MySQL prepare statements:

PREPARE  from / * definition * /
 Execute statement_name [USING/* Executes a preprocessing statement * /
{deallocatedropPREPARE/* Delete definition */;

The prepare statement is used to prepare a statement, specifying the name Statement_name, and then referencing the statement. The statement name is not case sensitive. The preparable_sql_statement can be a literal string or a user variable that contains the text of the statement. The text must appear as a single SQL statement, rather than as multiple statements. In this statement, '? ' A character can be used to identify a parameter when it is executed to indicate that the data value is bound to a query. ‘?’ Characters should not be quoted, even if you want to combine them with string values. Parameter markers can only be used where data values should appear, not SQL keywords, identifiers, etc.

If you can use tools such as Shell scripting, there are many ways to export data files with date formats as well. Do not start too much here.

How MySQL exports files with date formats

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.