Skip to content
Advertisement

How to call a function and dupe the return value in a SELECT query?

I’ve defined a function CREATE_UUID() to generate UUID. I want to INSERT INTO…SELECT to copy a table but with two new columns with the same value.

But doing so will call the function twice with different values. How should I modify this query so that it won’t have different values for two columns in the same row but does generate a new UUID for each row?

Advertisement

Answer

Variant 1.

But if you insert a lot of rows then server may “optimize” the execution, and all rows may obtain the same UUID.

Variant 2.

In this case server may, backward, call new generation for each inserted value.

Variant 3.

Seems to be the most safe. But used user-defined variable assignment is deprecated.

This everything in one fiddle.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement