I have the following script that creates a table:
CREATE TABLE [dbo].[Persons]
(
[Id] INT IDENTITY(1,1) NOT NULL,
[Name] NVARCHAR(250) NOT NULL,
[Surname] NVARCHAR(250) NOT NULL,
[NumberOfNotes] INT NOT NULL,
[TotalCash] FLOAT NULL,
[Result] AS ([NumberOfNotes] * [TotalCash] * (RAND()*(100-30)+30)),
CONSTRAINT [PK_Persons] PRIMARY KEY ([Id] ASC)
);
The only 2 possible scenarios in my situation when inserting a new record are:
- inserting a new record with [Name], [Surname], [NumberOfNotes], [Result]
- inserting a new record with [Name], [Surname], [NumberOfNotes], [TotalCash]
So I have to satisfy the following 2 criteria during creation
- I should be able to insert a new record where [Result] gets computed in case I am not inserting it. The computation formula is [NumberOfNotes] * [TotalCash] * SomeRandomNumber and I know for sure in this case the [TotalCash] will always be there for the formula an will never be null. But if instead I am inserting [Result] manually then it will insert my value and not the computed one ([TotalCash] can be null in this case).
- The SomeRandomNumber is fixed per record. I tried so far the (RAND()*(100-30)+30)) because I want a random number between 30 and 100 but it keeps changing for every select I run on the records. If instead I seed it as (RAND(Id)*(100-30)+30)) it's the same for multiple records.
If you want to be able to actually write to the
Result
column, you can't use a computed column.I would recommend having a regular column for
Result
, and using a view to implement the display logic, like: