select (select count(*) from TableA where colA <=a.ColA order by colA),* from tableA
只要有顺序: select (select count(*) from yourtable where colA <= A.colA) colAdd, * from yourtable A
select IDENTITY(int, 1,1) ID_Num,* into #temp from 表select * from #temp
select (select sum(1) from yourtable where colA <= A.colA) colAdd, * from yourtable A
select (select count(*) from yourtable where colA <= A.colA) colAdd, * from yourtable A order by A.colA --!!!!
如果有一列可以标出记录的顺序: select (select sum(1) from 表 where 顺序列<=tem.顺序列) id,* from 表 tem 否则:declare @a table(a int identity(1,1),b varchar(100),c varchar(100)) insert @a (a,b) select a,b from ....... select * from @a
select aa=IDENTITY(int ,1,1) ,colA,colB,colC into #aa from TableA select * from #aa
cnhchang() 你的意思是不是在增加人员时这个字段不好增加,要手工输入进入? 如果是这样的,我想可以在程序中去读出主键最大的No 再加1即可。select @No=max(No) from TableA where deptID =@deptid and teamid=@teamid 如果是查询,就需要建立临时表了。
alter table [表] add 列 int indentity(1,1)
cappuccinomn(晓神) 那这样不是把表都给改掉了吗?
人家没说不让改表阿,要求不是再加上一个字段吗? 要是不小要了删了就可以了 alter table [表] drop column [列]
select (select count(*) from yourtable where colA <= A.colA) colAdd, *
from yourtable A
from yourtable A
from yourtable A
order by A.colA --!!!!
select (select sum(1) from 表 where 顺序列<=tem.顺序列) id,* from 表 tem
否则:declare @a table(a int identity(1,1),b varchar(100),c varchar(100))
insert @a (a,b) select a,b from .......
select * from @a
你们的思路是利用colA的大小来进行汇总的,这样写有个问题
如果该表中没有哪个字段的每行的值都可以区分大小的(也就是每列都有重复的值),那就不行了。
另外,新增加的一列每次都要重新从数据库计算一次速度上是不是有点慢
不过,还是没有解决问题,因为为了加速,我并不想创建临时表。
再想想有没有好的办法?
cnhchang ---能把你的问题讲清楚点吗?
我的意思是我有一个表有:主键由3个字段组成(deptid,teamid,workerid)
我想得到如:
No workerid deptid teamid
1 123456 A001 D001
2 123458 A001 D001
3 123456 A001 D001
4 123452 A001 D001
1 123450 A001 D002
2 123451 A001 D002
. .
. .
不知道这样的NO能不能得到,如何得到。
我现在在数据库中加了一个这样的字段,是手输的很麻烦。
select aa=IDENTITY(int ,1,1) ,colA,colB,colC into #aa from TableA
select * from #aa
你的意思是不是在增加人员时这个字段不好增加,要手工输入进入?
如果是这样的,我想可以在程序中去读出主键最大的No 再加1即可。select @No=max(No)
from TableA where deptID =@deptid and teamid=@teamid
如果是查询,就需要建立临时表了。
add 列 int indentity(1,1)
那这样不是把表都给改掉了吗?
要是不小要了删了就可以了
alter table [表]
drop column [列]
我觉得没必要再建立临时表,只为这一列。
我现在是手动输入,然后显示NO的。
我只是想不用输入这列,能够通过程序自动得出就好了。
即对不同的deptid ,teamid 得出workerid 在该deptid ,teamid的NO就可以了。
可以解决的。】
select (select sum(1) from 表 where 顺序列<=tem.顺序列) id,* from 表 tem
不能增加字段;
没有顺序;
只用SELECT语句:
sql server 没有办法!
首先还得感谢你的热心,这个问题是我提出来得,目的就是在不改变表结构的情况下能够增加一自动增长的列。(当然不能通过临时表)而后面 cnhchang(chang) 提出就更明确了,那就是主键不在一个列上如何添加,并且还要通过分组添加。
讨论表结构的合理性是另外的问题
讨论在考虑用程序实现也不是目的,我想如果能够用SQL语句解决问题,转移到哪个平台上都不是问题,而且同一性更好。
通过前面的ta讨论,
看来还是大力说的比较好:
如果表中有主键是一列时,可以通过select (select sum(1) from 表 where 顺序列<=tem.顺序列) id,* from 表 tem 来解决的。
否则,只能通过临时表来实现。
结贴再等一会儿,看看有没有一些别的好的建议。