使用临时表的case语句

时间:2014-09-18 14:56:59

标签: sql sql-server-2012

我需要根据我的代码确定日期的结果。我想使用临时表和case语句,但这似乎不起作用。我正在使用SQL 2012谢谢

if object_id('tempdb..#abc') is not null drop table #abc
 create table #abc (abc varchar(25)
         , codetype varchar (25)
                        )
    Insert into #abc select 253.2, 'abc9'
    Insert into #abc select 428.41, 'abc10'

select pat.id
, edg.CODE
,  case when DATE > '9/1/2014' then abc.codetype = 'abc9' else abc.codetype = 'abc10' end

from pat
left join dx on pat.csn = dx.csn
left join edg on dx.dx = edg.dx
inner join #code9 as abc on abc.abc = edg.CODE 

where
 pat.DATE between '9/2/2014' and '9/2/2014'

1 个答案:

答案 0 :(得分:0)

此代码有几个值得注意的方面,例如使用具有相同值的between,混合左连接和内连接,以及在case中进行比较,这是一个无操作(由于where)。

但是,case中的分配肯定是错误的。这是不允许的:

case when DATE > '9/1/2014' then abc.codetype = 'abc9' else abc.codetype = 'abc10' end

也许你打算:

(case when [date] > '2014-09-01' then 'abc9' else 'abc10' end) as codetype
相关问题