存储过程报错。麻烦帮看下到底该如何写
存储过程报错。麻烦帮看下到底该怎么写。
错误:
无法使用 SELECT INTO 语句将标识列添加到表 '#t',该表的列 'id' 已继承了标识属性。
------解决方案--------------------
你是运行的时候报错是吧?
应该是你select 。。。from 的数据源表的id有建identity属性,或者是其他属性,你不要用select into #t from 的格式,你先用create table #t 把#t这个临时表创建好,然后用 insert into select 。。。from 的格式插入数据试试看看。
另外:存储过程中尽量不要用select 。。。into #table 的格式创建临时表,如果使用临时表就事先先create table #table把它创建好,然后再insert into 进去。
USE [egov]
GO
/****** Object: StoredProcedure [dbo].[PROC_SYSTEM_Pagination] Script Date: 05/13/2014 13:23:01 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ALTER proc [dbo].[PROC_SYSTEM_Pagination]
(
@PageIndex int, --第N页
@Sql nvarchar(4000), --SQL语句
@Order varchar(50), --排序字段
@Direction varchar(10),--排序方向
@PageSize int, --每页的大小
@Count int output --总计录数
--@PageCount int output --总页数
)
AS
BEGIN
--计算边界
SELECT @PageIndex=(CASE WHEN @PageIndex<1 THEN 0 ELSE @PageIndex-1 END)
--计算总记录数
Declare @totalRecord int
Declare @countsql nvarchar(4000)
SELECT @countsql='select @totalRecord=count(*) from ( '+@Sql+' ) tb '
EXEC sp_executesql @countsql,N'@totalRecord int OUTPUT',@totalRecord=@Count OUTPUT
Declare @SQL1 nvarchar(4000)
declare @i int
set @i=CHARINDEX('select',@Sql)
set @Sql=substring(@Sql,@i,6)+' top '+cast(@Count as varchar)+' '+substring(@Sql,@i+6,len(@Sql)-6)
if @Order=''
begin
SET @Order='id'
SET @Direction='asc'
SET @SQL1='select rowid=identity(int,1,1),* into #t from (' +@Sql +' order by '+@Order+' '+@Direction+') tb '+';'+
'select * from #t where rowid>='+cast(@PageSize*@PageIndex+1 as varchar)+' and rowid<='+cast(@PageSize*@PageIndex+@PageSize as varchar)
end
else
begin
SET @SQL1='select rowid=identity(int,1,1),* into #t from (' +@Sql +' order by '+@Order+' '+@Direction+',id '+@Direction+') tb '+';'+
'select * from #t where rowid>='+cast(@PageSize*@PageIndex+1 as varchar)+' and rowid<='+cast(@PageSize*@PageIndex+@PageSize as varchar)
end
EXEC (@SQL1)
END
错误:
无法使用 SELECT INTO 语句将标识列添加到表 '#t',该表的列 'id' 已继承了标识属性。
------解决方案--------------------
你是运行的时候报错是吧?
应该是你select 。。。from 的数据源表的id有建identity属性,或者是其他属性,你不要用select into #t from 的格式,你先用create table #t 把#t这个临时表创建好,然后用 insert into select 。。。from 的格式插入数据试试看看。
另外:存储过程中尽量不要用select 。。。into #table 的格式创建临时表,如果使用临时表就事先先create table #table把它创建好,然后再insert into 进去。