视图中的SQL case语句

时间:2013-09-25 11:44:19

标签: sql sql-server tsql

ALTER View Myview AS

(SELECT
        TOP 1 ORGANIZATION_id
    FROM ORGANIZATION 
    WHERE code = mo.FunctionCode) org_id,
(SELECT
        TOP 1 ID
    FROM DEPARTMENT
    WHERE [NAME] = mo.Code) dep_id,
(SELECT
        TOP 1 Name
    FROM DEPARTMENT
    WHERE [ID] = dep_id) Dep_Name,
(SELECT
        TOP 1 Org_Name
    FROM CA_ORGANIZATION
    WHERE [organization_id] = org_id) Org_Name,
(CASE    
    WHEN Dep_Name = 'sales' AND Org_Name = 'Advanture'
        THEN 'salesTeam'
    WHEN Dep_Name ! = 'Sales' AND Org_Name = 'External'
        THEN 'ExternalTeam'
    ELSE
        'DefaultTeam'
    END) type

    FROM detail_view dv
    LEFT OUTER JOIN Global_oganization mo  ON  mo.area =dv.code

更改视图时出错

Invalid column name 'Dep_Name'.
Msg 207, Level 16, State 1, Procedure myview,
Invalid column name 'Org_Name'.
Msg 207, Level 16, State 1, Procedure myview,
Invalid column name 'Dep_Name'.
Msg 207, Level 16, State 1, Procedure myview,
Invalid column name 'Org_Name'.

我编写了CASE语句来验证部门名称和组织名称以确定类型。

帮我修复此错误。

2 个答案:

答案 0 :(得分:4)

试试这个 -

ALTER VIEW dbo.Myview
AS
    SELECT  /*missing in your query*/
            t.org_id
        ,   t.dep_id
        ,   t.Dep_Name
        ,   t.Org_Name
        ,   [type] = 
            CASE
                WHEN Dep_Name = 'sales' AND Org_Name = 'Advanture' THEN 'salesTeam'
                WHEN Dep_Name != 'Sales' AND Org_Name = 'External' THEN 'ExternalTeam' 
                ELSE 'DefaultTeam'
            END
    FROM (
        SELECT org_id = (
                    SELECT TOP 1 ORGANIZATION_id
                    FROM dbo.ORGANIZATION
                    WHERE Code = mo.FunctionCode
                )
            , dep_id = (
                    SELECT TOP 1 ID
                    FROM dbo.Department
                    WHERE [name] = mo.Code
                )
            , Dep_Name = (
                    SELECT TOP 1 name
                    FROM dbo.Department
                    WHERE [ID] = dep_id
                )
            , Org_Name = (
                    SELECT TOP 1 Org_Name
                    FROM dbo.CA_ORGANIZATION
                    WHERE [organization_id] = org_id
                )
        FROM dbo.detail_view dv
        LEFT JOIN dbo.Global_oganization mo ON mo.area = dv.Code
    ) t

答案 1 :(得分:2)

试试这个

SELECT t.*,(CASE    
                WHEN t.Dep_Name = 'sales' AND t.Org_Name = 'Advanture' THEN 'salesTeam'
                WHEN t.Dep_Name ! = 'Sales' AND t.Org_Name = 'External' THEN 'ExternalTeam'
                ELSE 'DefaultTeam'
                END) type

FROM (
(select top 1 ORGANIZATION_id from ORGANIZATION 
                                    where code = mo.FunctionCode) org_id,
        (select top 1 ID from DEPARTMENT
                                    where [NAME] = mo.Code) dep_id,
        (SELECT top 1 Name FROM DEPARTMENT
                                    WHERE [ID] = dep_id) Dep_Name,
        (SELECT TOP 1 Org_Name FROM CA_ORGANIZATION
                                WHERE [organization_id] = org_id) Org_Name


                From detail_view dv
                Left outer join Global_oganization mo  on  mo.area =dv.code) t