我也来一帖!

AcHerat 元老 2011-01-09 04:42:08

create table t1(id int identity(1,1),ar varchar(10))
insert into t1
select 'ar' union all
select 'b' union all
select 'c'
go

select identity(int,1,1) as iid,* into t2 from t1 --想将筛选出的t1表里的数据增加序号列
select * from t2

drop table t1,t2

--错误
--无法使用 SELECT INTO 语句将标识列添加到表 't2',该表的列 'id' 已继承了标识属性。

/***********************************

问题:要查询t1里的列字段不固定,也就是说是未知的,表里所有的字段也是未知的,对于t1有没有自增的标识列也是未知,
当然也就用不了内连接。
如何将通过查询条件筛选出的t1里的数据增加一个序号列!需要的是sql 2000 语句,2005 也就是row_number()解决的事!

***********************************/
...全文
85 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cutesun 2011-01-09
  • 打赏
  • 举报
回复
我下面写的是表中包含IDENTITY列的处理方法
如果不含 应该还简单一些
刚才写吐血了,也测试过了 应该可以。。

declare @cols varchar(8000),@sql varchar(8000),@identitycolname varchar(8000)
select name INTO #tmpCol
from syscolumns where id=object_id( 'dbo.Table_2') and name!=''--查询表的所有列 存入临时表备用
SELECT @identitycolname=COLUMN_NAME FROM INFORMATION_SCHEMA.columns
WHERE TABLE_NAME='Table_2' AND COLUMNPROPERTY(OBJECT_ID('Table_2'),COLUMN_NAME,'IsIdentity')=1--查询identity对应的列明
SET @cols='';
UPDATE #tmpCol SET @cols=@cols+name+',' WHERE name!=@identitycolname;--将列名存入@cols变量
DROP Table #tmpCol;--删除临时表
SET @cols=substring(@cols,1,len(@cols)-1);
SET @sql='select IDENTITYCOL+0 as '+@identitycolname+','+@cols+',identity(int,1,1) iid INTO tmpResult from dbo.Table_2';--执行静态SQL,通过IDENTITYCOL引用名称未知的Identity列
exec(@sql);
select * from tmpResult;


可以通过下面的代码判断表中是否包含IDENTITY列

Select OBJECTPROPERTY(OBJECT_ID(’表名’),’TableHasIdentity’)--如果有,则返回1,否则返回0
AcHerat 元老 2011-01-09
  • 打赏
  • 举报
回复
明天早上继续来讨论这个问题!

SQL大版主在么?能把帖子分设为100么?发帖时没注意设置分数,直接点确定了。
AcHerat 元老 2011-01-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ssp2009 的回复:]
SQL code
declear num int
set num=1
select (num=num+1) as iid,* into t2 from t1

这样行不?我瞎写的。
[/Quote]

应该是不行的,'=' 报错,嗯。。。我想如果成功了iid字段应该是一个固定值吧!应该都会是2
快溜 2011-01-09
  • 打赏
  • 举报
回复
declear num int
set num=1
select (num=num+1) as iid,* into t2 from t1

这样行不?我瞎写的。
AcHerat 元老 2011-01-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 feixianxxx 的回复:]
create table t1(id int identity(1,1),ar varchar(10))
insert into t1
select 'ar' union all
select 'b' union all
select 'c'
go

select identity(int,1,1) as iid,id+1 AS id,ar into t2 from t1 ---id……
[/Quote]

主要问题是不知道查询的字段是不是原本的标识列,或者这么说,加序号列是在一个存储过程里,不同的表的
标识列的字段名不一样,所以也不能仅是id来+1变为普通列,而且在查询时如果选取的字段并不包含标识列,
那id + 1 会报错。
feixianxxx 2011-01-09
  • 打赏
  • 举报
回复

create table t1(id int identity(1,1),ar varchar(10))
insert into t1
select 'ar' union all
select 'b' union all
select 'c'
go

select identity(int,1,1) as iid,id+1 AS id,ar into t2 from t1 ---id+1 这样自增列变成普通列
select * from t2
/*
iid id ar
---- ---- ----
1 2 ar
2 3 b
3 4 c
*/
drop table t1,t2
AcHerat 元老 2011-01-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dlut_liuq 的回复:]
SQL code
--直接
select * into t2 from t1

想用标识时候用临时表。
[/Quote]

??? 怎么用!
飘零一叶 2011-01-09
  • 打赏
  • 举报
回复
--直接
select * into t2 from t1

想用标识时候用临时表。

AcHerat 元老 2011-01-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 rucypli 的回复:]
建个临时表declare @tb table(a int identity(1,1),b varchar)
[/Quote]

因为不知道查询出来的有几个字段,所以建临时表也不行!
rucypli 2011-01-09
  • 打赏
  • 举报
回复
建个临时表declare @tb table(a int identity(1,1),b varchar)
小小小小周 2011-01-09
  • 打赏
  • 举报
回复

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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