刚才的那个帖子已经结了,分都给了,下面接着上面的问题问。
函数已经知道咋写了。CREATE function getWellFlag
(@WellUNID varchar(50))
returns varchar(100)
as
begin
declare @flag varchar(100);
if exists(select 1 from tb where WellUNID=@WellUNID) --这里是比较复杂查询
set @flag='Y'
else
set @flag='N'
return @flag
end在我注释的地方,是一个复杂的查询,表tb如下:
WellUNID  状态
 1         A
 1         B
要求同时查到WellUNID=1并且状态=A,WellUNID=1并且状态=B的两条记录时,@flag返回Y

解决方案 »

  1.   


    在后面加个  if  exists(select 1 from tb where wellunid=@wellunid and 状态='A') and
    exists(select 1 from tb where wellunid=@wellunid and 状态='b')
     set @flag='Y'
      

  2.   

    if exists(select 1 
              from tb t 
              where exists(select 1 from tb where 状态='A' and WellUNID=t.WellUNID and WellUNID=@WellUNID)
              and exists(select 1 from tb where 状态='B' and WellUNID=t.WellUNID and WellUNID=@WellUNID)