求TSQL语句: 给定一个字符串,里面含有逗号分割的本号列表,要求查寻出符合本号的行,如
yhb
zcm     qh    bh
100001  1     1
100002  2     1
100003  1     2
100004  1     2
100005  1     3
100005  1     4设str_bh='1,3,4'时,出qh=1 bh在1,3,4的行,如下:yhb2
zcm     qh    bh
100001  1     1
100005  1     3
100005  1     4

解决方案 »

  1.   

    declare @str_bh varchar(100)
    set @str_bh='1,3,4'exec ('select * from table_name where qh=1 and bh in ('+@str_bh+')')
    --or
    select * from table_name where qh=1 and charindex(','+ltrim(bh)+',', ','+@str_bh+',')>0
      

  2.   

    Limpire again!
    Thanks for your speedy aid.
      

  3.   

    And the conclusion of the post will be speedy, too!
    Looking forward to your next appearence!