计算专栏 - 需要帮助撰写声明

时间:2012-05-22 10:43:44

标签: sql sql-server database calculated-columns

直到今天我需要设置一个计算机列,才知道计算列,所以请原谅这个愚蠢。这是我的create table语句

CREATE TABLE [Match](

    [Match Org] [nvarchar](10) NULL,
    [Bypass] [nvarchar](10) NULL,
    [win] as case when [Match Org] == 'yes' or [Bypass] == 'yes' then 'yes' else 'no'

) ON [PRIMARY]

GO

我希望win列自动计算为yes,如果匹配组织或旁路中有肯定的话...谢谢

3 个答案:

答案 0 :(得分:3)

CREATE TABLE [Match](

    [Match Org] [nvarchar](10) NULL,
    [Bypass] [nvarchar](10) NULL,
    [win] AS CASE WHEN  ( [Match Org] = 'yes'    --- equality check is:  =
                       OR [Bypass] = 'yes' )     --- not:  ==
                  THEN 'yes' 
                  ELSE 'no'
             END                                 --- END was missing
        PERSISTED             --- you may also want to make
                              --- the column PERSISTED 
) ON [PRIMARY]

答案 1 :(得分:1)

CREATE TABLE [Match]( 

    [Match Org] [nvarchar](10) NULL, 
    [Bypass] [nvarchar](10) NULL, 
    [win] as case  when  [Match Org] = 'yes' or [Bypass] = 'yes' then 'yes' else 'no' end
)

GO 

答案 2 :(得分:0)

你可以做的是

表格中的列创建

[win]  AS ([dbo].[GetColValue]([Match Org],[Bypass ]))

您可以创建的功能

ALTER FUNCTION [dbo].[GetColValue](@MatchOrg nvarchar(10),@Bypass nvarchar(10))
RETURNS varchar(10)
AS
    BEGIN
        DECLARE @Result varchar(10)
        SET @Result = case 
                  when ([Match Org] = 'yes' or [Bypass] = 'yes' )
                  then 'yes' else 'no' 
                  end        
         RETURN 
        (
            @Result
        )
    END
相关问题