select count(*)  from c_caseMain where charindex( ','+triallevel+',' , ','+@s1+',')>0

解决方案 »

  1.   

    select count(*)  from c_caseMain where triallevel in (1, 2,3)
    意为trialleveal的取值=1 or=2 or=3 双方类型均为int;而你的下一句当然报错,triallevel是int型,@s1为varchar型,类型不匹配,当然要报错啦
      

  2.   

    不对
    服务器: 消息 256,级别 16,状态 2,行 3
    数据类型 int 对于函数 charindex 无效。允许的类型为: char/varchar、nchar/nvarchar 和 binary/varbinary。注意:
    @s1 也可能是='3,2,8,44' 总之是一个整数序列
    在存储过程中如何能让数据库把他们从字符串中读出来。
    vb中有split函数,sql server中该怎么办?!
      

  3.   

    exec ('select count(*)  from c_caseMain where triallevel in ('' + @s1 + '')')
      

  4.   

    exec ('select count(*)  from c_caseMain where triallevel in (' + @s1 + ')')上面的错了。
      

  5.   

    --应该这样写:
    create proc p_test
    @s1 varchar(100)
    as
    exec('select count(*)  from c_caseMain where triallevel in ('+@s1+')')
    go--调用:
    exec p_test '1,2,3'
      

  6.   

    用一個臨時表@table_temp變數保存@sl的所有整數紀錄
    然後
    select count(*)  from c_caseMain where triallevel in (select * from @table_temp)
      

  7.   

    to wuyanfeng(耍库) 
       exec ('select count(*)  from c_caseMain where triallevel in (' + @s1 + ')')
    是可以执行的。
    不过,我想在存储过程中获得count(*)的值,怎么办?
    declare @triallevel varchar(100)
    declare @totals int
    set @triallevel='1,2,3'
       exec ('select @totals=count(*)  from c_caseMain where triallevel in (' + @s1 + ')')
    错误如下:
    服务器: 消息 137,级别 15,状态 1,行 1
    必须声明变量 '@totals'。
      

  8.   

    下面的代码我运行了,可以通过,你试试:declare @tmp_tbl table(num int)
    declare @int varchar(4000)
    declare @out     int
    set @int='1,2,3,4,5,6,7,8,9,'
    while @int<>''  
    begin
    insert into @tmp_tbl values(left(@int,(charindex(',',@int)-1)))
    set @int=right(@int,len(@int)-charindex(',',@int))
    end
    select set @out=count(*)  from 你的表 where num in(select * from @tmp_tbl)
      

  9.   

    更正:
    declare @tmp_tbl table(num int)
    declare @int varchar(4000)
    declare @out     int
    set @int='1,2,3,4,5,6,7,8,9,'
    while @int<>''  
    begin
    insert into @tmp_tbl values(left(@int,(charindex(',',@int)-1)))
    set @int=right(@int,len(@int)-charindex(',',@int))
    end
    select set @out=count(*)  from 你的表 where 你的字段 in(select * from @tmp_tbl)
      

  10.   

    存储过程:
    create proc var_tbl
    @int varchar(4000),
    @out  int output
    as
    declare @tmp_tbl table(num int)
    while @int<>''  
    begin
    insert into @tmp_tbl values(left(@int,(charindex(',',@int)-1)))
    set @int=right(@int,len(@int)-charindex(',',@int))
    end
    select @out=count(*)  from 你的表 where 你的字段 in(select * from @tmp_tbl)go条用实例:
    declare @test  int
    declare @test1  varchar(50)
    set @test1='1,4,5,23,26,'--注意,一定要以“,”结尾
    execute var_tbl @test1,@test output
    select @test
      

  11.   

    这个办法可以,有没有更好的办法?我给100分
    发信至[email protected]