SQL Formatter — INSERT / UPDATE / DELETE / MERGE


← SQL Formatter overview

Contents


INSERT

Both INSERT INTO … VALUES … and INSERT INTO … SELECT … forms are supported.

If the entire statement is short enough it is kept on one line (governed by the Simple statement max-length setting). Otherwise the formatter expands as follows:

Example — both lists short enough to stay on one line each:

INSERT INTO employees(employee_id, first_name, last_name, department_id, salary)
  VALUES (101, 'John', 'Smith', 10, 5000);

Example — standard expanded layout:

INSERT INTO employees(
    employee_id,
    first_name,
    last_name,
    department_id,
    salary
  )
  VALUES (
    101,
    'John',
    'Smith',
    10,
    5000
  );

Brick list mode for INSERT

When Brick list — Enabled is on and the column or value list contains enough items (controlled by Min items to activate), the formatter switches to brick layout for both the column list and the VALUES list.

In brick layout the items are packed into rows, each row starting at the column position of the first item and limited by Max line length and Max items per line.

Example — brick layout, max 3 items per row:

INSERT INTO employees (
    employee_id, first_name, last_name,
    department_id, job_id, hire_date,
    salary
  )
  VALUES (
    101, 'John', 'Smith', 10,  'DEV',
    DATE '2020-01-15', 5000
  );

Linked brick — column list and VALUES list

When Linked INSERT col / VALUES is on (default), the VALUES list wraps at exactly the same item positions as the column list. This means every value sits directly below its corresponding column name, making the relationship between columns and values immediately obvious at a glance.

Linked (default):

INSERT INTO employees (
    employee_id, first_name, last_name,
    department_id, job_id, hire_date,
    salary
  )
  VALUES (
    101, 'John', 'Smith',
    10, 'DEV', DATE '2020-01-15',
    5000
  );

Unlinked (each list wraps independently):

INSERT INTO employees (
   employee_id, first_name, last_name,
   department_id, job_id, hire_date,
   salary
  )
  VALUES (
    101, 'John', 'Smith', 10, 'DEV',
    DATE '2020-01-15', 5000
  );

Note: Linking requires that the column list and the VALUES list have the same number of items. When the counts differ (e.g. because DEFAULT or NULL padding was omitted), each list is formatted independently.


RETURNING INTO

RETURNING … INTO … clauses appear after INSERT, UPDATE, DELETE, and in the body of a MERGE action.

Three layout paths are used depending on total width:

Example — two-line layout:

UPDATE employees
  SET
    salary = salary * 1.1
  WHERE employee_id = 101
  RETURNING employee_id, first_name, last_name
  INTO :v_id, :v_fname, :v_lname;

UPDATE

Each SET assignment is placed on its own line, indented one level from SET. When Align SET assignments is enabled, the right-hand side of every assignment (= value) is padded so that all values align in a single column.

WHERE follows on its own line, with conditions expanded by the same AND / OR rules as in a SELECT statement.

Example:

UPDATE employees
  SET
    last_name     = 'Johnson',
    department_id = 20,
    salary        = 6000
  WHERE employee_id = 101;

DELETE

DELETE FROM table starts on the first line. WHERE starts on its own line, with conditions expanded using the same AND / OR rules as a SELECT statement.

Example:

DELETE FROM employees
WHERE department_id = 10
  AND hire_date < DATE '2000-01-01';

MERGE

MERGE statements are formatted with each top-level clause on its own line at the base indentation level. The body of each WHEN block is indented two levels from MERGE.

Clause layout:

Example:

MERGE INTO employees t
  USING(SELECT employee_id, salary FROM new_salaries) s
  ON (t.employee_id = s.employee_id)
  WHEN MATCHED THEN
    UPDATE
    SET
      t.salary = s.salary
    WHERE t.salary <> s.salary
  WHEN NOT MATCHED THEN
    INSERT (employee_id, salary)
      VALUES (s.employee_id, s.salary);

LOG ERRORS / REJECT LIMIT clauses are formatted on their own line at the base indentation level.


← SELECT formatting   |   Formatting settings →