0

直到我今天需要设置一个计算列之前,我才听说过计算列,所以请原谅我的愚蠢。这是我的创建表语句

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

如果 match org 或 bypass 在其中有 yes,我希望 win 列自动计算为 yes...谢谢

4

3 回答 3

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]
于 2012-05-22T10:47:55.807 回答
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 
于 2012-05-22T10:52:33.067 回答
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
于 2012-05-22T10:51:03.670 回答