日期:2014-05-17  浏览次数:20807 次

请教ASP与存储过程问题
正常T-SQL语句select   *   from   table   order   by   code   asc(数据在万条以上   SQLSERVER2000)
问题1:如何使用存储过程输出查找结果  
问题2:如何实现分页功能
问题3:   存储过程真的要比普通的SQL语句站用资源小吗   还是已经把压力都转给数据库了   谢谢各位!

------解决方案--------------------
1.如果只是这样一句sql,写不写SP都一样。问题是不要一次把那么多数据取出来。
3.确实SP在数据库端完成。
分页参考
CREATE PROCEDURE sp_paging
(
@strTblName VARCHAR(100), --表名或视图表
@strFields VARCHAR(4000) = '* ', --欲选择字段列表
@strOrdField VARCHAR(100), --排序字段
@strKeyField VARCHAR(100), --主键
@intPageNo INT = 0, --页号,从0开始
@intPageSize INT = 10, --页尺寸
@strWhere VARCHAR(4000) = ' ', --条件
@bitOrdType BIT = 1 --排序,1=降序,0=升序
)
AS
SET NOCOUNT ON
DECLARE @strSQL VARCHAR(6000)

IF @intPageNo = 0
BEGIN
SET @strSQL = 'SELECT TOP ' + CAST(@intPageSize AS VARCHAR) + ' ' + @strFields + ' FROM ' + @strTblName
IF @strWhere != ' '
SET @strSQL = @strSQL + ' WHERE ' + @strWhere
IF @bitOrdType = 1
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' DESC '
ELSE
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' ASC '
END
ELSE
BEGIN
SET @strSQL = 'DECLARE @intCurPageNo int; '
SET @strSQL = @strSQL + 'DECLARE @intNextPageNo int; '
SET @strSQL = @strSQL + 'SET @intCurPageNo = ' + CAST(@intPageNo AS VARCHAR) + '* ' + CAST(@intPageSize AS VARCHAR) + '; '
SET @strSQL = @strSQL + 'SET @intNextPageNo = ' + CAST(@intPageNo+1 AS VARCHAR) + '* ' + CAST(@intPageSize AS VARCHAR) + '; '
SET @strSQL = @strSQL + 'DECLARE @strSQL VARCHAR(6000); '
IF @bitOrdType = 1
BEGIN
SET @strSQL = @strSQL + 'SET @strSQL = ' 'SELECT ' + @strFields + ' FROM (SELECT TOP ' ' + CAST(@intNextPageNo AS VARCHAR) + ' ' * FROM ' + @strTblName
IF @strWhere != ' '
SET @strSQL = @strSQL + ' WHERE ' + @strWhere
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' DESC) AS atmp WHERE ' + @strKeyField
+ ' NOT IN (SELECT TOP ' ' + CAST(@intCurPageNo AS VARCHAR) + ' ' ' + @strKeyField + ' FROM ' + @strTblName
IF @strWhere != ' '
SET @strSQL = @strSQL + ' WHERE ' + @strWhere
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' DESC) ORDER BY ' + @strOrdField + ' DESC ' '; '
END
ELSE
BEGIN
SET @strSQL = @strSQL + 'SET @strSQL = ' 'SELECT ' + @strFields + ' FROM (SELECT TOP ' ' + CAST(@intNextPageNo AS VARCHAR) + ' ' * FROM ' + @strTblName
IF @strWhere != ' '
SET @strSQL = @strSQL + ' WHERE ' + @strWhere
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' ASC ) AS atmp WHERE ' + @strKeyField
+ ' NOT IN (SELECT TOP ' ' + CAST(@intCurPageNo AS VARCHAR) + ' ' ' + @strKeyField + ' FROM ' + @strTblName
IF @strWhere != ' '
SET @strSQL = @strSQL + ' WHERE ' + @strWhere
SET @strSQL = @strSQL + ' ORDER BY ' + @strOrdField + ' ASC) ORDER BY ' + @strOrdField + ' ASC ' '; '
END
SET @strSQL = @strSQL + 'EXECUTE(@strSQL); '
END
print @strSQL
EXECUTE(@strSQL)

SET @strSQL = 'SELECT COUNT(*) FROM ' + @strTblName
IF @strWhere !