3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # The tests in this file ensure that a temporary table is used
13 # when required by an "INSERT INTO ... SELECT ..." statement.
15 # $Id: insert5.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
25 # Return true if the compilation of the sql passed as an argument
26 # includes the opcode OpenEphemeral. An "INSERT INTO ... SELECT"
27 # statement includes such an opcode if a temp-table is used
28 # to store intermediate results.
30 proc uses_temp_table {sql} {
31 return [expr {[lsearch [execsql "EXPLAIN $sql"] OpenEphemeral]>=0}]
34 # Construct the sample database.
37 forcedelete test2.db test2.db-journal
39 CREATE TABLE MAIN(Id INTEGER, Id1 INTEGER);
40 CREATE TABLE B(Id INTEGER, Id1 INTEGER);
41 CREATE VIEW v1 AS SELECT * FROM B;
42 CREATE VIEW v2 AS SELECT * FROM MAIN;
43 INSERT INTO MAIN(Id,Id1) VALUES(2,3);
44 INSERT INTO B(Id,Id1) VALUES(2,3);
54 SELECT * FROM B UNION ALL
55 SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
62 INSERT INTO B SELECT * FROM B;
64 SELECT * FROM MAIN WHERE exists (select * FROM B WHERE B.Id = MAIN.Id);
70 uses_temp_table { INSERT INTO b SELECT * FROM main }
73 uses_temp_table { INSERT INTO b SELECT * FROM b }
76 uses_temp_table { INSERT INTO b SELECT (SELECT id FROM b), id1 FROM main }
79 uses_temp_table { INSERT INTO b SELECT id1, (SELECT id FROM b) FROM main }
84 SELECT * FROM main WHERE id = (SELECT id1 FROM b WHERE main.id = b.id) }
87 uses_temp_table { INSERT INTO b SELECT * FROM v1 }
90 uses_temp_table { INSERT INTO b SELECT * FROM v2 }
95 SELECT * FROM main WHERE id > 10 AND max(id1, (SELECT id FROM b)) > 10;
99 # UPDATE: Using a column from the outer query (main.id) in the GROUP BY
100 # or ORDER BY of a sub-query is no longer supported.
102 # do_test insert5-2.9 {
106 # WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
109 do_test insert5-2.9 {
113 WHERE id > 10 AND (SELECT count(*) FROM v2 GROUP BY main.id)
115 } {1 {no such column: main.id}}