I have a SSDT Project in Visual Studio 2015 which has a table definition as follows that includes a computed column.
CREATE TABLE [dbo].[Test] (
[Id] INT NOT NULL,
[Cost] MONEY NULL,
[Vat] AS (Cost * 0.2),
[Total] AS (Cost + (Cost * 0.2)),
PRIMARY KEY CLUSTERED ([Id] ASC)
);
When I deploy to SQL Server I get the following:
CREATE TABLE [dbo].[Test](
[Id] [int] NOT NULL,
[Cost] [money] NULL,
[Vat] AS ([Cost]*(0.2)),
[Total] AS ([Cost]+[Cost]*(0.2)),
PRIMARY KEY CLUSTERED
(
[Id] ASC
)
My question is why do the brackets in my computed column disappear? Do I need to select an option somewhere to preserve them or am I missing something?
Computed columns aren't stored as text. They're stored in an internal, efficient form. That means that, any elements of the definition that aren't critical to the actual meaning of the computed column aren't retained.
Since *
has a higher precedence than +
, Cost + (Cost * 0.2)
and [Cost]+[Cost]*(0.2)
have exactly the same meaning. The server doesn't generate extra brackets when precedence already takes care of matters.