postgresql 9.4 - postgres - insert gen_random_uuid() for the column "key" if the count(key) < 1 -


generate gen_random_uuid() column key if there count(key) < 1

my table name keytable , column key.

i using below insert statement generate uuid : value in table.

insert keytable values(gen_random_uuid());  key  ---------------------------------------  5686473e-add1-4ab1-be85-7e62152ce539 

i wanted run insert statement when dont have values in "key" column.

in other words, if count(key) < 1 want run insert keytable values(gen_random_uuid()); please help.

use insert ... select construct instead

t=# begin; insert keytable select gen_random_uuid() (select count(key) keytable) < 1; begin insert 0 1 t=# insert keytable select gen_random_uuid() (select count(key) keytable) < 1; insert 0 0 t=# rollback; rollback 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -