You can use various control structures in database procedures, database functions and database triggers, for example loops or branches.
SQL Reference Manual, Routine (routine)
SQL Tutorial, Database Procedures, Database Functions, Database Triggers