如何解决分页存储过程错误:提示当 IDENTITY_INSERT 设置为 OFF 时,不能向表#t1中的标识列插入显式值
CREATE PROCEDURE getProductPage@iPageCount int OUTPUT, --总页数
@iPage int, --当前页号
@iPageSize int --每页记录数
as
set nocount on
begin
--创建临时表
create table #t (viewID int IDENTITY, --自增字段
CategoryID varchar(255),
Productname varchar(200),
SerialNum varchar(200),
KeyWord varchar(255),
price varchar(50),
BrandID int,
Unit varchar(50),
Pic varchar(255),
Watermark int,
IndexShow int,
Special int
)
--向临时表中写入数据
--set IDENTITY_INSERT #t ON
insert into #t (viewID,CategoryID,Productname ,SerialNum,KeyWord,price,BrandID,Unit,Pic,Watermark,IndexShow,Special) select ID,CategoryID,Productname ,SerialNum,KeyWord,price,BrandID,Unit,Pic,Watermark,IndexShow,Special from [Product]
--取得记录总数
declare @iRecordCount int
set @iRecordCount = @@rowcount
--确定总页数
IF @iRecordCount%@iPageSize=0
SET @iPageCount=CEILING(@iRecordCount/@iPageSize)
ELSE
SET @iPageCount=CEILING(@iRecordCount/@iPageSize)+1
--若请求的页号大于总页数,则显示最后一页
IF @iPage > @iPageCount
SELECT @iPage = @iPageCount
--确定当前页的始末记录
DECLARE @iStart int --start record
DECLARE @iEnd int --end record
SELECT @iStart = (@iPage - 1) * @iPageSize
SELECT @iEnd = @iStart + @iPageSize + 1
--取当前页记录
select * from #t where viewID>@iStart and viewID<@iEnd
--删除临时表
DROP TABLE #t
return @iRecordCount
end
GO