字符串有单引号要用双引号set   @strWhere='Publish=1   and   CID='+rtrim(str(@CID))+'   and   Type=0   and   JName   like   ''%'+@JName+'''%' --用rtrim转换一下,或用cast/convert

解决方案 »

  1.   

    set   @strWhere='Publish=1   and   CID='+ltrim(str(@CID))+'    and   Type=0
    and   JName   like   ''%'+ltrim(@JName)+'%'' ' 
      

  2.   

    你们的也许都正确,现在还有些问题不能解决,再帮帮好吗?里面用到两个存储过程,提示第 1 行: ')' 附近有语法错误。ALTER PROCEDURE sp_getproducts
    @title nvarchar(63),
    @category int,
    @PageIndex int=1,
    @orderby bit=1
    AS   
    declare @strWhere varchar(2000)
    --set @strWhere='Publish=1   and   CID='+rtrim(str(@CID))+'   and   Type=0   and   JName   like   ''%'+@JName+'''%' --用rtrim转换一下,或用cast/convert
    set @strWhere='category='+ltrim(str(@category))+' and title like ''%'+ltrim(@title)+'''%' --用rtrim转换一下,或用cast/convert
    exec GetRecordFromPage'products','id',16,@PageIndex,@orderby,@strWhereALTER PROCEDURE GetRecordFromPage
        @tblName      varchar(255),       -- 表名
        @fldName      nvarchar(63),       -- 字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(2000) = ''  -- 查询条件 (注意: 不要加 where)
    ASdeclare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(1000)       -- 临时变量
    declare @strOrder varchar(500)        -- 排序类型if @OrderType != 0
    begin
        set @strTmp = '<(select min'
        set @strOrder = ' order by [' + @fldName + '] desc'
    end
    else
    begin
        set @strTmp = '>(select max'
        set @strOrder = ' order by [' + @fldName +'] asc'
    endset @strSQL = 'select top ' + str(@PageSize) + ' * from ['
        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
        + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
        + @strOrderif @strWhere != ''
        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
            + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
            + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
            + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrderif @PageIndex = 1
    begin
        set @strTmp = ''
        if @strWhere != ''
            set @strTmp = ' where (' + @strWhere + ')'    set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    endexec (@strSQL)
      

  3.   

    --try
    alter   PROCEDURE   sp_getproducts
    @title   nvarchar(63),
    @category   int,
    @PageIndex   int=1,
    @orderby   bit=1
    AS      
    declare   @strWhere   varchar(2000)
    --set   @strWhere='Publish=1       and       CID='+rtrim(str(@CID))+'       and       Type=0       and       JName       like       ''%'+@JName+'''%'   --用rtrim转换一下,或用cast/convert
    set   @strWhere='category='+ltrim(str(@category))+'   and   title   like   ''%'+ltrim(@title)+'%'''   --用rtrim转换一下,或用cast/convertexec   GetRecordFromPage 'products','id',16,@PageIndex,@orderby,@strWhere