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

有关一个存储过程的问题
SQL code
--些存储过程是通过传入的表名,字段,关键字,查询条件,排序字段来进行查询,返回记录记录总数
drop procedure mytestproceduer
go
create PROCEDURE mytestproceduer
 @serchclumname char(8000),--查询的字段列表
 @serchwords char(10),--查询的关键字
 @serchtable char(20),--查询的表名
 @topnumber int=0,--查询顶部的记录条数
 @outputnumber int=10 output--返回的记录总数
AS

declare @serchtablename char(8000);
declare @selectcount3 char(8000);
BEGIN
select @serchtablename='select top 1 ' + @serchclumname +' from '+ Rtrim(@serchtable) +' where [C_name]='''+rtrim(@serchwords)+'''' +' order BY C_id';
exec (@serchtablename);
print @serchtablename;
select @selectcount3='select ' + @serchclumname + ' from '+ Rtrim(@serchtable) +' where [C_name]='''+rtrim(@serchwords)+'''' +' order BY C_id';
exec (@selectcount3);
select @outputnumber=@@ROWCOUNT;
END
return @outputnumber;
exec mytestproceduer @serchclumname='C_id', @serchwords='wen',@serchtable='T_mytest',@topnumber=1


为什么下面这句会不行
select @serchtablename='select top 1 ' + @serchclumname +' from '+ Rtrim(@serchtable) +' where [C_name]='''+rtrim(@serchwords)+'''' +' order BY C_id';


如下直接写是正常的
select @serchtablename='select top 1 C_id from '+ Rtrim(@serchtable) +' where [C_name]='''+rtrim(@serchwords)+'''' +' order BY C_id';
这是为什么?

还有,如果我想返回记录的总数,有没有更好的方式?


------解决方案--------------------
http://blog.csdn.net/xuexiaodong2009/article/details/6336638
SQL2005分页查询
------解决方案--------------------
定义一个@rowcount,每执行一句sql就把@@rowcount加上
------解决方案--------------------
print出来就可以看见错误了