1. Learn
  2. /
  3. Courses
  4. /
  5. Transactions and Error Handling in PostgreSQL

Connected

Exercise

Writing do statements

Commonly when cleaning data, we'll get data that will have bad dates in it. This would cause an exception and halt our SQL statement; however, by using a DO function with an exception handler, our statement will run to completion. Let's see how we can handle that type of exception with the patients table and the created_on column. This will also give us a chance to use a DO style function.

Instructions

100 XP
  • Create a DO function to initiate catching an exception.
  • BEGIN a transaction where you INSERT the row (a1c = 5.8, glucose = 89, fasting = TRUE, and created_on = '37-03-2020 01:15:54') into patients.
  • Add an EXCEPTION handler, that inserts 'bad date' in the detail column of the errors table in case of an error.
  • Specify the 'plpgsql' language .