Submit Your Suggestions for Vista

Ideaspace topics with the most votes have been moved into the Suggestion Box. If there is a topic missing, please re-enter. Our product team reviews the Suggestion Box items regularly and will provide updates as to status and incorporation into upcoming releases.

Allow SQL MERGE

The SQL MERGE statement has been around since 2008 (10 years ) and is a great help for set based operations when inserting/updating rows. Updating your triggers to not block it would greatly be appreciated!

 

What is SQL Merge?

SQL Merge is a command that allows a programmer to do multiple inserts AND deletes in one command, instead of using separate commands for inserts and deletes.

 

How does using [select @numrows = @@rowcount] in VP's triggers block SQL MERGE?

Because SQL MERGE can do inserts, updates, and deletes all at the same time, the @@rowcount variable contains the TOTAL number of rows affected. Imagine for a moment that we are inserting 3 records, deleting 2, and updating 1. That's a total of 6 records affected. Thus, @@rowcount will be equal to 6. Let's step through the process as it hits a simple UPDATE trigger.

  • As the update trigger starts, @@rowcount equals 6. So the update trigger sets @numrows = 6.
  • Next up, the update trigger checks "Is @numrows different than the count of rows in [inserted] that have a valid company number?".
  • This would turn into something like "Is 6 equal to 4?" (4 = 3+1, 3 updated + 1 inserted).
  • Obviously, 4 does not equal 6, thus the UPDATE trigger will throw an error like "Invalid field data!!".

What about a INSERT trigger?

  • As the insert trigger starts, @@rowcount = 6, so trigger sets @numrows = 6.
  • Next up, the update trigger checks "Is @numrows different than the count of rows in [inserted] that have a valid company number?".
  • This would turn into something like "Is 6 equal to 4?" (4 = 3+1, 3 updated + 1 inserted).
  • And the same error would happen.

What WOULD happen if Viewpoint fixes their triggers:

  • As the INSERT trigger starts, it sets @numrows = (select count(*) from inserted) , would be 4. @numrows = 4.
  • Next up, the update trigger checks "Is @numrows different than the count of rows in [inserted] that have a valid company number?".
  • This would turn into something like "Is 4 equal to 4?" (4 = 3+1, 3 updated + 1 inserted).
  • YES!!! YES!!! Exactly what we want.

 

What would it take for Viewpoint to fix their triggers?

Go through ALL their table triggers and replace [select @numrows = @@rowcount] with

  • For all INSERT triggers, use [select @numrows = select count(*) from inserted where KeyID is null], then go the rest of the trigger, and anywhere where the [inserted] table is referenced, make sure to add the same WHERE condition.
  • For all UPDATE triggers, do the same thing but use [where KeyID is NOT null] throughout the trigger. This would make sure that each time it interacts with the [inserted] table, it is only selecting rows that are UPDATE rows.
  • For all DELETE triggers, do a join between the [inserted] and [deleted] tables, and count rows that exist ONLY in the [deleted] table. Reason: An UPDATE operation stores copies of the rows BEFORE update in the [deleted] table, then AFTER the update in the [inserted] table. Thus we want to make sure in the DELETE trigger to only consider rows that are NOT also in the inserted table.

Why go to all this work?

So that we can start using MERGE startements in our stored procedures. This is particularly useful when importing large amounts of data and can seriously speed things up. Using SET BASED OPERATIONS is always recommended over LOOPS whenever possible, by pretty much every SQL performance guru I've run across. 

 

Viewpoint? What are your long term plans with the database?

  • Nathan Sutherland
  • May 10 2018
Company Cutting Edge Plumbing & Mechanical
Job Title / Role IT Admin
I need it... 1 month
  • Attach files
  • Travis Hatch commented
    December 07, 2021 22:06

    I need this perticularly on Table dbo.vDDUI Trigger vtDDUIi. It is only a one line change.
    we want to use a merge statement to override the GridCol field from UserSrc to UserDest.
    this way we can set create a standard column order accross all of our users.

  • Nathan Sutherland commented
    July 05, 2018 14:47

    Andre, do you have an updated list of those bugs? The pages linked say "Microsoft Connect has been retired" when trying to view the actual bug.

    Thanks for the warnings about MERGE.

    Perhaps we should be campaigning for VP to swap out their cursors for actual set-based operations?

    https://www.datavail.com/blog/curse-of-the-cursors-in-code-development/

    I'll keep this one on my wishlist though, as I'd love to see VP give a little more attention to their DB performance.

  • Andre Ferreira commented
    May 14, 2018 05:57

    Merge statements have their downfalls.

    I do agree if it's possible, but it's also important to keep this in mind. May be possible once minimum requirements change to SQL 2016.

     

    https://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/

Dear Viewpoint Suggestion Box contributor;

We at Viewpoint sincerely thank you for your contribution to Suggestion Box on how we can improve Viewpoint products. While we can’t do everything at once, we rely upon your feedback to help guide the prioritization of our product improvements, and Suggestion Box is a critical tool for us to understand and prioritize our customers’ needs. Viewpoint reviews Suggestion Box regularly for all of our products and updates statuses, adds comments, and performs various house-keeping (including deleting) as needed to ensure that Suggestion Box is maintained as a productive environment for product enhancements requests.

1515 SE Water Avenue, Suite 300, Portland, OR 97214 |  800.333.3197  | Contact Us | Terms of Use | Privacy | Support Policies

© 2023 Trimble Inc. All Rights Reserved. Viewpoint®, Vista™, Spectrum®, ProContractor™, Jobpac Connect™, Viewpoint Team™, Viewpoint Analytics™, Viewpoint Field View™, Viewpoint Estimating™, Viewpoint For Projects™, Viewpoint HR Management™, Viewpoint Field Management™, Viewpoint Financial Controls™, Vista Field Service™, Spectrum Service Tech™, ViewpointOne™, ProjectSight® and Trimble Construction One™ are trademarks or registered trademarks of Trimble Inc. or its affiliates in the United States and other countries. Other names and brands may be claimed as the property of others.