菜鸟遇到个问题 我写了个存储过程 分页的 目的是 从一个表 根据不同的分类来查询并且分页
前3个是分页的设置 第四个 是分类的 数据 第五个 是分类的子项 每次运行报错 无法将 'y'转换为int ALTER procedure [dbo].[fyinnovationbyzitem](@pagesize int,@currpage int,@totalrecords int output,@zid int,@zitem char)
as
begin
declare @total int;
set @total = (select COUNT(*) from Innovation where Cast(@zitem as nvarchar(50)) = @zid);
set @totalrecords = @total;
select top(@pagesize) * from Innovation where Id not in (select top((@currpage - 1)*@pagesize) Id from Innovation where Cast(@zitem as nvarchar(50)) = @zid) and Cast(@zitem as nvarchar(50)) = @zid;
end下边是调用存储过程的代码
asp。netpublic IList<Innovation> fyinnovationbyzitem(int pagesize, int currpage, out int totalrecords,int zid,string zitem)
        {
            IList<Innovation> lt = new List<Innovation>();
            string cs = Dbunit.ConnectionString;
            SqlConnection conn = new SqlConnection(cs);
            SqlParameter[] sp = new SqlParameter[5];
            sp[0] = new SqlParameter("@pagesize", pagesize);
            sp[1] = new SqlParameter("@currpage", currpage);
            sp[2] = new SqlParameter("@totalrecords", SqlDbType.Int, 4);
            sp[2].Direction = ParameterDirection.Output;
            sp[3] = new SqlParameter("@zid", zid);
            sp[4] = new SqlParameter("@zitem", zitem);
            using (SqlDataReader sdr = Dbunit.ExecuteReader("fyinnovationbyzitem", CommandType.StoredProcedure, sp))
            {
                if (sdr != null)
                {
                    Innovation inn = null;
                    while (sdr.Read())
                    {
                        inn = new Innovation();
                        inn.Id = sdr.GetInt32(0);
                        inn.Img = sdr.GetString(1);
                        inn.Contents = sdr.GetString(2);
                        inn.Peiyang = sdr.GetInt32(3);
                        inn.Keyan = sdr.GetInt32(4);
                        inn.Lingyu = sdr.GetInt32(5);
                        inn.State = sdr.GetInt32(6);
                        inn.Shengyuan = sdr.GetInt32(7);
                        inn.Year = sdr.GetString(8);
                        inn.Click = sdr.GetInt32(9);
                        inn.Title = sdr.GetString(10);
                        lt.Add(inn);
                    }
                }
            }
            totalrecords = Convert.ToInt32(sp[2].Value);
            return lt;
        }

解决方案 »

  1.   

    你的@zid是int,而@zitem是char,而你的代码里 有这样的比较: Cast(@zitem as nvarchar(50)) = @zid
    会不会是@zitem里有字符呢,转化不成int 
      

  2.   

    对 因为 zitem是子项 就是select * from users where sex = 0
    类似于这个  sex 就是@zitem  0是@zid
      

  3.   

    @zitem char)--char如果不带参数,默认就是char(1)
      

  4.   

    试试ALTER procedure [dbo].[fyinnovationbyzitem](@pagesize int,@currpage int,@totalrecords int output,@zid int,@zitem char)--char如果不带参数,默认就是char(1)
    as
    begin
    declare @total int;
    set @total = (select COUNT(*) from Innovation where Cast(@zitem as nvarchar(50)) = cast(@zid AS nvarchar(50));
    set @totalrecords = @total;
    select top(@pagesize) * from Innovation where Id not in (select top((@currpage - 1)*@pagesize) Id from Innovation where Cast(@zitem as nvarchar(50)) =  cast(@zid AS nvarchar(50));) and Cast(@zitem as nvarchar(50)) =  cast(@zid AS nvarchar(50));;
    end
      

  5.   

    加了(50) 了 可是 还是报出 'year'转换为 int时失败 问题是我没用让他 转换啊
      

  6.   


    ALTER procedure [dbo].[fyinnovationbyzitem](@pagesize int,@currpage int,@totalrecords int output,@zid int,@zitem nvarchar(50))
    as
    begin
    declare @total int;
    set @total = (select COUNT(*) from Innovation where Cast(@zitem as nvarchar(50)) = Cast(@zid as nvarchar(50)));
    set @totalrecords = @total;
    select top(@pagesize) * from Innovation where Id not in (select top((@currpage - 1)*@pagesize) Id from Innovation where Cast(@zitem as nvarchar(50)) = @zid) and Cast(@zitem as nvarchar(50)) = Cast(@zid as nvarchar(50));
    end我都全部转换成 成nvarchar了 
      

  7.   

    @zitem是用来存字段名称?
    declare @topsize int=2
    declare @columnname varchar(20)='yearid'select top (@topsize) *
    from [dbo].[tb]
    where @columnname=2010上面这种写法是会报错的。
    字段名不能用变量
    不然就要写成
    Exec ('你拼接的sql语句')
      

  8.   

    谢谢大神了 这是我现在的 还是不行 如果可以 直接在我这上边改一下吧 谢谢了ALTER procedure [dbo].[fyinnovationbyzitem](@pagesize int,@currpage int,@totalrecords int output,@zid int,@zitem nvarchar(50))
    as
    begin
    declare @columnname nvarchar(50) = @zitem;
    declare @total int;
    set @total = (select COUNT(*) from Innovation where @columnname = Cast(@zid as nvarchar(50)));
    set @totalrecords = @total;
    select top(@pagesize) * from Innovation where Id not in (select top((@currpage - 1)*@pagesize) Id from Innovation where @columnname = @zid) and @columnname = Cast(@zid as nvarchar(50));
    end
      

  9.   

    不是 4个 int 一个nvarchar
      

  10.   

    你试试是在id not in里面就报错还是整句才报错?
      

  11.   

    已经解决了 谢谢大家 贴出来 以防以后有人也碰到同样的问题~~ALTER procedure [dbo].[fyinnovationbyzitem](@pagesize int,@currpage int,@totalrecords int output,@zid int,@zitem nvarchar(50))
    as
    begin
    declare @columnname nvarchar(50) = @zitem;
    declare @total int;
    set @total = (select COUNT(*) from Innovation where @columnname = Cast(@zid as nvarchar(50)));
    set @totalrecords = @total;
    --select top(@pagesize) * from Innovation where Id not in 
    --(select top((@currpage - 1)*@pagesize) Id from Innovation where @columnname = Cast(@zid as nvarchar(50))) 
    --and @columnname = Cast(@zid as nvarchar(50));
    exec('select top('+@pagesize+') * from Innovation where Id not in (select top(('+@currpage + '-' + '1'+')*'+@pagesize+') Id from Innovation where '+@columnname+' = '+@zid+') and '+@columnname+' = '+@zid+'');
    end如果传进去的 参数有 字段名 就要拼sql语句了