Skip to content

Instantly share code, notes, and snippets.

@stefanborghys
Last active March 11, 2016 16:20
Show Gist options
  • Save stefanborghys/5197947 to your computer and use it in GitHub Desktop.
Save stefanborghys/5197947 to your computer and use it in GitHub Desktop.
Drop a trigger from an Oracle database using Liquibase (http://www.liquibase.org).
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog ../../xsd/dbchangelog-2.0.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext ../../xsd/dbchangelog-ext.xsd"
logicalFilePath="drop_trigger_changelog.xml">
<changeSet id="1" author="stefanborghys" runOnChange="true">
<preConditions onFail="MARK_RAN">
<sqlCheck expectedResult="1">
SELECT DECODE(COUNT(*),0,0,1) FROM DBA_OBJECTS WHERE OBJECT_TYPE = 'TRIGGER' AND OBJECT_NAME = 'TRIGGER_NAME';
</sqlCheck>
</preConditions>
<comment>Drop a trigger from an Oracle database.</comment>
<sql><![CDATA[DROP TRIGGER TRIGGER_NAME;]]></sql>
<rollback>
<![CDATA[
create or replace
trigger SCHEMA_NAME.TRIGGER_NAME
BEFORE INSERT OR UPDATE ON TABLE_NAME
BEGIN
-- The trigger's logic
END;
]]>
</rollback>
</changeSet>
</databaseChangeLog>
@stefanborghys
Copy link
Author

I was looking for a way to remove an unused trigger from an Oracle database.
Because we use Liquibase (*) as our database change managing tool i needed to create a changelog script capable of doing this. I could not find a good solution on the internet, so i came up with the given example piece of code.
This script checks if the trigger (still) exists and only if so it tries to remove it.
I also added a rollback statement, in case something fails and also to have a history of the removed trigger.
The preCondition check might look like overkill but comes in handy when working in situations where you have to deal with multiple databases in different states.

@Teqqles
Copy link

Teqqles commented May 7, 2014

You can use the same syntax should you wish to drop an Oracle package. There aren't many in the wild but still good to know. (e.g.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment