We are SQL Server Experts. Call Us TODAY! +17738095456 or email us at sales@itimpact.com
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
  • The Team
  • Our Services
    • Dashboards Developers
    • Microsoft SQL Server
    • Microsoft SQL Server hosting for Access
    • SQL Server Integration Services (SSIS)
    • SQL Server Reporting Services (SSRS)
    • SQL Server Data Quality Services (DQS)
    • Microsoft Access
    • Microsoft VB.Net
    • Cross Functional / Business Unit Integration
    • Financial Statement Consolidation
    • Website Design
  • Case Studies
    • Automatic Reporting for Security Services Firm
    • Amazon API Integration with Access
    • Custom Quoting and Proposal Solution
    • Data Quality Analysis for Government Program
    • New Claim System Reduces Processing Time
    • Pipe Tracking Software
    • Product Search Analysis
    • Safety Training, Incident and Accident Tracking Tool
    • Union Dues and Membership Management System
  • Blog
  • Contact
Toggle panel Social icons
SQL Server

Mixed Blessings of SQL Triggers

Sometime we take over a project where the original developer used SQL Triggers on the tables and unfortunately, we tend to find that more often, the triggers are not written in most effective manner. I’d like to highlight few common mistakes I see made with using the triggers:

Assuming only one row

I can’t cite number of times I had to perform bulk data operations only to have it fail or generate unexpected results because of a trigger that couldn’t handle the bulk data operation. That is, the developer simply assumes that there are going to be only single row being inserted, updated or deleted. That is very bad assumption. Sometime the original developers might put in a check like:
IF ((SELECT COUNT(*) FROM inserted) = 1
>do the thing<

Which is OK but that brings me to the next point…

Defining triggers that should fire only sometimes

if it’s only for single-insert/update did it really have to be a trigger? Why not put it in a Access’ AfterUpdate event behind the form or a stored procedure? Triggers incur some overhead and limiting it to only single-row operation misses out on the value of trigger and better approach for managing the logic. Believe me, you really don’t want a trigger that is applicable only sometimes. The trigger will always fire and for business rules that are applicable only sometimes, I think there are better solutions.

Using cursors or procedural programming in a trigger

Let me make it absolutely clear. SQL Server is optimized for set-based operations, not procedural operations. The temptation is great to write T-SQL in the same way we write VBA or C# or whatever programming language you use. Don’t. What you know about your favorite programming language does NOT apply to SQL. Using cursors enables you to write T-SQL a bit more procedurally but it’s akin to hammering nails with a wrench. You’ll get there eventually but the results won’t be beautiful. The key thing about triggers in SQL Server is that it’s always statement-based, meaning all rows that are being operated are affected once. This is why we have inserted and deleted tables, and not a single row constructor. Some other RDBMS products supports row-based triggers but for whatever reasons, Microsoft has declined to support this kind of construction. In many cases, it shouldn’t matter — you want your triggers to run as fast as it can and that means being able to deal with the inserted and deleted like what they are — tables to be operated upon on as a set. Many times, it is possible to rewrite a trigger that would only handle single row into a version that will work for unlimited numbers of rows, removing the IF construct. That’s a big win in performance.

But what if I only need it sometimes and I can’t do it in the client?

There are a number of cases where we have to enforce business rules and they may not be always applicable. The answer is to define a trigger on a view instead of the base table. You can create an INSTEAD OF trigger upon a view and expose that view to the client. Because the trigger is related to the view, manipulating the base table won’t cause the trigger to fire which is both good and bad.

But one biggest plus in my book for defining a trigger on a view is this— I always write my view definition like this:

CREATE VIEW dbo.vwMyView AS
-- IMPORTANT: There is a trigger(s) defined on this view
-- If the view is altered, check the trigger to ensure
-- it is consistent with the new view definition
SELECT
aColumn,
otherColumn
FROM dbo.aTable;

Now I have better reassurance that if I have to revisit view next year or if someone else comes and read my T-SQL definition, they won’t be unpleasantly surprised by trigger lurking in the shadows. Try to do that with a table trigger. It’s not that easy to document or discoverable especially since we typically do an ALTER TABLE that won’t contain the complete definition as an ALTER VIEW does. It’s not bulletproof but much better than it is with table triggers.

In the end, use it sparingly
One unfortunate consequence of creating triggers is that they cost some more maintenance. Make one too many triggers, you may end up with a database schema that’s more fragile than a house of cards. Sometimes it’s just that business rules are best enforced in the application level or at least via a stored procedure. Done right, they can be a boon and simplify your development. Just be careful with the double edges.

Happy selecting!

Click here to cancel reply.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts

  • 23 Jan. 2019 7 essential SQL skills that can help you command a higher paycheck

  • 18 Jan. 2018 Quick Guide on Importing Data from SQL Server (or SQL Azure) to Cosmos DB

  • 10 Jan. 2018 Top 7 Questions about Basic Availability Groups

  • 04 Jan. 2018 An introduction to mssql-cli, a command-line client for SQL Server

  • Share and work together on the intranet with SharePoint Team Sites

ITImpact

About IT Impact, Inc.

IT Impact, Inc.

IT Impact, Inc. is a minority-owned software company specializing in custom software solutions. We work with SQL Server, Microsoft Access, web applications, and much more.

Our staff is top of the line and includes two Microsoft MVPs!

Twitter

Tweets by @ITImpactInc

Facebook

© ITImpact. Webdesign by GreatCustomWebsites.com