今天早上在bz以及各位大虾的热心帮助下完成了分页...但做着做着又发现问题了....下面这个是早上bz写的动态查询
set @s='
select top '+ltrim(@pageSize)+' xh,xm,xb 
from jw_xsb 
where xh not in (
    select top '+ltrim((@page-1)*@pageSize)+' xh 
    from jw_xsb 
    order by xh asc) 
order by xh asc'现在我需要这样一个效果,我要添加一个条件,就是要获得的数据要求当班级代码等于我输入的班级代码,bjdm=@bjdm
我是这样写的
set @s='
select top '+ltrim(@pageSize)+' xh,xm,xb 
from jw_xsb 
where xh not in (
    select top '+ltrim((@page-1)*@pageSize)+' xh 
    from jw_xsb where bjdm='+ltrim(@bjdm)+'    order by xh asc) 
order by xh asc'
表里面的列bjdm的类型是bjdm char(4)
定义的类型是@bjdm char(4),
这一列里有3个数据01,02,A01,当我输入的jgdm是“A07”时就是“列名 'A07' 无效”
输入其他两个就是下面这个错误提示
“将 varchar 值 'A07 ' 转换为数据类型为 int 的列时发生语法错误。”难道是动态的时候不能用条件查询某一列???

解决方案 »

  1.   


    --try:
    set @s='
    select top '+ltrim(@pageSize)+' xh,xm,xb 
    from jw_xsb 
    where xh not in (
        select top '+ltrim((@page-1)*@pageSize)+' xh 
        from jw_xsb where bjdm='''+ltrim(@bjdm)+'''   order by xh asc) 
    order by xh asc'
      

  2.   

    字符变量:@bjdm两边要加3个'''的。
      

  3.   

    create table #(code varchar(20),value int)
    declare @sql varchar(200)
    set @sql='insert into # select ''AAA'',10' 
    -- AAA之前的一对单引号与之后的一对单引号分别代表一个单引号print @sql  --查看这个字符串的实际内容
    /*
    insert into # select 'AAA',10
    */exec(@sql)
    select * from #
    /*
    code                 value       
    -------------------- ----------- 
    AAA                  10
    */--如果需要insert一个值为'A''AA'的字符串,如下:
    set @sql='insert into # select ''A''''AA'',10'  
    --两层嵌套的字符串内部,一个单引号需要经过两次转义,于是变成了4个单引号print @sql  --查看这个字符串的实际内容
    /*
    insert into # select 'A''AA',10
    */exec(@sql)select * from #
    /*
    code                 value       
    -------------------- ----------- 
    AAA                  10
    A'AA                 10
    */
    drop table #
      

  4.   

    当你想在动态里表达
    a='a'的时候 要写成 a=''a''
    规则看我上面的资料
      

  5.   

    set @s='
    select top '+ltrim(@pageSize)+' xh,xm,xb 
    from jw_xsb 
    where xh not in (
        select top '+ltrim((@page-1)*@pageSize)+' xh 
        from jw_xsb where bjdm='''+@bjdm+'''   order by xh asc) 
    order by xh asc'
    GO
      

  6.   


    set @s='
    select top '+ltrim(@pageSize)+' xh,xm,xb 
    from jw_xsb 
    where xh not in (
        select top '+ltrim((@page-1)*@pageSize)+' xh 
        from jw_xsb where bjdm='''+ltrim(@bjdm)+'''   order by xh asc) 
    order by xh asc'
    在动态SQL中单引变双引