存储过程报错。麻烦帮看下到底该怎么写。

苦逼de小Coder 2014-05-13 01:32:06

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' 已继承了标识属性。
...全文
120 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
引用 13 楼 wj38775428 的回复:
哎。结了吧。现在只能找老板商量了
呵呵,不商量的话,你那个整起来我觉得相当烦人
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
哎。结了吧。现在只能找老板商量了
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
引用 10 楼 wj38775428 的回复:
[quote=引用 8 楼 hwhmh2010 的回复:] [quote=引用 6 楼 wj38775428 的回复:] [quote=引用 5 楼 hwhmh2010 的回复:] [quote=引用 3 楼 wj38775428 的回复:] [quote=引用 2 楼 hwhmh2010 的回复:] select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?[/quote] 目前问题我已经给你测试出来了,定位为identity属性冲突,下面的测试你可以试试:
create table testtt (
	id int identity(1,1) not null,
	name varchar(10)
)

insert into testtt values('abc')

select rowid=identity(int,1,1),* into #a from testtt

select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote] 恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote] 不止一个表---这个确实有点让人挠头。。。 那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。[/quote] 我也知道 把rowid去掉就万事大吉了,但是老板不让去掉[/quote] 搞不懂,你的数据源表里面已经有了identity属性的列了,为什么还非要你加个rowid=identity(int,1,1)呢。。。这不是折磨人吗。。。 实在不行,那就还是只能用方法一了,去掉“*”,把需要查询的表的每一列(除去identity列)都列出来,放在rowid=identity(int,1,1) 后面,这个需要拼接sql语句,很麻烦。。。 给你一个找出某个表的所有属性列的语句:

--你可以在where条件后面加上条件T.name=@table ,进而列出所有表。
SELECT 
        T.[name] AS [表名] ,  
        AC.[name] AS [列名] ,  
        TY.[name] AS [系统数据类型],
		AC.[is_identity]
FROM    sys.[tables] AS T  
        INNER JOIN sys.[all_columns] AC ON T.[object_id] = AC.[object_id]  
        INNER JOIN sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]  
                                     AND AC.[user_type_id] = TY.[user_type_id]  
WHERE   T.[is_ms_shipped] = 0  
ORDER BY T.[name] ,  
        AC.[column_id]
这个只是一个思路,具体要写的话,估计还比较复杂,考虑的东西应该还很多。。。 说服你老板把那个rowid=identity去掉呗。。。
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
引用 9 楼 wj38775428 的回复:
[quote=引用 7 楼 hwhmh2010 的回复:] 给你几个方法吧: 第一:select rowid=identity(int,1,1),* into #t from ----把这里面的 “*”改成所需要的列,把你需要的列都一一列出来,注意去掉带有identity的列。。。 第二:select rowid=identity(int,1,1),* into #t from 把这里面的rowid=identity(int,1,1)这个列去掉。。。 第三:在你存储过程第一个begin下面把你的临时表#t用create table语句建好,后面用insert into。。。----不过如果你需要查询的数据源表的列不同的话,这个方法就不好整了。。。 就第一个和第二个两个方法你选一个试试
第一:要查的表太多了。我没办法一一判断表名,然后列出字段 第二:rowid是用于标识的序号,不让去掉[/quote] 那你能判断所有要查询的表是否有相同的列? 如果可以判断有相同的列那就这样试试: 把rowid=identity(int,1,1) 改成 row_number() over (order by colname)然后into #t , 这个colname是每个表都包含的一个列名---但是这样做的话,你的程序就会相对很死板了。。。。
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
引用 8 楼 hwhmh2010 的回复:
[quote=引用 6 楼 wj38775428 的回复:] [quote=引用 5 楼 hwhmh2010 的回复:] [quote=引用 3 楼 wj38775428 的回复:] [quote=引用 2 楼 hwhmh2010 的回复:] select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?[/quote] 目前问题我已经给你测试出来了,定位为identity属性冲突,下面的测试你可以试试:
create table testtt (
	id int identity(1,1) not null,
	name varchar(10)
)

insert into testtt values('abc')

select rowid=identity(int,1,1),* into #a from testtt

select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote] 恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote] 不止一个表---这个确实有点让人挠头。。。 那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。[/quote] 我也知道 把rowid去掉就万事大吉了,但是老板不让去掉
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
引用 7 楼 hwhmh2010 的回复:
给你几个方法吧: 第一:select rowid=identity(int,1,1),* into #t from ----把这里面的 “*”改成所需要的列,把你需要的列都一一列出来,注意去掉带有identity的列。。。 第二:select rowid=identity(int,1,1),* into #t from 把这里面的rowid=identity(int,1,1)这个列去掉。。。 第三:在你存储过程第一个begin下面把你的临时表#t用create table语句建好,后面用insert into。。。----不过如果你需要查询的数据源表的列不同的话,这个方法就不好整了。。。 就第一个和第二个两个方法你选一个试试
第一:要查的表太多了。我没办法一一判断表名,然后列出字段 第二:rowid是用于标识的序号,不让去掉
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
引用 6 楼 wj38775428 的回复:
[quote=引用 5 楼 hwhmh2010 的回复:] [quote=引用 3 楼 wj38775428 的回复:] [quote=引用 2 楼 hwhmh2010 的回复:] select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?[/quote] 目前问题我已经给你测试出来了,定位为identity属性冲突,下面的测试你可以试试:
create table testtt (
	id int identity(1,1) not null,
	name varchar(10)
)

insert into testtt values('abc')

select rowid=identity(int,1,1),* into #a from testtt

select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote] 恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了[/quote] 不止一个表---这个确实有点让人挠头。。。 那就用刚给你的方法二,把rowid=identity(int,1,1)去掉试试吧。。。
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
给你几个方法吧: 第一:select rowid=identity(int,1,1),* into #t from ----把这里面的 “*”改成所需要的列,把你需要的列都一一列出来,注意去掉带有identity的列。。。 第二:select rowid=identity(int,1,1),* into #t from 把这里面的rowid=identity(int,1,1)这个列去掉。。。 第三:在你存储过程第一个begin下面把你的临时表#t用create table语句建好,后面用insert into。。。----不过如果你需要查询的数据源表的列不同的话,这个方法就不好整了。。。 就第一个和第二个两个方法你选一个试试
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
引用 5 楼 hwhmh2010 的回复:
[quote=引用 3 楼 wj38775428 的回复:] [quote=引用 2 楼 hwhmh2010 的回复:] select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?[/quote] 目前问题我已经给你测试出来了,定位为identity属性冲突,下面的测试你可以试试:
create table testtt (
	id int identity(1,1) not null,
	name varchar(10)
)

insert into testtt values('abc')

select rowid=identity(int,1,1),* into #a from testtt

select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本[/quote] 恩,具体问题确实是这个,如果只查一个表我知道怎么改,只要给主键*1就可以了,但是现在问题因为不是只查一个表,只能用*来查找全部,所有没法确定主键,这个就让我挠头了
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
引用 3 楼 wj38775428 的回复:
[quote=引用 2 楼 hwhmh2010 的回复:] select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?[/quote] 目前问题我已经给你测试出来了,定位为identity属性冲突,下面的测试你可以试试:
create table testtt (
	id int identity(1,1) not null,
	name varchar(10)
)

insert into testtt values('abc')

select rowid=identity(int,1,1),* into #a from testtt

select * into #a from testtt
。。。具体怎么改,稍等一会,我看看你的脚本
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
引用 2 楼 hwhmh2010 的回复:
select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
正常创建临时表应该这么写对吧: create table #临时表名(字段1 约束条件,字段2 约束条件) 问题是我不确定要查的是哪个表
苦逼de小Coder 2014-05-13
  • 打赏
  • 举报
回复
引用 2 楼 hwhmh2010 的回复:
select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
这个存储过程不是确定只查一个表。 应该怎么写创建语句呢?
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
select rowid=identity(int,1,1),* into #t from ----看看这个,应该你的数据源表里头已经有了identity属性的列,所以你再生成一个rowid也是identity的列,同时插入#t ,应该是这里问题,你先create table #t ,然后试试。。。
山寨DBA 2014-05-13
  • 打赏
  • 举报
回复
你是运行的时候报错是吧? 应该是你select 。。。from 的数据源表的id有建identity属性,或者是其他属性,你不要用select into #t from 的格式,你先用create table #t 把#t这个临时表创建好,然后用 insert into select 。。。from 的格式插入数据试试看看。 另外:存储过程中尽量不要用select 。。。into #table 的格式创建临时表,如果使用临时表就事先先create table #table把它创建好,然后再insert into 进去。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧