数据库:Sql server 2005
ALTER proc [dbo].[test]@iscj bit,
@yongdi nvarchar(50)
asdeclare @sql nvarchar(3000)
set @sql=' select top 5 * from table where levelet=2 'if (@yongdi<>'')
begin
set @sql=@sql+' and yongdi='+@yongdi
endif(@iscj<>'') --这里该如何判断,不论传什么值进去,都查不出false的记录 begin
set @sql=@sql+' and iscj='+cast(@iscj as varchar(10))
endexec (@sql)

解决方案 »

  1.   

    if(@iscj<>'')
    begin
    set @sql=@sql+' and iscj='+cast(@iscj as varchar(1))
    end
      

  2.   

    不需要用动态执行
    ALTER proc [dbo].[test]@iscj bit,
    @yongdi nvarchar(50)
    as
    begin
         select top 5 * from [table]
           where levelet=2 
             and yongdi=ISNULL(@yongdi,yongdi)
            and iscj=ISNULL(@iscj,iscj)
    end
      

  3.   

    @iscj=0 -- @iscj=1
    这个是布尔值
      

  4.   

    还是不对,始终不能执行 为false的
      

  5.   

    bit是0和1
    不是true和false
      

  6.   

    在上层代码里可以直接传入 true或false.
      

  7.   


    if(@iscj<>'') --这里该如何判断,不论传什么值进去,都查不出false的记录begin
    set @sql=@sql+' and iscj='''+@iscj+''''
    end
      

  8.   


    if(@iscj<>'false' or @iscj<>'true') 
    begin 
    set @sql=@sql+' and iscj='''+@iscj+'''' 
    end