日期:2014-05-18  浏览次数:20564 次

求高手相助
CREATE PROCEDURE DeclareInquires
@CreateUser NVARCHAR(50) , 
@DeclareId NVARCHAR(10), 
@StartTime NVARCHAR(10), -----开始日期
@EndTime NVARCHAR(10), -----结束日期
@kinds NVARCHAR(50)
AS
begin
select DISTINCT ew.event_id,cl.code_desc as event,cl2.code_desc as status ,eh3.result_value as victims,
ew.create_user,eh1.result_value as happen_date, eh2.result_value as happen_time ,ew.create_time 
from event_worker ew,code_list cl,code_list cl2,event_head_worker eh1, event_head_worker eh2,event_head_worker eh3 
where ew.event=cl.code_value and convert(nvarchar(10),ew.status_id)=cl2.code_value and cl.type_id=3 
and cl2.type_id=1 and ew.event_id=eh1.event_id and eh1.event_id=eh2.event_id and eh2.event_id=eh3.event_id
and eh1.title_code ='T005'and eh2.title_code ='T006' and eh3.title_code ='T001'
and (@DeclareId ='' or ew.event_id = @DeclareId)


and case when @kinds ='Personal' then (@CreateUser ='' or ( ew.create_user = @CreateUser or eh3.result_value = @CreateUser) ) else (@CreateUser= '' or eh3.result_value like '%'+@CreateUser+ '%' ) end
and (@StartTime = '' or convert(nvarchar(10), ew.create_time,120)>=@StartTime)  
and (@EndTime = '' or convert(nvarchar(10), ew.create_time,120)<=@EndTime)  
and (@kinds = 'Personal' or ew.status_id='2') 
and eh3.active='Y' and eh2.active='Y' and eh1.active='Y' and ew.active='Y'order by ew.event_id desc
end
GO
标注为红色的语句一直提示=附近有错误。。。

------解决方案--------------------
SQL code

and (( @kinds ='Personal' and (@CreateUser ='' or ( ew.create_user = @CreateUser or eh3.result_value = @CreateUser) ))
 or (@kinds <>'Personal' and (@CreateUser= '' or eh3.result_value like '%'+@CreateUser+ '%' )))

------解决方案--------------------
SQL code
CREATE PROCEDURE DeclareInquires
@CreateUser NVARCHAR(50) ,  
@DeclareId NVARCHAR(10),  
@StartTime NVARCHAR(10),    -----开始日期
@EndTime NVARCHAR(10), -----结束日期
@kinds NVARCHAR(50)
AS
begin
select DISTINCT ew.event_id,cl.code_desc as event,cl2.code_desc as status ,eh3.result_value as victims,
ew.create_user,eh1.result_value as happen_date, eh2.result_value as happen_time ,ew.create_time  
from event_worker ew,code_list cl,code_list cl2,event_head_worker eh1, event_head_worker eh2,event_head_worker eh3  
where ew.event=cl.code_value and convert(nvarchar(10),ew.status_id)=cl2.code_value and cl.type_id=3  
and cl2.type_id=1 and ew.event_id=eh1.event_id and eh1.event_id=eh2.event_id and eh2.event_id=eh3.event_id
and eh1.title_code ='T005'and eh2.title_code ='T006' and eh3.title_code ='T001'
and (@DeclareId ='' or ew.event_id = @DeclareId)
and case when @kinds ='Personal' then (@CreateUser ='' or ( ew.create_user = @CreateUser or eh3.result_value = @CreateUser) )   --Then后面的or 无法唯一定位付给的值,不符合case语法要求。
        else (@CreateUser= '' or eh3.result_value like '%'+@CreateUser+ '%' ) end 
and (@StartTime = '' or convert(nvarchar(10), ew.create_time,120)>=@StartTime)   
and (@EndTime = '' or convert(nvarchar(10), ew.create_time,120)<=@EndTime)   
and (@kinds = 'Personal' or ew.status_id='2')  
and eh3.active='Y' and eh2.active='Y' and eh1.active='Y' and ew.active='Y'order by ew.event_id desc
end