存储过程
ALTER PROCEDURE [dbo].[prStandardClient]
@Flag int=0,
@ID INT = 0,
@title nvarchar(50) = null,
@PageIndex int=1,
@PageSize int=5,
@sortid int = 0,
@level int = 1,--国家法律
@type tinyint = 1,--条文
@kwds varchar(200) = null,--关键字
@WhereSql Nvarchar(2000)=null,
@Recount int=0 output
AS
DECLARE @SQLSTR NVARCHAR(4000),@WHERE NVARCHAR(2000)
BEGIN
if @Flag = 3--根据条件查询列表
begin
SET @WHERE = N'WHERE 1=1'
if @kwds is not null and len(@kwds) > 0
begin
if @type = 0
begin
SET @WHERE = @WHERE + N' AND (TITLE LIKE ''%'+@kwds+'%'' or CONTENT LIKE ''%'+@kwds+'%'')'
    end
    ELSE IF @type = 1
    begin
    SET @WHERE = @WHERE + N' AND TITLE LIKE ''%'+@kwds+'%'''
    end
    ELSE IF @type = 2
    begin
    SET @WHERE = @WHERE +  N' AND CONTENT LIKE ''%'+@kwds+'%'''
    end
    ELSE IF @type = 3
    begin
SET @kwds=replace(@kwds,' ','')
    SET @WHERE = @WHERE +  N' AND replace(numId,'' '','''') LIKE ''%'+@kwds+'%'''
    end
end
if @sortid is not null and @sortid>0
begin
SET @WHERE = @WHERE +  N' AND '',''+standardSort_id+'','' like ''%,'+cast(@sortid as varchar(10))+',%''  '
end
IF @level <> 0
begin
set @WHERE = @WHERE + N' AND StandardLEVEL_ID='+CAST(@level as varchar(10))
end
set @SQLSTR = N'select top '+CAST(@PageSize as varchar(10))+N' ID,case standardLevel_id when 4 then dbo.fcStandardTitle(TITLE,standardSort_id) else Title end as Title,Promulgate_Organ as PromulgateOrgan,
          promulgate_date as promulgatedate, memo, numId,
          executive_date as executivedate,content  from 
(
select  * from 
(select top 99.999 percent * from standard  where len(cast(content as nvarchar(max)))>20 order by executive_date desc) t1
union all
select  * from
(select top 99.999 percent * from standard  where len(cast(content as nvarchar(max)))<=20 or len(cast(content as nvarchar(max))) is null order by executive_date desc) t2) t3   '+@WHERE+N' AND ID NOT IN 
(SELECT TOP '+CAST((@PageIndex-1)*@PageSize as varchar(10))+N' ID FROM 
(select  * from 
(select top 99.999 percent * from standard  where len(cast(content as nvarchar(max)))>20 order by executive_date desc) t1
union all
select  * from
(select top 99.999 percent * from standard  where len(cast(content as nvarchar(max)))<=20 or len(cast(content as nvarchar(max))) is null  order by executive_date desc) t2) t3 
'+@WHERE+' )
'
EXEC SP_EXECUTESQL @SQLSTR
SET @SQLSTR = N'SELECT @Recount=COUNT(0) FROM standard '+@WHERE 
--print @SQLSTR
EXEC SP_EXECUTESQL @SQLSTR,N'@Recount int output',@Recount=@Recount output
end
END执行代码:declare @p3 int
set @p3=NULL
exec prStandardClient @PageIndex=1,@PageSize=10,@Recount=@p3 output,@kwds=default,@sortid=49,@level=0,@type=1,@Flag=3
select @p3错误:在将 varchar 值 '5,49' 转换成数据类型 int 时失败解释:给@sortid赋其他值都不错,就赋5或者49时提示这个错误

解决方案 »

  1.   

     '5,49' 这是一个字符串,不拆分怎么能转换成int???
      

  2.   

    你想要一个或多个id 值 ,不应该定义成int类型的@sortid varchar(50) = '',--相应判断的地方也修改一下
    if @sortid is not null and @sortid <> ''
    begin
    SET @WHERE = @WHERE + N' AND '',''+standardSort_id+'','' like ''%,'+@sortid+',%'' '
    end