大意如下,凑活着看吧,具体应该怎么写啊:
@Count=select count(0) as B from Table2
if(Count<>0 )
{return 
select (aaa.A)/(bbb.B)
from
((select count(0) as A from Table1)aaa
,
(select count(0) as B from Table2)bbb
)的值}
else
{
return 0
}

解决方案 »

  1.   

    select 
    case when (select count(*) from Table2)>0 
    then (select count(*) from Table1)/(select count(*) from Table2) 
    else 0 end 
    as 结果列
      

  2.   

    create proc my_proc
    asdeclare @n1 int
    declare @n2 intselect @n1=count(0) from Table1
    select @n2=count(0) from Table2if @n1 <> 0
    begin
    if @n2 =0--除数为0
    return 0
    else
    return @n2/@n2
    end
    else
    return 0
      

  3.   

    set @Count=(select count(0) as B from Table2 )
    if(Count <>0 ) 
    beginreturn (
    select (aaa.A)/(bbb.B) 
    from 
    ((select count(0) as A from Table1)aaa 

    (select count(0) as B from Table2)bbb 
    )
    )end 
    else 
    return 0 
      

  4.   

    --一定要判断被除数是否为0,否则错误
    create proc my_proc
    asdeclare @n1 int
    declare @n2 intselect @n1=count(0) from Table1
    select @n2=count(0) from Table2if @n1 <> 0
    begin
        if @n2 =0--除数为0
            return 0
        else
        return @n1/@n2
    end
    else
        return 0
      

  5.   

    create proc ReturnValue 
    as
    begin
       return SELECT CASE 
                     WHEN (select count(*) from table_b) > 0 THEN (SELECT count(*) from table_a)/(select count(*) from table_b)
                     ELSE  0
                     END AS rate
    end;