2012-05-22 31 views
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 

我想勝利柱自動計算爲yes,如果任一比賽組織或旁路有是在他們......謝謝

回答

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] 
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 
+0

WTF .... atlest給予爲什麼它-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