<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="select-into">
      </a>
      15.2.13.1 SELECT ... INTO Statement
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045180693408">
  </a>
  <a class="indexterm" name="idm46045180691952">
  </a>
  <p>
   The
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ...
        INTO
    </code>
   </a>
   form of
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   enables a query result to be stored in variables or written to a
        file:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <code class="literal">
       SELECT ... INTO
       <em class="replaceable">
        <code>
         var_list
        </code>
       </em>
      </code>
      selects column
            values and stores them into variables.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       SELECT ... INTO OUTFILE
      </code>
      writes the
            selected rows to a file. Column and line terminators can be
            specified to produce a specific output format.
     </p>
    </li>
    <li class="listitem">
     <p>
      <code class="literal">
       SELECT ... INTO DUMPFILE
      </code>
      writes a single
            row to a file without any formatting.
     </p>
    </li>
   </ul>
  </div>
  <p>
   A given
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statement can
        contain at most one
   <code class="literal">
    INTO
   </code>
   clause, although as
        shown by the
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   syntax
        description (see
   <a class="xref" href="select.html" title="15.2.13 SELECT Statement">
    Section 15.2.13, “SELECT Statement”
   </a>
   ), the
   <code class="literal">
    INTO
   </code>
   can appear in different positions:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Before
      <code class="literal">
       FROM
      </code>
      . Example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa60742057"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">INTO</span> <span class="token variable">@myvar</span> <span class="token keyword">FROM</span> t1<span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Before a trailing locking clause. Example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa7355401"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1 <span class="token keyword">INTO</span> <span class="token variable">@myvar</span> <span class="token keyword">FOR</span> <span class="token keyword">UPDATE</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      At the end of the
      <a class="link" href="select.html" title="15.2.13 SELECT Statement">
       <code class="literal">
        SELECT
       </code>
      </a>
      .
            Example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa60378267"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> t1 <span class="token keyword">FOR</span> <span class="token keyword">UPDATE</span> <span class="token keyword">INTO</span> <span class="token variable">@myvar</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
  <p>
   The
   <code class="literal">
    INTO
   </code>
   position at the end of the statement
        is the preferred position. The position before a locking clause
        is deprecated; expect support for it to be removed in a future
        version of MySQL. In other words,
   <code class="literal">
    INTO
   </code>
   after
   <code class="literal">
    FROM
   </code>
   but not at the end of the
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   produces a warning.
  </p>
  <p>
   An
   <code class="literal">
    INTO
   </code>
   clause should not be used in a nested
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   because such a
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   must return its result to
        the outer context. There are also constraints on the use of
   <code class="literal">
    INTO
   </code>
   within
   <a class="link" href="union.html" title="15.2.18 UNION Clause">
    <code class="literal">
     UNION
    </code>
   </a>
   statements; see
   <a class="xref" href="union.html" title="15.2.18 UNION Clause">
    Section 15.2.18, “UNION Clause”
   </a>
   .
  </p>
  <p>
   For the
   <code class="literal">
    INTO
    <em class="replaceable">
     <code>
      var_list
     </code>
    </em>
   </code>
   variant:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <em class="replaceable">
       <code>
        var_list
       </code>
      </em>
      names a list of one or
            more variables, each of which can be a user-defined
            variable, stored procedure or function parameter, or stored
            program local variable. (Within a prepared
      <code class="literal">
       SELECT
            ... INTO
       <em class="replaceable">
        <code>
         var_list
        </code>
       </em>
      </code>
      statement, only user-defined variables are permitted; see
      <a class="xref" href="local-variable-scope.html" title="15.6.4.2 Local Variable Scope and Resolution">
       Section 15.6.4.2, “Local Variable Scope and Resolution”
      </a>
      .)
     </p>
    </li>
    <li class="listitem">
     <p>
      The selected values are assigned to the variables. The
            number of variables must match the number of columns. The
            query should return a single row. If the query returns no
            rows, a warning with error code 1329 occurs (
      <code class="literal">
       No
            data
      </code>
      ), and the variable values remain unchanged.
            If the query returns multiple rows, error 1172 occurs
            (
      <code class="literal">
       Result consisted of more than one row
      </code>
      ).
            If it is possible that the statement may retrieve multiple
            rows, you can use
      <code class="literal">
       LIMIT 1
      </code>
      to limit the
            result set to a single row.
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa21860490"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> id<span class="token punctuation">,</span> <span class="token keyword">data</span> <span class="token keyword">INTO</span> <span class="token variable">@x</span><span class="token punctuation">,</span> <span class="token variable">@y</span> <span class="token keyword">FROM</span> test<span class="token punctuation">.</span>t1 <span class="token keyword">LIMIT</span> <span class="token number">1</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
   </ul>
  </div>
  <a class="indexterm" name="idm46045180649264">
  </a>
  <a class="indexterm" name="idm46045180647776">
  </a>
  <p>
   <code class="literal">
    INTO
    <em class="replaceable">
     <code>
      var_list
     </code>
    </em>
   </code>
   can
        also be used with a
   <a class="link" href="table.html" title="15.2.16 TABLE Statement">
    <code class="literal">
     TABLE
    </code>
   </a>
   statement, subject to these restrictions:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The number of variables must match the number of columns in
            the table.
     </p>
    </li>
    <li class="listitem">
     <p>
      If the table contains more than one row, you must use
      <code class="literal">
       LIMIT 1
      </code>
      to limit the result set to a
            single row.
      <code class="literal">
       LIMIT 1
      </code>
      must precede the
      <code class="literal">
       INTO
      </code>
      keyword.
     </p>
    </li>
   </ul>
  </div>
  <p>
   An example of such a statement is shown here:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa74939026"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">TABLE</span> employees <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> lname <span class="token keyword">DESC</span> <span class="token keyword">LIMIT</span> <span class="token number">1</span>
    <span class="token keyword">INTO</span> <span class="token variable">@id</span><span class="token punctuation">,</span> <span class="token variable">@fname</span><span class="token punctuation">,</span> <span class="token variable">@lname</span><span class="token punctuation">,</span> <span class="token variable">@hired</span><span class="token punctuation">,</span> <span class="token variable">@separated</span><span class="token punctuation">,</span> <span class="token variable">@job_code</span><span class="token punctuation">,</span> <span class="token variable">@store_id</span><span class="token punctuation">;</span></code></pre>
  </div>
  <a class="indexterm" name="idm46045180637760">
  </a>
  <a class="indexterm" name="idm46045180636272">
  </a>
  <p>
   You can also select values from a
   <a class="link" href="values.html" title="15.2.19 VALUES Statement">
    <code class="literal">
     VALUES
    </code>
   </a>
   statement that generates a
        single row into a set of user variables. In this case, you must
        employ a table alias, and you must assign each value from the
        value list to a variable. Each of the two statements shown here
        is equivalent to
   <a class="link" href="set-variable.html" title="15.7.6.1 SET Syntax for Variable Assignment">
    <code class="literal">
     SET @x=2, @y=4,
        @z=8
    </code>
   </a>
   :
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa19051156"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> <span class="token punctuation">(</span><span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> t <span class="token keyword">INTO</span> <span class="token variable">@x</span><span class="token punctuation">,</span><span class="token variable">@y</span><span class="token punctuation">,</span><span class="token variable">@z</span><span class="token punctuation">;</span>

<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> <span class="token punctuation">(</span><span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> t<span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">)</span> <span class="token keyword">INTO</span> <span class="token variable">@x</span><span class="token punctuation">,</span><span class="token variable">@y</span><span class="token punctuation">,</span><span class="token variable">@z</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   User variable names are not case-sensitive. See
   <a class="xref" href="user-variables.html" title="11.4 User-Defined Variables">
    Section 11.4, “User-Defined Variables”
   </a>
   .
  </p>
  <a class="indexterm" name="idm46045180629440">
  </a>
  <a class="indexterm" name="idm46045180628368">
  </a>
  <p>
   The
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ... INTO
        OUTFILE '
     <em class="replaceable">
      <code>
       file_name
      </code>
     </em>
     '
    </code>
   </a>
   form of
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   writes the selected rows
        to a file. The file is created on the server host, so you must
        have the
   <a class="link" href="privileges-provided.html#priv_file">
    <code class="literal">
     FILE
    </code>
   </a>
   privilege to use
        this syntax.
   <em class="replaceable">
    <code>
     file_name
    </code>
   </em>
   cannot be an
        existing file, which among other things prevents files such as
   <code class="filename">
    /etc/passwd
   </code>
   and database tables from being
        modified. The
   <a class="link" href="server-system-variables.html#sysvar_character_set_filesystem">
    <code class="literal">
     character_set_filesystem
    </code>
   </a>
   system
        variable controls the interpretation of the file name.
  </p>
  <p>
   The
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ... INTO
        OUTFILE
    </code>
   </a>
   statement is intended to enable dumping a
        table to a text file on the server host. To create the resulting
        file on some other host,
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ... INTO
        OUTFILE
    </code>
   </a>
   normally is unsuitable because there is no way
        to write a path to the file relative to the server host file
        system, unless the location of the file on the remote host can
        be accessed using a network-mapped path on the server host file
        system.
  </p>
  <p>
   Alternatively, if the MySQL client software is installed on the
        remote host, you can use a client command such as
   <code class="literal">
    mysql
        -e "SELECT ..." &gt;
    <em class="replaceable">
     <code>
      file_name
     </code>
    </em>
   </code>
   to generate the
        file on that host.
  </p>
  <p>
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ... INTO
        OUTFILE
    </code>
   </a>
   is the complement of
   <a class="link" href="load-data.html" title="15.2.9 LOAD DATA Statement">
    <code class="literal">
     LOAD
        DATA
    </code>
   </a>
   . Column values are written converted to the
        character set specified in the
   <code class="literal">
    CHARACTER SET
   </code>
   clause. If no such clause is present, values are dumped using
        the
   <code class="literal">
    binary
   </code>
   character set. In effect, there is
        no character set conversion. If a result set contains columns in
        several character sets, so is the output data file, and it may
        not be possible to reload the file correctly.
  </p>
  <p>
   The syntax for the
   <em class="replaceable">
    <code>
     export_options
    </code>
   </em>
   part of the statement consists of the same
   <code class="literal">
    FIELDS
   </code>
   and
   <code class="literal">
    LINES
   </code>
   clauses
        that are used with the
   <a class="link" href="load-data.html" title="15.2.9 LOAD DATA Statement">
    <code class="literal">
     LOAD DATA
    </code>
   </a>
   statement. For information about the
   <code class="literal">
    FIELDS
   </code>
   and
   <code class="literal">
    LINES
   </code>
   clauses, including their default
        values and permissible values, see
   <a class="xref" href="load-data.html" title="15.2.9 LOAD DATA Statement">
    Section 15.2.9, “LOAD DATA Statement”
   </a>
   .
  </p>
  <p>
   <code class="literal">
    FIELDS ESCAPED BY
   </code>
   controls how to write
        special characters. If the
   <code class="literal">
    FIELDS ESCAPED BY
   </code>
   character is not empty, it is used when necessary to avoid
        ambiguity as a prefix that precedes following characters on
        output:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The
      <code class="literal">
       FIELDS ESCAPED BY
      </code>
      character
     </p>
    </li>
    <li class="listitem">
     <p>
      The
      <code class="literal">
       FIELDS [OPTIONALLY] ENCLOSED BY
      </code>
      character
     </p>
    </li>
    <li class="listitem">
     <p>
      The first character of the
      <code class="literal">
       FIELDS TERMINATED
            BY
      </code>
      and
      <code class="literal">
       LINES TERMINATED BY
      </code>
      values
     </p>
    </li>
    <li class="listitem">
     <p>
      ASCII
      <code class="literal">
       NUL
      </code>
      (the zero-valued byte; what is
            actually written following the escape character is ASCII
      <code class="literal">
       0
      </code>
      , not a zero-valued byte)
     </p>
    </li>
   </ul>
  </div>
  <p>
   The
   <code class="literal">
    FIELDS TERMINATED BY
   </code>
   ,
   <code class="literal">
    ENCLOSED
        BY
   </code>
   ,
   <code class="literal">
    ESCAPED BY
   </code>
   , or
   <code class="literal">
    LINES
        TERMINATED BY
   </code>
   characters
   <span class="emphasis">
    <em>
     must
    </em>
   </span>
   be
        escaped so that you can read the file back in reliably. ASCII
   <code class="literal">
    NUL
   </code>
   is escaped to make it easier to view with
        some pagers.
  </p>
  <p>
   The resulting file need not conform to SQL syntax, so nothing
        else need be escaped.
  </p>
  <p>
   If the
   <code class="literal">
    FIELDS ESCAPED BY
   </code>
   character is empty,
        no characters are escaped and
   <code class="literal">
    NULL
   </code>
   is output
        as
   <code class="literal">
    NULL
   </code>
   , not
   <code class="literal">
    \N
   </code>
   . It is
        probably not a good idea to specify an empty escape character,
        particularly if field values in your data contain any of the
        characters in the list just given.
  </p>
  <a class="indexterm" name="idm46045180586000">
  </a>
  <p>
   <code class="literal">
    INTO OUTFILE
   </code>
   can also be used with a
   <a class="link" href="table.html" title="15.2.16 TABLE Statement">
    <code class="literal">
     TABLE
    </code>
   </a>
   statement when you want to
        dump all columns of a table into a text file. In this case, the
        ordering and number of rows can be controlled using
   <code class="literal">
    ORDER BY
   </code>
   and
   <code class="literal">
    LIMIT
   </code>
   ; these
        clauses must precede
   <code class="literal">
    INTO OUTFILE
   </code>
   .
   <code class="literal">
    TABLE ... INTO OUTFILE
   </code>
   supports the same
   <em class="replaceable">
    <code>
     export_options
    </code>
   </em>
   as does
   <code class="literal">
    SELECT ... INTO OUTFILE
   </code>
   , and it is subject to
        the same restrictions on writing to the file system. An example
        of such a statement is shown here:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa10031898"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">TABLE</span> employees <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> lname <span class="token keyword">LIMIT</span> <span class="token number">1000</span>
    <span class="token keyword">INTO</span> <span class="token keyword">OUTFILE</span> <span class="token string">'/tmp/employee_data_1.txt'</span>
    <span class="token keyword">FIELDS</span> <span class="token keyword">TERMINATED</span> <span class="token keyword">BY</span> <span class="token string">','</span> <span class="token keyword">OPTIONALLY</span> <span class="token keyword">ENCLOSED</span> <span class="token keyword">BY</span> <span class="token string">'"'</span><span class="token punctuation">,</span> <span class="token keyword">ESCAPED</span> <span class="token keyword">BY</span> <span class="token string">'\'
    LINES TERMINATED BY '</span>\n'<span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   You can also use
   <code class="literal">
    SELECT ... INTO OUTFILE
   </code>
   with
        a
   <a class="link" href="values.html" title="15.2.19 VALUES Statement">
    <code class="literal">
     VALUES
    </code>
   </a>
   statement to write
        values directly into a file. An example is shown here:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa37421590"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> <span class="token punctuation">(</span><span class="token keyword">VALUES</span> <span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token keyword">ROW</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> t
    <span class="token keyword">INTO</span> <span class="token keyword">OUTFILE</span> <span class="token string">'/tmp/select-values.txt'</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   You must use a table alias; column aliases are also supported,
        and can optionally be used to write values only from desired
        columns. You can also use any or all of the export options
        supported by
   <code class="literal">
    SELECT ... INTO OUTFILE
   </code>
   to
        format the output to the file.
  </p>
  <a class="indexterm" name="idm46045180571824">
  </a>
  <a class="indexterm" name="idm46045180570752">
  </a>
  <p>
   Here is an example that produces a file in the comma-separated
        values (CSV) format used by many programs:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa87469967"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">SELECT</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>a<span class="token operator">+</span>b <span class="token keyword">INTO</span> <span class="token keyword">OUTFILE</span> <span class="token string">'/tmp/result.txt'</span>
  <span class="token keyword">FIELDS</span> <span class="token keyword">TERMINATED</span> <span class="token keyword">BY</span> <span class="token string">','</span> <span class="token keyword">OPTIONALLY</span> <span class="token keyword">ENCLOSED</span> <span class="token keyword">BY</span> <span class="token string">'"'</span>
  <span class="token keyword">LINES</span> <span class="token keyword">TERMINATED</span> <span class="token keyword">BY</span> <span class="token string">'\n'</span>
  <span class="token keyword">FROM</span> test_table<span class="token punctuation">;</span></code></pre>
  </div>
  <a class="indexterm" name="idm46045180568080">
  </a>
  <p>
   If you use
   <code class="literal">
    INTO DUMPFILE
   </code>
   instead of
   <code class="literal">
    INTO OUTFILE
   </code>
   , MySQL writes only one row into
        the file, without any column or line termination and without
        performing any escape processing. This is useful for selecting a
   <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
    <code class="literal">
     BLOB
    </code>
   </a>
   value and storing it in a
        file.
  </p>
  <p>
   <a class="link" href="table.html" title="15.2.16 TABLE Statement">
    <code class="literal">
     TABLE
    </code>
   </a>
   also supports
   <code class="literal">
    INTO
        DUMPFILE
   </code>
   . If the table contains more than one row, you
        must also use
   <code class="literal">
    LIMIT 1
   </code>
   to limit the output to
        a single row.
   <code class="literal">
    INTO DUMPFILE
   </code>
   can also be used
        with
   <code class="literal">
    SELECT * FROM (VALUES ROW()[, ...]) AS
    <em class="replaceable">
     <code>
      table_alias
     </code>
    </em>
    [LIMIT 1]
   </code>
   . See
   <a class="xref" href="values.html" title="15.2.19 VALUES Statement">
    Section 15.2.19, “VALUES Statement”
   </a>
   .
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    Any file created by
    <code class="literal">
     INTO OUTFILE
    </code>
    or
    <code class="literal">
     INTO DUMPFILE
    </code>
    is owned by the operating
          system user under whose account
    <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
     <span class="command">
      <strong>
       mysqld
      </strong>
     </span>
    </a>
    runs. (You should
    <span class="emphasis">
     <em>
      never
     </em>
    </span>
    run
    <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
     <span class="command">
      <strong>
       mysqld
      </strong>
     </span>
    </a>
    as
    <code class="literal">
     root
    </code>
    for this
          and other reasons.) The umask for file creation is 0640; you
          must have sufficient access privileges to manipulate the file
          contents.
   </p>
   <p>
    If the
    <a class="link" href="server-system-variables.html#sysvar_secure_file_priv">
     <code class="literal">
      secure_file_priv
     </code>
    </a>
    system variable is set to a nonempty directory name, the file
          to be written must be located in that directory.
   </p>
  </div>
  <p>
   In the context of
   <a class="link" href="select-into.html" title="15.2.13.1 SELECT ... INTO Statement">
    <code class="literal">
     SELECT ...
        INTO
    </code>
   </a>
   statements that occur as part of events executed
        by the Event Scheduler, diagnostics messages (not only errors,
        but also warnings) are written to the error log, and, on
        Windows, to the application event log. For additional
        information, see
   <a class="xref" href="events-status-info.html" title="27.4.5 Event Scheduler Status">
    Section 27.4.5, “Event Scheduler Status”
   </a>
   .
  </p>
  <p>
   Support is provided for periodic synchronization of output files
        written to by
   <code class="literal">
    SELECT INTO OUTFILE
   </code>
   and
   <code class="literal">
    SELECT INTO DUMPFILE
   </code>
   , enabled by setting the
   <a class="link" href="server-system-variables.html#sysvar_select_into_disk_sync">
    <code class="literal">
     select_into_disk_sync
    </code>
   </a>
   server
        system variable introduced in that version. Output buffer size
        and optional delay can be set using, respectively,
   <a class="link" href="server-system-variables.html#sysvar_select_into_buffer_size">
    <code class="literal">
     select_into_buffer_size
    </code>
   </a>
   and
   <a class="link" href="server-system-variables.html#sysvar_select_into_disk_sync_delay">
    <code class="literal">
     select_into_disk_sync_delay
    </code>
   </a>
   .
        For more information, see the descriptions of these system
        variables.
  </p>
 </div>
 <br/>
</div>
