续:【关于Select *,identity into table1 from table2 的问题--目前非常迷惑,请高手作答】

xtabpole 2004-01-16 04:32:46
问题简化成:

兄弟们,那就换一张简单的表吧:

a1, a2, a3, date
d, 11, 22, 2003-01-01 00:00:00.000
b, 11, 22, 2003-01-10 00:00:00.000
c, 11, 22, 2003-01-05 00:00:00.000
a, 11, 22, 2003-01-15 00:00:00.000


问题变成,如果我需要生成下面的表,该如何写(按a1来编号,用identity function)
a1, a2, a3, date ,ID_num
a, 11, 22, 2003-01-15 00:00:00.000, 1
b, 11, 22, 2003-01-10 00:00:00.000, 2
c, 11, 22, 2003-01-05 00:00:00.000, 3
d, 11, 22, 2003-01-01 00:00:00.000, 4

我需要下面的另外一张表,又如何写(按date来编号,用identity function)

a1, a2, a3, date ,ID_num
d, 11, 22, 2003-01-01 00:00:00.000,1
c, 11, 22, 2003-01-05 00:00:00.000,2
b, 11, 22, 2003-01-10 00:00:00.000,3
a, 11, 22, 2003-01-15 00:00:00.000,4

这样问题变得很清楚了吧,至于其他的Where条件就是附加条件了,这个解决了,就好办了。

原来的稍微复杂一些的问题所在:http://expert.csdn.net/Expert/topic/2669/2669527.xml?temp=.7047693

...全文
84 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
gis_2000 2004-09-28
  • 打赏
  • 举报
回复
不是这么简单。
我的也是sql 7,但是结果就是你想要的。
我相信sql没有这么差,当它是sql 7的时候,犯了这么大的错误----如果那是错误的话。

如果你的sql 7没有问题,如果你没有搞错的话,那答案应该是:
结果是随机的。
xtabpole 2004-01-16
  • 打赏
  • 举报
回复

我知道了,是因为我的Sql Server是7.0的缘故,如果是Sql Server 2000就完全正确了。

谢谢大家!

xtabpole 2004-01-16
  • 打赏
  • 举报
回复
是不是我的Sql Server坏了阿,还是别的一些什么原因,郁闷
xtabpole 2004-01-16
  • 打赏
  • 举报
回复
在我的机子上运行结果:

select *,id_num=identity(int,1,1) into #t1 from @表 order by date

select * from #t1

结果:
a1 a2 a3 date id_num
d 11 22 2003-01-01 00:00:00.000 1
c 11 22 2003-01-05 00:00:00.000 3
b 11 22 2003-01-10 00:00:00.000 2
a 11 22 2003-01-15 00:00:00.000 4

select *,id_num=identity(int,1,1) into #t2 from @表 order by a1

select * from #t2

结果:


a1 a2 a3 date id_num
a 11 22 2003-01-15 00:00:00.000 4
b 11 22 2003-01-10 00:00:00.000 2
c 11 22 2003-01-05 00:00:00.000 3
d 11 22 2003-01-01 00:00:00.000 1

并不和zjcxc(邹建) 的结果一样。

zjcxc 元老 2004-01-16
  • 打赏
  • 举报
回复
我在未打补丁及打了补丁的2000中,执行上面语句的测试结果是一样的.
gmlxf 2004-01-16
  • 打赏
  • 举报
回复
楼上的结果都出来了,现在不迷惑了吧。
xtabpole 2004-01-16
  • 打赏
  • 举报
回复
靠,是不是真的跟Service Pack3有关系阿,

各位兄弟,帮我查查,这个Identity Function是不是在Service Pack3里面有改进阿?

在我的
zjcxc 元老 2004-01-16
  • 打赏
  • 举报
回复
--上面帖错测试结果
/*--测试结果
a1 a2 a3 date id_num
---- ----------- ----------- ------------------------ -------
d 11 22 2003-01-01 00:00:00.000 1
c 11 22 2003-01-05 00:00:00.000 2
b 11 22 2003-01-10 00:00:00.000 3
a 11 22 2003-01-15 00:00:00.000 4

(所影响的行数为 4 行)
--*/
zjcxc 元老 2004-01-16
  • 打赏
  • 举报
回复
--按date也没问题啊

declare @表 table(a1 char(1),a2 int,a3 int,date datetime)
insert into @表
select 'd',11,22,'2003-01-01 00:00:00.000'
union all select 'b',11,22,'2003-01-10 00:00:00.000'
union all select 'c',11,22,'2003-01-05 00:00:00.000'
union all select 'a',11,22,'2003-01-15 00:00:00.000'

--按date
select *,id_num=identity(int,1,1) into #t1 from @表 order by date

select * from #t1
go

drop table #t1

/*--测试结果
a1 a2 a3 date id_num
---- ----------- ----------- ------------------------- -----------
a 11 22 2003-01-15 00:00:00.000 1
b 11 22 2003-01-10 00:00:00.000 2
c 11 22 2003-01-05 00:00:00.000 3
d 11 22 2003-01-01 00:00:00.000 4

(所影响的行数为 4 行)

a1 a2 a3 date id_num
---- ----------- ----------- ------------------------- --------
d 11 22 2003-01-01 00:00:00.000 1
c 11 22 2003-01-05 00:00:00.000 2
b 11 22 2003-01-10 00:00:00.000 3
a 11 22 2003-01-15 00:00:00.000 4

(所影响的行数为 4 行)
--*/
zjcxc 元老 2004-01-16
  • 打赏
  • 举报
回复
--有什么问题吗?
declare @表 table(a1 char(1),a2 int,a3 int,date datetime)
insert into @表
select 'd',11,22,'2003-01-01 00:00:00.000'
union all select 'b',11,22,'2003-01-10 00:00:00.000'
union all select 'c',11,22,'2003-01-05 00:00:00.000'
union all select 'a',11,22,'2003-01-15 00:00:00.000'

--按a1升序
select *,id_num=identity(int,1,1) into #t1 from @表 order by a1

--按a1降序
select *,id_num=identity(int,1,1) into #t2 from @表 order by a1 desc

select * from #t1
select * from #t2
go

drop table #t1,#t2

/*--测试结果
a1 a2 a3 date id_num
---- ----------- ----------- ------------------------- -----------
a 11 22 2003-01-15 00:00:00.000 1
b 11 22 2003-01-10 00:00:00.000 2
c 11 22 2003-01-05 00:00:00.000 3
d 11 22 2003-01-01 00:00:00.000 4

(所影响的行数为 4 行)

a1 a2 a3 date id_num
---- ----------- ----------- ------------------------- --------
d 11 22 2003-01-01 00:00:00.000 1
c 11 22 2003-01-05 00:00:00.000 2
b 11 22 2003-01-10 00:00:00.000 3
a 11 22 2003-01-15 00:00:00.000 4

(所影响的行数为 4 行)
--*/
leeboyan 2004-01-16
  • 打赏
  • 举报
回复
1.
select a1, a2, a3, date,identity(1,1) as id_num into temptable from yourtable order by a1
select * from temptable
drop table temptable
2.
select a1, a2, a3, date,identity(1,1) as id_num into temptable from yourtable order by date
select * from temptable
drop table temptable
xtabpole 2004-01-16
  • 打赏
  • 举报
回复
目的是想通过identity 生成的ID列来唯一标识某列,同时ID号的顺序是严格按照 field1, field2的顺序生成。

请问:identity生成的时侯是依据怎样的顺序给表中的记录加标签的?若有是什么样的规则,还是没有规则可循,每次的生成结果都不一样?若想使它按照人为指定的规则加标签,该如何写语句?

solidpanther 2004-01-16
  • 打赏
  • 举报
回复
看一下这个例子:
declare @MyTable table(a int,b varchar(100))
insert @mytable values (1,'a')
insert @mytable values (2,'b')
insert @mytable values (3,'c')
insert @mytable values (4,'d')
insert @mytable values (1,'e')
insert @mytable values (3,'f')
insert @mytable values (2,'g')
------------------------------------
select *,identity(int,1,1) as id into #tmptable from @mytable
select * from #tmptable
drop table #tmptable
------------------------------------
xtabpole 2004-01-16
  • 打赏
  • 举报
回复
注意,上面的表中如果我用如下Sql语句将生成以下的结果,达不到我的目的(以date为关键字给纪录编号):

select *,identity(int,1,1) as id_num into bb2 from bb order by date

结果

a1,a2,a3,date,id_num
d,11,22,2003-01-01 00:00:00.000,1
c,11,22,2003-01-05 00:00:00.000,3
b,11,22,2003-01-10 00:00:00.000,2
a,11,22,2003-01-15 00:00:00.000,4


而我想要的结果却是:
a1,a2,a3,date,id_num
d,11,22,2003-01-01 00:00:00.000,1
c,11,22,2003-01-05 00:00:00.000,2
b,11,22,2003-01-10 00:00:00.000,3
a,11,22,2003-01-15 00:00:00.000,4

够清楚了吧?这回

34,590

社区成员

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

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