How to reuse calculated columns avoiding duplicating the sql statement?

后端 未结 6 1351
忘掉有多难
忘掉有多难 2020-12-09 07:51

I have a lots of calculated columns and they keep repeting themselves, one inside of the others, including nested cases statements.

There is a really simplified ver

6条回答
  •  遥遥无期
    2020-12-09 08:40

    Do you want calculated results out of your table? In that case you can put the relevant calculations in scalar valued user defined function and use that inside your select statement.

    Or do you want the calculated results to appear as columns in the table, then use a computed column:

    CREATE TABLE Test(
        ID INT NOT NULL IDENTITY(1,1),
        TimesTen AS ID * 10
    )
    

提交回复
热议问题