SQL Server中的动态字段

时间:2011-12-06 20:07:39

标签: sql sql-server sql-server-2008

如何在SQL Server表中创建动态字段?我想将它添加到create table命令:

isDeleted = CASE WHEN DeleteDate is null THEN 0 ELSE 1

2 个答案:

答案 0 :(得分:5)

您正在寻找创建computed column

CREATE TABLE dbo.YourTable (
    ...
    isDeleted AS CASE WHEN DeleteDate IS NULL THEN 0 ELSE 1 END 
);

答案 1 :(得分:1)

我不是100%确定你的动态字段是什么意思,但我认为这就是你所要求的:

SELECT    MyTable.Foo,
          CASE WHEN MyTable.Foo = "FOO" THEN
              0
          ELSE
              1
          END AS isDeleted
FROM      MyTable