]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/copyselect.sql
Have \copy go through SendQuery
[postgresql] / src / test / regress / sql / copyselect.sql
1 --
2 -- Test cases for COPY (select) TO
3 --
4 create table test1 (id serial, t text);
5 insert into test1 (t) values ('a');
6 insert into test1 (t) values ('b');
7 insert into test1 (t) values ('c');
8 insert into test1 (t) values ('d');
9 insert into test1 (t) values ('e');
10
11 create table test2 (id serial, t text);
12 insert into test2 (t) values ('A');
13 insert into test2 (t) values ('B');
14 insert into test2 (t) values ('C');
15 insert into test2 (t) values ('D');
16 insert into test2 (t) values ('E');
17
18 create view v_test1
19 as select 'v_'||t from test1;
20
21 --
22 -- Test COPY table TO
23 --
24 copy test1 to stdout;
25 --
26 -- This should fail
27 --
28 copy v_test1 to stdout;
29 --
30 -- Test COPY (select) TO
31 --
32 copy (select t from test1 where id=1) to stdout;
33 --
34 -- Test COPY (select for update) TO
35 --
36 copy (select t from test1 where id=3 for update) to stdout;
37 --
38 -- This should fail
39 --
40 copy (select t into temp test3 from test1 where id=3) to stdout;
41 --
42 -- This should fail
43 --
44 copy (select * from test1) from stdin;
45 --
46 -- This should fail
47 --
48 copy (select * from test1) (t,id) to stdout;
49 --
50 -- Test JOIN
51 --
52 copy (select * from test1 join test2 using (id)) to stdout;
53 --
54 -- Test UNION SELECT
55 --
56 copy (select t from test1 where id = 1 UNION select * from v_test1 ORDER BY 1) to stdout;
57 --
58 -- Test subselect
59 --
60 copy (select * from (select t from test1 where id = 1 UNION select * from v_test1 ORDER BY 1) t1) to stdout;
61 --
62 -- Test headers, CSV and quotes
63 --
64 copy (select t from test1 where id = 1) to stdout csv header force quote t;
65 --
66 -- Test psql builtins, plain table
67 --
68 \copy test1 to stdout
69 --
70 -- This should fail
71 --
72 \copy v_test1 to stdout
73 --
74 -- Test \copy (select ...)
75 --
76 \copy (select "id",'id','id""'||t,(id + 1)*id,t,"test1"."t" from test1 where id=3) to stdout
77 --
78 -- Drop everything
79 --
80 drop table test2;
81 drop view v_test1;
82 drop table test1;
83
84 -- psql handling of COPY in multi-command strings
85 copy (select 1) to stdout\; select 1/0; -- row, then error
86 select 1/0\; copy (select 1) to stdout; -- error only
87 copy (select 1) to stdout\; copy (select 2) to stdout\; select 0\; select 3; -- 1 2 3
88
89 create table test3 (c int);
90 select 0\; copy test3 from stdin\; copy test3 from stdin\; select 1; -- 1
91 1
92 \.
93 2
94 \.
95 select * from test3;
96 drop table test3;