Server IP : 103.119.228.120 / Your IP : 18.219.253.199 Web Server : Apache System : Linux v8.techscape8.com 3.10.0-1160.119.1.el7.tuxcare.els2.x86_64 #1 SMP Mon Jul 15 12:09:18 UTC 2024 x86_64 User : nobody ( 99) PHP Version : 5.6.40 Disable Function : shell_exec,symlink,system,exec,proc_get_status,proc_nice,proc_terminate,define_syslog_variables,syslog,openlog,closelog,escapeshellcmd,passthru,ocinum cols,ini_alter,leak,listen,chgrp,apache_note,apache_setenv,debugger_on,debugger_off,ftp_exec,dl,dll,myshellexec,proc_open,socket_bind,proc_close,escapeshellarg,parse_ini_filepopen,fpassthru,exec,passthru,escapeshellarg,escapeshellcmd,proc_close,proc_open,ini_alter,popen,show_source,proc_nice,proc_terminate,proc_get_status,proc_close,pfsockopen,leak,apache_child_terminate,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,dl,symlink,shell_exec,system,dl,passthru,escapeshellarg,escapeshellcmd,myshellexec,c99_buff_prepare,c99_sess_put,fpassthru,getdisfunc,fx29exec,fx29exec2,is_windows,disp_freespace,fx29sh_getupdate,fx29_buff_prepare,fx29_sess_put,fx29shexit,fx29fsearch,fx29ftpbrutecheck,fx29sh_tools,fx29sh_about,milw0rm,imagez,sh_name,myshellexec,checkproxyhost,dosyayicek,c99_buff_prepare,c99_sess_put,c99getsource,c99sh_getupdate,c99fsearch,c99shexit,view_perms,posix_getpwuid,posix_getgrgid,posix_kill,parse_perms,parsesort,view_perms_color,set_encoder_input,ls_setcheckboxall,ls_reverse_all,rsg_read,rsg_glob,selfURL,dispsecinfo,unix2DosTime,addFile,system,get_users,view_size,DirFiles,DirFilesWide,DirPrintHTMLHeaders,GetFilesTotal,GetTitles,GetTimeTotal,GetMatchesCount,GetFileMatchesCount,GetResultFiles,fs_copy_dir,fs_copy_obj,fs_move_dir,fs_move_obj,fs_rmdir,SearchText,getmicrotime MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/local/ssl/share/doc/postgresql-9.2.24/html/ |
Upload File : |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >VALUES</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.2.24 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="VACUUM" HREF="sql-vacuum.html"><LINK REL="NEXT" TITLE="PostgreSQL Client Applications" HREF="reference-client.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2017-11-06T22:43:11"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.2.24 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="VACUUM" HREF="sql-vacuum.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="PostgreSQL Client Applications" HREF="reference-client.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-VALUES" ></A >VALUES</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN79830" ></A ><H2 >Name</H2 >VALUES -- compute a set of rows</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN79835" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >VALUES ( <TT CLASS="REPLACEABLE" ><I >expression</I ></TT > [, ...] ) [, ...] [ ORDER BY <TT CLASS="REPLACEABLE" ><I >sort_expression</I ></TT > [ ASC | DESC | USING <TT CLASS="REPLACEABLE" ><I >operator</I ></TT > ] [, ...] ] [ LIMIT { <TT CLASS="REPLACEABLE" ><I >count</I ></TT > | ALL } ] [ OFFSET <TT CLASS="REPLACEABLE" ><I >start</I ></TT > [ ROW | ROWS ] ] [ FETCH { FIRST | NEXT } [ <TT CLASS="REPLACEABLE" ><I >count</I ></TT > ] { ROW | ROWS } ONLY ]</PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79843" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >VALUES</TT > computes a row value or set of row values specified by value expressions. It is most commonly used to generate a <SPAN CLASS="QUOTE" >"constant table"</SPAN > within a larger command, but it can be used on its own. </P ><P > When more than one row is specified, all the rows must have the same number of elements. The data types of the resulting table's columns are determined by combining the explicit or inferred types of the expressions appearing in that column, using the same rules as for <TT CLASS="LITERAL" >UNION</TT > (see <A HREF="typeconv-union-case.html" >Section 10.5</A >). </P ><P > Within larger commands, <TT CLASS="COMMAND" >VALUES</TT > is syntactically allowed anywhere that <TT CLASS="COMMAND" >SELECT</TT > is. Because it is treated like a <TT CLASS="COMMAND" >SELECT</TT > by the grammar, it is possible to use the <TT CLASS="LITERAL" >ORDER BY</TT >, <TT CLASS="LITERAL" >LIMIT</TT > (or equivalently <TT CLASS="LITERAL" >FETCH FIRST</TT >), and <TT CLASS="LITERAL" >OFFSET</TT > clauses with a <TT CLASS="COMMAND" >VALUES</TT > command. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79860" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >expression</I ></TT ></DT ><DD ><P > A constant or expression to compute and insert at the indicated place in the resulting table (set of rows). In a <TT CLASS="COMMAND" >VALUES</TT > list appearing at the top level of an <TT CLASS="COMMAND" >INSERT</TT >, an <TT CLASS="REPLACEABLE" ><I >expression</I ></TT > can be replaced by <TT CLASS="LITERAL" >DEFAULT</TT > to indicate that the destination column's default value should be inserted. <TT CLASS="LITERAL" >DEFAULT</TT > cannot be used when <TT CLASS="COMMAND" >VALUES</TT > appears in other contexts. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >sort_expression</I ></TT ></DT ><DD ><P > An expression or integer constant indicating how to sort the result rows. This expression can refer to the columns of the <TT CLASS="COMMAND" >VALUES</TT > result as <TT CLASS="LITERAL" >column1</TT >, <TT CLASS="LITERAL" >column2</TT >, etc. For more details see <A HREF="sql-select.html#SQL-ORDERBY" ><I ><I >ORDER BY</I > Clause</I ></A >. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >operator</I ></TT ></DT ><DD ><P > A sorting operator. For details see <A HREF="sql-select.html#SQL-ORDERBY" ><I ><I >ORDER BY</I > Clause</I ></A >. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >count</I ></TT ></DT ><DD ><P > The maximum number of rows to return. For details see <A HREF="sql-select.html#SQL-LIMIT" ><I ><I >LIMIT</I > Clause</I ></A >. </P ></DD ><DT ><TT CLASS="REPLACEABLE" ><I >start</I ></TT ></DT ><DD ><P > The number of rows to skip before starting to return rows. For details see <A HREF="sql-select.html#SQL-LIMIT" ><I ><I >LIMIT</I > Clause</I ></A >. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79901" ></A ><H2 >Notes</H2 ><P > <TT CLASS="COMMAND" >VALUES</TT > lists with very large numbers of rows should be avoided, as you might encounter out-of-memory failures or poor performance. <TT CLASS="COMMAND" >VALUES</TT > appearing within <TT CLASS="COMMAND" >INSERT</TT > is a special case (because the desired column types are known from the <TT CLASS="COMMAND" >INSERT</TT >'s target table, and need not be inferred by scanning the <TT CLASS="COMMAND" >VALUES</TT > list), so it can handle larger lists than are practical in other contexts. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79909" ></A ><H2 >Examples</H2 ><P > A bare <TT CLASS="COMMAND" >VALUES</TT > command: </P><PRE CLASS="PROGRAMLISTING" >VALUES (1, 'one'), (2, 'two'), (3, 'three');</PRE ><P> This will return a table of two columns and three rows. It's effectively equivalent to: </P><PRE CLASS="PROGRAMLISTING" >SELECT 1 AS column1, 'one' AS column2 UNION ALL SELECT 2, 'two' UNION ALL SELECT 3, 'three';</PRE ><P> </P ><P > More usually, <TT CLASS="COMMAND" >VALUES</TT > is used within a larger SQL command. The most common use is in <TT CLASS="COMMAND" >INSERT</TT >: </P><PRE CLASS="PROGRAMLISTING" >INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama');</PRE ><P> </P ><P > In the context of <TT CLASS="COMMAND" >INSERT</TT >, entries of a <TT CLASS="COMMAND" >VALUES</TT > list can be <TT CLASS="LITERAL" >DEFAULT</TT > to indicate that the column default should be used here instead of specifying a value: </P><PRE CLASS="PROGRAMLISTING" >INSERT INTO films VALUES ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'), ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama', DEFAULT);</PRE ><P> </P ><P > <TT CLASS="COMMAND" >VALUES</TT > can also be used where a sub-<TT CLASS="COMMAND" >SELECT</TT > might be written, for example in a <TT CLASS="LITERAL" >FROM</TT > clause: </P><PRE CLASS="PROGRAMLISTING" >SELECT f.* FROM films f, (VALUES('MGM', 'Horror'), ('UA', 'Sci-Fi')) AS t (studio, kind) WHERE f.studio = t.studio AND f.kind = t.kind; UPDATE employees SET salary = salary * v.increase FROM (VALUES(1, 200000, 1.2), (2, 400000, 1.4)) AS v (depno, target, increase) WHERE employees.depno = v.depno AND employees.sales >= v.target;</PRE ><P> Note that an <TT CLASS="LITERAL" >AS</TT > clause is required when <TT CLASS="COMMAND" >VALUES</TT > is used in a <TT CLASS="LITERAL" >FROM</TT > clause, just as is true for <TT CLASS="COMMAND" >SELECT</TT >. It is not required that the <TT CLASS="LITERAL" >AS</TT > clause specify names for all the columns, but it's good practice to do so. (The default column names for <TT CLASS="COMMAND" >VALUES</TT > are <TT CLASS="LITERAL" >column1</TT >, <TT CLASS="LITERAL" >column2</TT >, etc in <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN >, but these names might be different in other database systems.) </P ><P > When <TT CLASS="COMMAND" >VALUES</TT > is used in <TT CLASS="COMMAND" >INSERT</TT >, the values are all automatically coerced to the data type of the corresponding destination column. When it's used in other contexts, it might be necessary to specify the correct data type. If the entries are all quoted literal constants, coercing the first is sufficient to determine the assumed type for all: </P><PRE CLASS="PROGRAMLISTING" >SELECT * FROM machines WHERE ip_address IN (VALUES('192.168.0.1'::inet), ('192.168.0.10'), ('192.168.1.43'));</PRE ><P></P ><DIV CLASS="TIP" ><BLOCKQUOTE CLASS="TIP" ><P ><B >Tip: </B > For simple <TT CLASS="LITERAL" >IN</TT > tests, it's better to rely on the list-of-scalars form of <TT CLASS="LITERAL" >IN</TT > than to write a <TT CLASS="COMMAND" >VALUES</TT > query as shown above. The list of scalars method requires less writing and is often more efficient. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79947" ></A ><H2 >Compatibility</H2 ><P ><TT CLASS="COMMAND" >VALUES</TT > conforms to the SQL standard. <TT CLASS="LITERAL" >LIMIT</TT > and <TT CLASS="LITERAL" >OFFSET</TT > are <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > extensions; see also under <A HREF="sql-select.html" >SELECT</A >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN79955" ></A ><H2 >See Also</H2 ><A HREF="sql-insert.html" >INSERT</A >, <A HREF="sql-select.html" >SELECT</A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-vacuum.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="reference-client.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >VACUUM</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >PostgreSQL Client Applications</TD ></TR ></TABLE ></DIV ></BODY ></HTML >