use TeachMis; go if object_id('GetStudentPaged') is not null drop procedure GetStudentPaged; go create procedure GetStudentPaged @startRow int, @maxmimumRows int, @whereExpression nvarchar(512), @sortExpression nvarchar(512) as if (object_id('tempdb..#Student') is not null) drop table #Student; go --產生沒有記錄的暫存資料表 SELECT identity(int,1,1) as RowId ,[StudentId] ,[Name] ,[Sex] ,[BirthDate] ,[Nation] ,[NativePlace] ,[Address] ,[Photo] ,[Memo] ,[StudentStatus] into #Student FROM [Student] where 1=0 declare @sql nvarchar(max) set @sql = 'insert into #Student SELECT [StudentId] ,[Name] ,[Sex] ,[BirthDate] ,[Nation] ,[NativePlace] ,[Address] ,[Photo] ,[Memo] ,[StudentStatus] FROM [Student]' if @whereExpression is not null and @whereExpression <> '' begin set @sql = @sql + ' where ' + @whereExpression end if @sortExpression is not null and @sortExpression <> '' begin set @sql = @sql + 'order by ' + @sortExpression end execute sp_executesql @sql select [StudentId] ,[Name] ,[Sex] ,[BirthDate] ,[Nation] ,[NativePlace] ,[Address] ,[Photo] ,[Memo] ,[StudentStatus] from #Student where RowId between @startRow and @startRow + @maximumRows -1 order by RowId go |