@a,@b,@c,@d,@e判断“5个数中任意两个都不相等”的SQL语句怎么写?

解决方案 »

  1.   

    declare @a int,@b int,@c int,@d int,@e int,@s varchar(1000)
    select @a = 1,@b = 2 ,@c = 1 ,@d = 4 ,@e = 5, @s = ''
    select @s = @s + ',' + convert(varchar, id) from 
    (
    select @b [id]
    union select @c
    union select @d
    union select @e
    ) a
    if (select charindex( ',' + convert(varchar,@a) + ',' , @s + ',') > 0)
    begin
    --有相同的数字
    end 
      

  2.   

    declare @sql nvarchar(4000),@rowcount int,@count varchar(1000)
    select @count = N'1,29,3,9,1'select @sql = N'declare @tb table ([rowcount] int)'+char(13)+'insert into @tb '+stuff(replace(','+@count,',',' union select '),1,6,'')+char(13)+'select @rowcount = @@rowcount'
    execute sp_executesql @sql,N'@rowcount int output',@rowcount outputif @rowcount = len(@count)-len(replace(@count,',',''))+1
    select '无重复数字'
    else 
    select '有重复数字'
      

  3.   

    if exists(select * from (select @a ID union All select @b union All  select @c union All  select @d union All  select @e) as 表
    group by ID
    having count(*)>1)
    print '相等'
    else
    print '不等'
      

  4.   

    if (@a=@b or @a=@c or @a=@d or @a=@e or @b=@c or @b=@d or @b=@e or @c=@d or @c=@e or @d=@e)
    print '相等'
    else
    print '不等'
      

  5.   

    if (select count(distinct a) from (select @a a union select @b union select @b union select @c union select @d union select @e) as a)<5
    print '有相等的数'