[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.4

Need to fix this trigger to run on both SQL7 and SQL 2000.

Asked by Dexstar in MS SQL Server

Tags: column, name, sql, trigger

I need to have code for this trigger that works properly on both SQL7 and SQL 2000.  If I try to run the original version (made for SQL7) on SQL2000, it gives me an error.  With a small change, I can make it so that SQL 2000 accepts it, but then SQL7 doesn't like it.  Help me come up with a version that works on both so I can have one script that I can use on both platforms.

FYI:  I can't accept any answer that uses cursors or that doesn't work properly on batch updates.

Here is the trigger code:

-------------------------------------
-- SQL Server 7.0
-------------------------------------
CREATE TRIGGER tblAccountEntries_Maintain_Balance
ON dbo.tblAccountEntries
FOR INSERT, UPDATE, DELETE
AS
      -- Do not return row count
      SET NOCOUNT ON
      -- Update the balance if the amount, AssetID, or AccountID has changed
      -- OR if the record is being deleted (indicated by COLUMNS_UPDATED() = 0)
      IF ( UPDATE(Amount) OR UPDATE(AssetID) OR UPDATE(AccountID) OR (COLUMNS_UPDATED() = 0) )
            BEGIN
                  -- Update the Balances
                  UPDATE
                        dbo.tblAccountBalances
                  SET
                        CurrentBalance = AB.CurrentBalance + Change
                  FROM
                        dbo.tblAccountBalances AB INNER JOIN
                         (
                              SELECT COALESCE(I.AccountID, D.AccountID) AS AccountID,
                                    COALESCE(I.AssetID, D.AssetID) AS AssetID,
                                    SUM(COALESCE(I.Amount, 0) - COALESCE(D.Amount,0)) AS Change
                                    FROM Inserted I FULL OUTER JOIN Deleted D ON
                                          I.AccountID = D.AccountID
                                          AND
                                          I.AssetID = D.AssetID
                                          AND
                                          I.EntryID = D.EntryID
                                    GROUP BY
                                          COALESCE(I.AccountID, D.AccountID),
                                          COALESCE(I.AssetID, D.AssetID)
                         ) AS ChangeData ON
                              AB.AssetID = AssetID
                              AND
                              AB.AccountID = AccountID

                  -- Rollback on error
                  IF ( @@ERROR <> 0 )
                        BEGIN
                              ROLLBACK TRANSACTION
                        END
            END


-----------------------------
-- SQL Server 2000
------------------------------
CREATE TRIGGER tblAccountEntries_Maintain_Balance
ON dbo.tblAccountEntries
FOR INSERT, UPDATE, DELETE
AS
      -- Do not return row count
      SET NOCOUNT ON
      -- Update the balance if the amount, AssetID, or AccountID has changed
      -- OR if the record is being deleted (indicated by COLUMNS_UPDATED() = 0)
      IF ( UPDATE(Amount) OR UPDATE(AssetID) OR UPDATE(AccountID) OR (COLUMNS_UPDATED() = 0) )
            BEGIN
                  -- Update the Balances
                  UPDATE
                        dbo.tblAccountBalances
                  SET
                        CurrentBalance = AB.CurrentBalance + Change
                  FROM
                        dbo.tblAccountBalances AB INNER JOIN
                         (
                              SELECT COALESCE(I.AccountID, D.AccountID) AS AccountID,
                                    COALESCE(I.AssetID, D.AssetID) AS AssetID,
                                    SUM(COALESCE(I.Amount, 0) - COALESCE(D.Amount,0)) AS Change
                                    FROM Inserted I FULL OUTER JOIN Deleted D ON
                                          I.AccountID = D.AccountID
                                          AND
                                          I.AssetID = D.AssetID
                                          AND
                                          I.EntryID = D.EntryID
                                    GROUP BY
                                          COALESCE(I.AccountID, D.AccountID),
                                          COALESCE(I.AssetID, D.AssetID)
                         ) AS ChangeData ON
                              AB.AssetID = ChangeData.AssetID
                              AND
                              AB.AccountID = ChangeData.AccountID

                  -- Rollback on error
                  IF ( @@ERROR <> 0 )
                        BEGIN
                              ROLLBACK TRANSACTION
                        END
            END

---
(Yes, I really do always format my SQL code that way...)

The only change is to this part, where in the SQL2000 version, I specify the sub-query's alias on the columns:
                         ) AS ChangeData ON
                              AB.AssetID = ChangeData.AssetID
                              AND
                              AB.AccountID = ChangeData.AccountID

If I run the SQL7 version on SQL2K, I get this error:
      Server: Msg 209, Level 16, State 1, Procedure tblAccountEntries_Maintain_Balance, Line 2 Ambiguous column name 'AssetID'.

If I run the SQL2K version on SQL7, I get this error:
      Error 107: the column prefix 'ChangeData' does not match with a table name or alias name used in the query

Oh, and I'm always open to any other suggestions you guys could make about optimizing the query or anything like that.  This trigger runs quite frequently, so anything that improves the performance is worth extra points.

Dex*
[+][-]03/30/04 01:07 PM, ID: 10717831Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: MS SQL Server
Tags: column, name, sql, trigger
Sign Up Now!
Solution Provided By: danblake
Participating Experts: 2
Solution Grade: A
 
[+][-]03/30/04 12:34 PM, ID: 10717443Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/30/04 12:40 PM, ID: 10717502Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/30/04 12:43 PM, ID: 10717545Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/30/04 12:57 PM, ID: 10717708Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/30/04 01:22 PM, ID: 10718008Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/30/04 01:33 PM, ID: 10718134Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/30/04 01:39 PM, ID: 10718189Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03/30/04 03:40 PM, ID: 10719307Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/30/04 03:59 PM, ID: 10719425Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/31/04 09:38 AM, ID: 10725585Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04/02/04 12:16 PM, ID: 10744076Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92