mssql 存储过程查询问题

G.spring 2011-09-13 03:13:29
/*
*@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+@ordreby

end
/* -------------------------------------*/
end

set @pageCount=ltrim(@pageCount)

set @AllCount=ltrim(@count)

exec sp_executesql @sql

end
GO


查询数字或字母的话可以查询

查询汉字就不行

请高手指点
...全文
111 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
G.spring 2011-09-13
  • 打赏
  • 举报
回复
打错了 我在控制面板里设置好系统语言了 可还是不能查询
G.spring 2011-09-13
  • 打赏
  • 举报
回复
我已经设置好系统时间了 可还是不能查询
--小F-- 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 leeguangchun 的回复:]
现在中文能查询了

可是日语不能查询这又是怎么回事呢?
[/Quote]

日语查询是需要设置系统的语言的

-晴天 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 leeguangchun 的回复:]
引用 2 楼 leeguangchun 的回复:
我的存储过程是用来 分页的


@where nvarchar(3000),
@queryfield Nvarchar(600)

这俩个是查询条件
别的字段类型不能全改成Nvarchar
[/Quote]

不单单这俩,定义的变量类型也得改.
G.spring 2011-09-13
  • 打赏
  • 举报
回复
现在中文能查询了

可是日语不能查询这又是怎么回事呢?
G.spring 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 leeguangchun 的回复:]
我的存储过程是用来 分页的
[/Quote]

@where nvarchar(3000),
@queryfield Nvarchar(600)

这俩个是查询条件
别的字段类型不能全改成Nvarchar
G.spring 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 qianjin036a 的回复:]
把 varchar 都改为 nvarchar 试试.
[/Quote]

改了 不好使
-晴天 2011-09-13
  • 打赏
  • 举报
回复
把 varchar 都改为 nvarchar 试试.
G.spring 2011-09-13
  • 打赏
  • 举报
回复
我的存储过程是用来 分页的
--小F-- 2011-09-13
  • 打赏
  • 举报
回复
这么长?首先说明下你的存储过程是做什么的啊

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧