How to validate a trigger in SQL Server

  • My problem is this: sometimes changes to database schema invalidate the SQL statements in a trigger. For instance I may drop a column in a table that a trigger references. When I do that, I do not receive any indication of that problem until the next time the trigger fires, which might be months out in production in certain situations.

    What is the best way to validate objects, such as triggers or user defined functions, in a SQL server database?

  • Try running:

    sp_refreshsqlmodule or

    sp_refreshview

    on potentially affected objects.

    You should get errors on the object(s) that now reference unknown columns.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • I ran EXEC sys.sp_refreshsqlmodule @name = 'trg_Audit_Del' but it did not give me any error even though that trigger is using a table that doesn't exist in the database. Any other suggestions?

  • A table that doesn't exist is a different issue, because SQL's deferred name resolution allows that, but not invalid/missing column names.

    The other thing to try would be dmv:

    sys.sql_expression_dependencies

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • Do you have any suggestions for table that doesn't exist in trigger? how to find them?

  • Again, check this DMV:

    sys.sql_expression_dependencies

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply