日期:2014-05-18  浏览次数:20433 次

sql语句问题。让我很头痛
小弟为了用存储过程实现分页在网上搜了个代码实例,按照他(http://www.bccn.net/Article/net/aspx/jszl/200804/7096.html)上面照做的。
当我调试的时候出现如下错误

'=' 附近有语法错误。
关键字 'select' 附近有语法错误。
'=' 附近有语法错误。

小弟对sql知道的比较少,希望指点。
C#代码如下
  SqlConnection sqlConn = DB.SqlConn();
  SqlDataAdapter sqlAdapter = DB.SqlAdapter("dbo.pr_pagination", sqlConn);
  DataSet ds = DB.Ds();
  sqlAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
   
  sqlAdapter.SelectCommand.Parameters.Add("@tablelist", SqlDbType.NVarChar, 400).Value = "userlogin,commenttitle,commentcontent";
  sqlAdapter.SelectCommand.Parameters.Add("@tablename", SqlDbType.NVarChar, 100).Value = "users,usercomment";
  sqlAdapter.SelectCommand.Parameters.Add("@selectwhere", SqlDbType.NVarChar, 4000).Value = "where d=1";
  sqlAdapter.SelectCommand.Parameters.Add("@selectorderid", SqlDbType.NVarChar, 50).Value = "userid,usercommendid";
  sqlAdapter.SelectCommand.Parameters.Add("@selectorder", SqlDbType.NVarChar, 400).Value = "order by commenttime asc";
  sqlAdapter.SelectCommand.Parameters.Add("@intpageno", SqlDbType.Int).Value = pageNo;
  sqlAdapter.SelectCommand.Parameters.Add("@intpagesize", SqlDbType.Int).Value = pageSize;
  sqlAdapter.SelectCommand.Parameters.Add("@recordcount", SqlDbType.Int).Direction = ParameterDirection.Output;
  sqlAdapter.SelectCommand.Parameters.Add("rowcount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

  sqlAdapter.Fill(ds, "dbo.pr_pagination");
  this.rptComment.DataSource = ds.Tables["dbo.pr_pagination"];
  this.rptComment.DataBind();

一下是存储过程代码
use DIS
go
if exists (select 1 from sysobjects where id=OBJECT_ID('dbo.pr_pagination') and type='P')
drop procedure dbo.pr_pagination
go
create procedure dbo.pr_pagination
(
@tablelist nvarchar(400), --搜索表的字段
@tablename nvarchar(100), --搜索表的名称
@selectwhere nvarchar(4000), --搜索条件,注意:不用再写where了
@selectorderid nvarchar(50) output, --表主键字段名称
@selectorder nvarchar(400), --排序,可以使用多字段排序,但主键字段必须放在最前面,也可以不写
@intpageno int=1, --页号
@intpagesize int=1, --每页显示数
@recordcount int output --总记录数(存储过程输出参数)
)
as
declare @tmpselect nvarchar(600) 
declare @tmp nvarchar(600)

set nocount on --当为on时表示不返回计数(表示受sql语句影响的行数),当为off时表示返回计数

set @tmpselect='select @recordcount=count(*) from '+@tablename+''+@selectwhere
execute sp_executesql @tmpselect, --执行上面的sql语句
N'@recordcount int output', --执行输出数据的sql语句,output出总记录数
@recordcount output

if(@recordcount=0) --如何没有数据,则返回0
return 0

--判断页数是否正确
if(@intpageno-1)*@intpagesize>@recordcount --页号大于总页数,返回错误
return -1
set nocount off --打开计数

if @selectwhere !=''
begin
set @tmpselect='select top'+STR(@intpagesize)+''+@tablelist
+'from'+@tablename
+'where'+@selectorderid
+'not in(select top '+STR((@intpageno-1)*@intpagesize)+''+@selectorderid
+'from'+@tablename+''+@selectwhere+''+@selectorder+') and '+@selectwhere+''+@selectorder
end
else
begin
set @tmpselect='select top'+str(@intpagesize)+''+@tablelist
+'from'+@tablename
+'where'+@selectorderid
+'not in(select top '+STR((@intpageno-1)*@intpagesize)+''+@selectorderid
+'from'+@tablename+''