sql查询显示状态代码而不使用状态码表

时间:2013-10-15 07:53:31

标签: sql sql-server

如何根据下面的表结构在输出中显示状态代码?

表名:

请求

列:

RequestID,RequestMessage,RequestStatusID,RequestPriorityID

示例

假设RequestStatusID转换为:

1 = IN_PROGRESS, 2 = CANCELED, 3 = COMPLETED

仅为了记录,我不允许创建一个新表来保存请求状态。 我必须以某种方式将它们包含在select语句中,并且请求状态代码是否在select语句中进行硬编码无关紧要。

我很想知道是否有另一种方法可以实现这一点,而无需使用额外的表格。

4 个答案:

答案 0 :(得分:4)

案例陈述:

SELECT RequestID,
       CASE RequestStatusID 
       WHEN 1 THEN 'IN_PROGRESS'
       WHEN 2 THEN 'CANCELED'
       WHEN 3 THEN 'COMPLETED'
       END,
       RequestDesc,
       RequestPriorityID
 FROM #Request

如果使用SQL Server 2012,则使用CHOOSE()函数:

SELECT RequestID,
       CHOOSE(RequestStatusID,'IN_PROGRESS','CANCELED','COMPLETED'),
       RequestDesc,
       RequestPriorityID
 FROM #Request

答案 1 :(得分:1)

您可以使用CASE

SELECT  *,
        CASE WHEN RequestStatusID = 1 THEN 'IN_PROGRESS'
             WHEN RequestStatusID = 2 THEN 'CANCELED'
             ELSE 'COMPLETED' 
        END StatusName
FROM    tableName

答案 2 :(得分:0)

您可以使用case,但实际上我喜欢派生表的想法,因为将来更容易更改:

select
    R.*, C.Name as RequestStatusName
from Request as R
    left outer join (values
         (1, 'IN_PROGRESS'),
         (2, 'CANCELED'),
         (3, 'COMPLETED')
    ) as C(Id, Name) on C.Id = R.RequestStatusID 

因此,您可以添加任意数量的列,例如:

select
    R.*,
    C.Name as RequestStatusName,
    C.Description as RequestStatusDescription
from Request as R
    left outer join (values
         (1, 'IN_PROGRESS', 'In progress description'),
         (2, 'CANCELED', 'Canceled description'),
         (3, 'COMPLETED', 'Completed description')
    ) as C(Id, Name, Description) on C.Id = R.RequestStatusID 

如果您必须经常加入,可以创建视图:

create view vw_RequestStatusNames
as
    select C.Id, C.Name, C.Description
    from (values
         (1, 'IN_PROGRESS', 'In progress description'),
         (2, 'CANCELED', 'Canceled description'),
         (3, 'COMPLETED', 'Completed description')
    ) as C(Id, Name, Description)

并在连接中使用它:

select
    R.*,
    C.Name as RequestStatusName,
    C.Description as RequestStatusDescription
from Request as R
    left outer join vw_RequestStatusNames as C on C.Id = R.RequestStatusID 

<强> => SQL FIDDLE DEMO

答案 3 :(得分:0)

如果允许您创建一个函数,则可以使用Table Valued Function,因为您不仅可以在此处使用它,还可以在其他查​​询中使用它。基本上易于维护和一致的结果。

表值函数:

create function dbo.RequestStatus()
returns @ReqStatus table
(
  id int identity(1,1), 
  status varchar(50)
)
as
begin

   insert into @ReqStatus values
   ('IN_PROGRESS'), ('CANCELED'), ('COMPLETED')

   return
end

GO

功能使用和查询( Fiddle demo ):

select r.RequestID,r.RequestMessage,r.RequestStatusID,
       r.RequestPriorityID, s.status
from Request r join (select id,status from dbo.RequestStatus()) s
    on r.RequestStatusID = s.id

结果:

| REQUESTID | REQUESTMESSAGE | REQUESTSTATUSID | REQUESTPRIORITYID |      STATUS |
|-----------|----------------|-----------------|-------------------|-------------|
|         1 |           Abcd |               1 |                 1 | IN_PROGRESS |
|         2 |           lmno |               2 |                 1 |    CANCELED |
|         3 |           pqrs |               3 |                 1 |   COMPLETED |