0

I'm writing a function in PL/pgSQL where I need to update a column in multiple rows. However, I need to update only 67 random rows of the larger number of rows.

For example, if I have a table of animals and I want to update the food column for each animal that is labeled cat, how can I update ONLY 10 of those rows that are cats, and not update the other rows that are cats?

Patrick
  • 29,357
  • 6
  • 62
  • 90
coder4lyf
  • 927
  • 1
  • 17
  • 36

1 Answers1

0

Here is a reference on how to select n random rows. Use it as follows.

Step 1 - create a temporary table.

Step 2 - insert your random rows into this table. Use the reference to see how to do it.

Step 3 - update the records in the temporary table

Step 4 - update the real table from the temporary table

Step 5 - drop the temporary table.

Community
  • 1
  • 1
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43