日期:2014-05-18  浏览次数:20459 次

在查询语名在的时候,想实现自动编号
Select   Name,Product   From   TableName
我想在查询的时候添加一个自动编号
怎样实现

不想在创建一个临时表

------解决方案--------------------
2005:
create table ta(id int,name int)
insert ta select 1,2 union all select 1,3


select *,row=ROW_NUMBER () over (order by id)
from ta
------解决方案--------------------
如果Name不存在重复:

select (select count(*) from TableName where Name <=t.Name) as RowID,Name,Product from TableName t
------解决方案--------------------
以下是SQL Server 2005支持的语法:
select *,row=ROW_NUMBER () over (order by id) from ta

------解决方案--------------------
Select right( '000 '+rtrim((select count(*) from tablename t where t.name <tablename.name)),3),Name,Product From TableName
------解决方案--------------------
2000只有用临时表/新增显示列实现
create table ta(id int,name int)
insert ta select 1,2
union all select 1,3

select *,自编号=1 into # from ta
declare @i int
set @i=0
update # set 自编号=@i,@i=@i+1
查询:
select * from #

------解决方案--------------------
select id=(select count(*) from tb where id <=a.id),Name,Product from tb a

------解决方案--------------------
mark
------解决方案--------------------
学习
------解决方案--------------------
如果有主键就行
以下语句完成的功能,为一个有主键的表显示时增加一个序号列,必须有主键才行。
select (select count(*) from gzda where 编号 <=A.编号) as RowID,编号,姓名 from gzda A