Search Postgresql Archives

SELECT INTO question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

 

We’re trying to understand what happened with a SELECT INTO. The problem can be see with this example:

 

# create table t1 (id int, v int);

CREATE TABLE

 

# insert into t1 (select x, x from generate_series(1, 5) as g(x));

INSERT 0 5

 

# select * from t1;

id | v

----+----

  1 |  1

  2 |  2

  3 |  3

  4 |  4

  5 |  5

(5 rows)

 

nms=# select into t2 from t1;

SELECT 5

 

# select * from t2;

--

(5 rows)

 

# select * into t3 from t1;

SELECT 5

 

# select * from t3;

id | v

----+----

  1 |  1

  2 |  2

  3 |  3

  4 |  4

  5 |  5

(5 rows)

 

As you can see on the first select into, the result in t2 is … missing, no “data” at all, unlike t3 which was the expected answer. Upon closer inspection, it was realized that the “_expression_” in the statement was left out (oops!), but instead of getting a syntax error, it worked.

 

So why did it work and why was nothing stored?

 

The only answer I’ve been able to come up with is that the _expression_ was evaluated as a “null _expression_” for each row, so it gave us 5 null rows. A small part of my brain understands that, but most of my brain goes “what?!”

 

I’ve noticed that I can also do:

 

# select from t1;

--

(5 rows)

 

That also doesn’t make sense and yet it does in a weird way. I suspect the answer revolves around some corner case in the SQL Standard.

 

So, what’s going on here?

 

Thanks,

Kevin

This e-mail transmission, and any documents, files or previous e-mail messages attached to it, may contain confidential information. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, you are hereby notified that any disclosure, distribution, review, copy or use of any of the information contained in or attached to this message is STRICTLY PROHIBITED. If you have received this transmission in error, please immediately notify us by reply e-mail, and destroy the original transmission and its attachments without reading them or saving them to disk. Thank you.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux