Code
1SET QUOTED_IDENTIFIER ON
2GO
3SET ANSI_NULLS ON
4GO
5
6
7ALTER PROCEDURE GetRecords
8(@TableName varchar(15), ---表名
9@KeyName varchar(30), ---key值欄位名
10@ReturnFieldName varchar(300), ---返回欄位名集合
11@Condition Varchar(1000), ---條件集合
12@Order varchar(1000), ---排序集合
13@RecordNum int, ---返回一頁的記錄數
14@StartNO int, ---開始記錄數
15@@RealNum INT OUTPUT, ---真實一頁返回筆數
16@@TotalCount INT OUTPUT ) ---總共筆數
17AS
18
19DECLARE @PreRecCount VARCHAR( 10 ) ---上次的筆數
20DECLARE @CurRecCount VARCHAR( 10 ) ---當前的筆數
21
22
23declare @ReturnTotal int ---回傳的總筆數
24DECLARE @SQLStr NVARCHAR(500) ---回傳總筆數的SQL語句
25set @SQLStr=N'SELECT @ReturnTotal=COUNT(*) FROM ' ---因為這個參數要求回傳所以處理不一樣,前者把變量寫在SQL語句中
26set @SQLStr=@SQLStr+ @TableName +' WHERE 1=1 ' +@Condition ---直接讓變量轉化為值
27exec sp_executesql @SQLStr,N'@ReturnTotal int output',
28@ReturnTotal output
29Set @@TotalCount=@ReturnTotal
30
31----返回該頁可以返回的真實筆數
32IF @@TotalCount > ( @StartNO + 1 ) * @RecordNum
33 SET @@RealNum = @RecordNum
34ELSE
35 SET @@RealNum = @@TotalCount - @StartNO * @RecordNum
36
37SET @CurRecCount = CAST( @StartNO * @RecordNum + @@RealNum AS VARCHAR( 10 ) )
38IF @STARTNO = 0
39 EXEC( 'SELECT TOP ' + @RecordNum + @ReturnFieldName+' FROM '+@TableName+' WHERE 1=1 '+@Condition+' Order By '+@Order )
40ELSE
41 BEGIN
42 SET @PreRecCount = CAST( @StartNO * @RecordNum AS VARCHAR( 10 ) )
43 EXEC( 'SELECT TOP ' + @RecordNum +' '+ @ReturnFieldName+ ' FROM '+@TableName+' WHERE 1=1 '+@Condition+' AND '+@KeyName+' NOT IN '
44 + '(SELECT TOP ' + @PreRecCount + @KeyName+' FROM '+@TableName+' WHERE 1=1 '+@Condition+' Order By '+@Order+')' +' Order By '+@Order)
45 END
46
47print @PreRecCount
48print @CurRecCount
49
50GO
51SET QUOTED_IDENTIFIER OFF
52GO
53SET ANSI_NULLS ON
54GO
55
56
1SET QUOTED_IDENTIFIER ON
2GO
3SET ANSI_NULLS ON
4GO
5
6
7ALTER PROCEDURE GetRecords
8(@TableName varchar(15), ---表名
9@KeyName varchar(30), ---key值欄位名
10@ReturnFieldName varchar(300), ---返回欄位名集合
11@Condition Varchar(1000), ---條件集合
12@Order varchar(1000), ---排序集合
13@RecordNum int, ---返回一頁的記錄數
14@StartNO int, ---開始記錄數
15@@RealNum INT OUTPUT, ---真實一頁返回筆數
16@@TotalCount INT OUTPUT ) ---總共筆數
17AS
18
19DECLARE @PreRecCount VARCHAR( 10 ) ---上次的筆數
20DECLARE @CurRecCount VARCHAR( 10 ) ---當前的筆數
21
22
23declare @ReturnTotal int ---回傳的總筆數
24DECLARE @SQLStr NVARCHAR(500) ---回傳總筆數的SQL語句
25set @SQLStr=N'SELECT @ReturnTotal=COUNT(*) FROM ' ---因為這個參數要求回傳所以處理不一樣,前者把變量寫在SQL語句中
26set @SQLStr=@SQLStr+ @TableName +' WHERE 1=1 ' +@Condition ---直接讓變量轉化為值
27exec sp_executesql @SQLStr,N'@ReturnTotal int output',
28@ReturnTotal output
29Set @@TotalCount=@ReturnTotal
30
31----返回該頁可以返回的真實筆數
32IF @@TotalCount > ( @StartNO + 1 ) * @RecordNum
33 SET @@RealNum = @RecordNum
34ELSE
35 SET @@RealNum = @@TotalCount - @StartNO * @RecordNum
36
37SET @CurRecCount = CAST( @StartNO * @RecordNum + @@RealNum AS VARCHAR( 10 ) )
38IF @STARTNO = 0
39 EXEC( 'SELECT TOP ' + @RecordNum + @ReturnFieldName+' FROM '+@TableName+' WHERE 1=1 '+@Condition+' Order By '+@Order )
40ELSE
41 BEGIN
42 SET @PreRecCount = CAST( @StartNO * @RecordNum AS VARCHAR( 10 ) )
43 EXEC( 'SELECT TOP ' + @RecordNum +' '+ @ReturnFieldName+ ' FROM '+@TableName+' WHERE 1=1 '+@Condition+' AND '+@KeyName+' NOT IN '
44 + '(SELECT TOP ' + @PreRecCount + @KeyName+' FROM '+@TableName+' WHERE 1=1 '+@Condition+' Order By '+@Order+')' +' Order By '+@Order)
45 END
46
47print @PreRecCount
48print @CurRecCount
49
50GO
51SET QUOTED_IDENTIFIER OFF
52GO
53SET ANSI_NULLS ON
54GO
55
56