Assigns random values from a set to a table column (postgres)

CREATE TABLE set_values( id serial primary key, value varchar(255) ); INSERT INTO set_values (value) values ('val1'), ('val2'), ('val3'), ('val4'); UPDATE target_table SET target_column=subquery.value FROM (SELECT rand.id, value FROM set_values s join ( SELECT trunc(random()*4+1) as i, a.id FROM generate_series(1,n_table_rows) as a(id) ) as rand on s.id = rand.i) AS subquery WHERE id = subquery.id; DROP TABLE set_values;
Update a specific column from a specific table with a set of values for each row randomly.
OBS.: Remember on the WHERE clause the id is id from target_Table!, id could be any unique identifier :)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.