SQL Server Reference a Calculated Column

前端 未结 6 1075
傲寒
傲寒 2020-12-30 21:23

I have a select statement with calculated columns and I would like to use the value of one calculated column in another. Is this possible? Here is a contrived example to s

6条回答
  •  青春惊慌失措
    2020-12-30 21:38

    No.

    All the results of a single row from a select are atomic. That is, you can view them all as if they occur in parallel and cannot depend on each other.

    If you're referring to computed columns, then you need to update the formula's input for the result to change during a select.

    Think of computed columns as macros or mini-views which inject a little calculation whenever you call them.

    For example, these columns will be identical, always:

    -- assume that 'Calc' is a computed column equal to Salaray*.25
    SELECT Calc, Salary*.25 Calc2 FROM YourTable
    

    Also keep in mind that the persisted option doesn't change any of this. It keeps the value around which is nice for indexing, but the atomicity doesn't change.

提交回复
热议问题