/*
*@curentpage 当前页
*@pagesize 每页记录数
*@TableName 表名
*@key 主键(筛选排序)
*@where 查询条件  
1)空为 null  
2)有查询条件不要带where  
*@orderfield 排序字段  
@queryfield 查询字段  
*@order '0'表示 desc '1'是asc ,'2' 没有排序字段
*@pageCount 总页数  
*/  
CREATE PROCEDURE NewAndPage
@currentpage int,@pagesize int,
@TableName varchar(300),@key varchar(300),
@where nvarchar(3000),
@order varchar(10),
@pageCount int output, --总页数
@AllCount varchar(12) output, --总记录数
@orderfield varchar(100),
@queryfield Nvarchar(600)  as
begin
---------------执行的sql语句-----------------------------
declare @sql nvarchar(4000),@ordreby nvarchar(2000)
declare @tempsql1 varchar(2000),@tempsql2 varchar(2000)
---------------记录总数----------------------------------
declare @count bigint
---------------临时变量----------------------------------  
declare @temp1 bigint,@temp2 bigint  
---------------临时保存sql语句---------------------------
--declare @str varchar(4500)set @TableName=' '+@TableName+' '
set @key=' '+@key+' '
set @orderfield=' '+@orderfield+' '
if @order='0'
set @ordreby=' order by '+@orderfield+'desc'
else if @order='1'
set @ordreby=' order by '+@orderfield
else if @order='2'
set @ordreby=' 'if @where='null'
set @sql='select @count = count(1) from '+ @TableName
else
set @sql='select @count = count(1) from '+ @TableName+' where '+@where  ------------@count 付值(声明变量@count 在说明是output 内型)---------------------------
exec sp_executesql @sql,N'@count int out',@count out
------------求总页数------------------------------
if (@count%@pagesize)=0
set @pagecount=@count/@pagesize
else  
set @pagecount=@count/@pagesize+1-----------判断显示当前页是否异常------------------
if @currentpage>@pagecount  
set @currentpage=@pagecount
if @currentpage<1
set @currentpage=1
----------记录数小于页面显示记录数-----------------
if(@currentpage=1)
begin  
if @where='null'
set @where=' '
else
set @where=' where '+@where  
set @sql = 'select top'+ str(@pagesize)+@queryfield+' from '+@TableName+@where+@ordreby
end
else
begin
/* ---------------desc----------------------  
*@temp1表示前面的记录
*@temp2表示后面的记录
*假设一共77个记录,每次取10个。取67~58(第2页),去掉前面的57(1~57)个和后面的10个(77~66)
*/
if @order=0
begin  
set @temp1 = @count-@currentpage*@pagesize
if @temp1<0
set @temp1=0
set @temp2 = (@currentpage - 1)*@pagesize
if @where='null'
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' order by ' +@orderfield
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName + @ordreby
end
else
begin
set @tempsql1='select top ' + str(@temp1)+' '+@key+' from ' + @TableName+' where '+@where+' order by ' +@orderfield
set @tempsql2='select top ' + str(@temp2)+' '+@key+' from ' + @TableName+' where '+@where+@ordreby
end  
set @sql=' select top ' + str(@pagesize) + @queryfield + ' from ' + @TableName + ' where '+@key+ ' not in '  
set @sql= @sql+' ( '+ @tempsql1 +' ) and '
set @sql= @sql+@key+ ' not in ( '+@tempsql2 +' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordreby
end  
/* ----------------asc---------------------
* @temp 表示前面显示的记录总数  
* 去掉 @temp 在取出 pagesize 个即可
*/  
else  
begin
set @temp1=(@currentpage-1)*@pagesize
if @where='null'
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName + @ordreby
else
set @tempsql1='select top '+ str(@temp1)+' '+@key+' from ' + @TableName ++' where '+@where+@ordreby
set @sql=' select top ' + str(@pagesize) + @queryfield+' from ' + @TableName + ' where '+@key+ ' not in '  
set @sql=@sql+' ( '+@tempsql1+' ) '
if @where='null'
set @sql= @sql+@ordreby
else
set @sql= @sql+' and '+@where+@ordrebyend
/* -------------------------------------*/  
end  set @pageCount=ltrim(@pageCount)  set @AllCount=ltrim(@count)  exec sp_executesql @sqlend
GO
这是我的存储过程查询 数字 V查询 字母 V查询 汉字 V查询 韩语 X这是怎么回事呢 字段类型也是Nvarchar 应该可以查询 为什么查询不了呢请高手指点

解决方案 »

  1.   


    这是我的存储过程查询 数字 V查询 字母 V查询 汉字 V查询 韩语 X这是怎么回事呢 字段类型也是Nvarchar 应该可以查询 为什么查询不了呢--看不懂什么意思
      

  2.   

    是指where条件里?!还是啥东西?
      

  3.   

     string where = "1=1 and tw_classtwo='0'";
                       where += " and (tw_protagonist like N'%" + key + "%' or tw_director like N'%" + key + "%' or tw_protagonist like N'%" + key + "%' or tw_name like N'%" + key + "%')";
            PagiNation pn = new PagiNation();
            DataTable dt =pn.getPagiNationData(Convert.ToInt32(page), 4, "Film_V_Icon", "tw_id", where, "0", "tw_id", "*");
      

  4.   

    这样,你把你的sql语句直接打印出来
    然后sql里直接执行下看看效果
    这个明显跟存储过程没关系,是语言的问题
      

  5.   


    set @where=N'1=1 and tw_classtwo=''0'' and (tw_protagonist like N''%영%'' or tw_director like N''%영%'' or tw_protagonist like N''%영%'' or tw_name like N''%영%'') '我要是在@where=加N的话 可以查询 不加的话 就查询不了
      

  6.   

    N是unicode,国际编码方式
    比如繁体、外语,除了英文时最好用unicode,避免乱码
      

  7.   

    就像你写程序时编码方式有时用utf-8有时用GBK
      

  8.   

    非常感谢geniuswjt问题以解决