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 8928701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:30:18+00:00 2026-06-15T08:30:18+00:00

I want to define a column in my table with following requirements: The column

  • 0

I want to define a column in my table with following requirements:

  1. The column should be insertable. If the value is provided in the INSERT statement, then it should be inserted.
  2. If the column is not referenced in the INSERT statement, then it should be set to the sum of two other columns.

Because of the first requirement, I cannot user computed columns, since they are not insertable. Because of the second, I cannot use DEFAULT, because it doesn’t allow referencing other columns in the definition. What other options do I have?

BTW, the column should be NOT NULL.

  • 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-06-15T08:30:20+00:00Added an answer on June 15, 2026 at 8:30 am

    Here you go, I’m demonstrating this with an example schema since you’ve not provided your real table/column names.

    Table:

    CREATE TABLE test
      (
         id   INT NOT NULL PRIMARY KEY IDENTITY, --made up key
         col1 INT, --first column to add, wasn't sure if this was nullable or not
         col2 INT, --second column to add, wasn't sure if this was nullable or not
         col3 INT NOT NULL --this is the column to optionally insert into
      ) 
    

    Here is the trigger definition:

    CREATE TRIGGER demo
    ON test
    INSTEAD OF INSERT
    AS
        INSERT INTO test (col1,col2,col3)
        SELECT inserted.col1,
               inserted.col2,
               CASE
                 WHEN inserted.col3 IS NULL THEN COALESCE(inserted.col1, 0) + COALESCE(inserted.col2, 0)
                 ELSE inserted.col3
               END
        FROM   inserted
    

    Basically it replaces any insert statement done on the table with the one in the trigger, so I check using the inserted temporary table to see if the value that is trying to be inserted into our non-nullable optional column, col3, is NULL. If it is, I replace it with the addition of col1 and col2 (I’m coalescing with zero as you didn’t mention if the two source columns are nullable or not).

    You can then run insert statements which either include it or not, despite the fact col3 is not nullable:

    INSERT INTO test(col1,col2)
    SELECT 12, 31
    GO
    
    INSERT INTO test(col1, col2, col3)
    SELECT 1, 2, 89
    GO
    

    Results are:

    ID  COL1 COL2 COL3
    ------------------  
    1   12   31    43
    2   1    2     89
    

    If the trigger wasn’t there, you could have got an error trying to run that first insert statement, telling you it couldn’t insert NULL into col3.

    Notice also that the second insert statement that specifies a value has not been replaced by the addition, as requested.

    Here’s a working SQL Fiddle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I want to define a class that supports __getitem__ , but does not allow
I have a restaurants table with a name column. I've defined the following index:
I have a table with a following format. PID ID Label Value ------------------------------------------ 1
In my sql server table, I want to add a computed column that is
I have a table that has an ntext column defined as [value1] [ntext] NOT
I want to define just one row in my table. To this row, I
I want to structure a table to mimic column level filters as row level
I want to make an AUTO_INCREMENT column in a database table,here is the syntax
I am trying to define the following model where Appointment table has foreign key

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.