Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6373279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:20:39+00:00 2026-05-25T01:20:39+00:00

I have a column in my table called Value1. I then have a computed

  • 0

I have a column in my table called Value1. I then have a computed column, Value2, with the formula of;

(CASE WHEN [Value1] > [Value2] THEN [Value1] ELSE [Value2] END)

I can’t save this as SQL Server balks at the self-reference of the Value2 computed column in the formula.

Is there anything else I can do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:20:40+00:00Added an answer on May 25, 2026 at 1:20 am

    It seems like you need to keep track of not only what value1 is now but also what value1 used to be. You won’t be able to do that with a computed column, because it can only react to the current value, not itself or the previous value.

    I suggest an INSTEAD OF TRIGGER as opposed to a computed column. Here is a simple example:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.SparkyMark
    (
        [key]    INT IDENTITY(1,1) PRIMARY KEY,
        [string] VARCHAR(32),
        Value1   INT,
        Value2   INT
    );
    GO
    

    An INSTEAD OF INSERT TRIGGER:

    CREATE TRIGGER dbo.SparkyMark_BeforeInsert
    ON dbo.SparkyMark
    INSTEAD OF INSERT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        INSERT dbo.SparkyMark([string], Value1, Value2)
            SELECT [string], Value1, Value1 FROM inserted;
    END
    GO
    

    An INSTEAD OF UPDATE TRIGGER:

    CREATE TRIGGER dbo.SparkyMark_BeforeUpdate
    ON dbo.SparkyMark
    INSTEAD OF UPDATE
    AS
    BEGIN
        SET NOCOUNT ON;
    
        UPDATE sm
            SET [string] = i.[string],
                Value1 = i.Value1, 
                Value2 = CASE WHEN sm.Value2 < i.Value1 THEN i.Value1 ELSE sm.Value2 END
            FROM
                dbo.SparkyMark AS sm
            INNER JOIN
                inserted AS i
                ON sm.[key] = i.[key];
    END
    GO
    

    Now let’s insert a couple of rows and prove we can maintain Value2 without ever inserting or updating that column directly:

    INSERT dbo.SparkyMark([string], Value1) SELECT 'foo', 3;
    INSERT dbo.SparkyMark([string], Value1) SELECT 'foo', 5;
    
    -- Value1 and Value2 are the same:
    SELECT * FROM dbo.SparkyMark ORDER BY [key];
    
    -- they will still be the same because the new Value1 > old Value2:
    UPDATE dbo.SparkyMark SET Value1 = Value1 + 1;
    SELECT * FROM dbo.SparkyMark ORDER BY [key];
    
    -- now they will be one less because the new Value1 < old Value2:
    UPDATE dbo.SparkyMark SET Value1 = Value1 - 1;
    SELECT * FROM dbo.SparkyMark ORDER BY [key];
    
    -- in row 1 Value1 drops by 2 but Value2 stays the same:
    UPDATE dbo.SparkyMark SET Value1 = Value1 - 2 WHERE [key] = 1;
    SELECT * FROM dbo.SparkyMark ORDER BY [key];
    
    -- and finally we get both values in both rows equal again:
    UPDATE dbo.SparkyMark SET Value1 = Value1 + 5;
    SELECT * FROM dbo.SparkyMark ORDER BY [key];
    

    Clean-up:

    DROP TRIGGER dbo.SparkyMark_BeforeInsert, dbo.SparkyMark_BeforeUpdate;
    DROP TABLE dbo.SparkyMark;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a column called Type , which can have three
If I have table with a Date column called myDate , with values like
I have a table column called post_tags within a table called posts where assigned
MS SQL Server 2000 I have a column in Table A called Name. I
I have a table called Event. A column has duration--which is...how long that event
I have a table called ApprovalTasks... Approvals has a status column I also have
I have a table called scheduler_sched which has several columns, including a column called
I have a table called, tblClient with an encrypted column called SSN . Due
Suppose I have a table called Companies that has a DepartmentID column. There's also
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.