有这样的一个表,docid是表的主键
 docid   infoid
  1         1
  2         1
  3         1
  4         2
  5         2
  6         2我输入docids=(1,2,3)怎么判断infoid是同一目录的,
如果在docids=(1,2,3)条件查询出的infoid都是1的话 输出1
如果在docids=(1,2,3)条件查询出的infoid包含多个的话  就输出infoid的个数比如docids=(1,2,3)条件查出的infoid都是1 那么输出的数是1
docids=(1,2,4)条件查出的infoid结果有1和2 那么它输出的数是2请问高手们这样的sql语言怎么写,我用的是sql server2005,谢谢!

解决方案 »

  1.   

    SELECT CASE WHEN (SELECT COUNT(DISTINCT INFOID) FROM TB WHERE DOCID IN (1,2,3))=1 THEN '是同一目录' ELSE '不是同一目录' END
      

  2.   

    大哥,请问DOCID IN (1,2,3))=1是什么意思 为什么要加‘=1’呢?
      

  3.   

    DECLARE @AttachDocIds varchar(100)
    set @AttachDocIds='1,2'
    SET @Ncount=(SELECT CASE WHEN (SELECT COUNT(DISTINCT InfoId) 
           FROM dbo.CMS_ATTACHDOC 
          WHERE AttachDocId IN (''+@AttachDocIds+''))=1 THEN 1 ELSE 2 END)错误是说'1,2'转换int错误   我传来的是string类型的 那怎么搞?
      

  4.   

    create table tb(docid int,infoid varchar(10))
    insert into tb select 1,1
    insert into tb select 2,1
    insert into tb select 3,1
    insert into tb select 4,2
    insert into tb select 5,2
    insert into tb select 6,2
    go
    create procedure gettype
    @t varchar(100)
    as
    begin
    declare @sqlstr nvarchar(1000)
    set @sqlstr='select count(*) from(select distinct infoid from tb where docid in(' + @t +'))t'
    exec(@sqlstr)
    end
    go
    exec gettype '1,2,3'
    exec gettype '1,2,4'
    go
    drop procedure gettype
    drop table tb
    /*
    12
    */