COMMIT Statement - SQL Server to PostgreSQL Migration

In SQL Server and PostgreSQL, the COMMIT statement commits the current transaction.

SQL Server and PostgreSQL:

  -- Sample table
  CREATE TABLE colors (name VARCHAR(30));
 
  BEGIN TRANSACTION;
 
  -- Insert a sample row
  INSERT INTO colors VALUES('Black');
 
  COMMIT TRANSACTION;

COMMIT in Stored Procedure

SQL Server allows you to commit the transaction in a stored procedure:

SQL Server:

   -- Sample procedure
  CREATE PROCEDURE sp_add_color @name VARCHAR(30), @type CHAR(1)
  AS
    -- Insert a row
    INSERT INTO colors VALUES(@name);
 
    IF @type = 'C' 
      COMMIT TRANSACTION;
    ELSE
      ROLLBACK TRANSACTION;
  GO

In a PostgreSQL stored procedure, you can also commit the current transaction, but you have to use the COMMIT statement, not COMMIT TRANSACTION (although the TRANSACTION keyword is allowed outside procedure).

PostgreSQL:

  -- Sample procedure
  CREATE OR REPLACE PROCEDURE sp_add_color(p_name VARCHAR(30), p_type CHAR(1))
  AS $$
  BEGIN 
    -- Insert a row
    INSERT INTO colors VALUES(p_name);
 
    IF p_type = 'C' THEN 
      COMMIT; 
    ELSE
      ROLLBACK;
    END IF;
  END;
  $$ LANGUAGE plpgsql;

For more information, see SQL Server to PostgreSQL Migration.