简单问题(自动增1)

Oer 2003-08-22 03:24:07
就是说想让 xh 这个字段自动增1,表里没有这个字段;
请问这个SQL怎么写了?
select xh=??,stuname from table
结果为:
 xh stuname
1 aa
2 bb
3 cc
问 ??怎么写了??
...全文
28 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
kfcn 2003-08-22
  • 打赏
  • 举报
回复
表里没这个字段,加上就可以了.

--在表中添加此字段
alter table 表名 add xh int identity(1,1)

如果你只是在查询时生成,就用临时表:
create table #tb(xh,int identity(1,1),stuname)
go
select xh=identity(int,1,1),stuname into #tb from table
select * from #tb
drop table #tb

tanys 2003-08-22
  • 打赏
  • 举报
回复
可以在ASP中用个变量来实现这个自动加1功能,先让变量初始为1,然后让它随表的记录数自动增加即可。
wwwsoftware 2003-08-22
  • 打赏
  • 举报
回复
ASP上我不是很清楚,如果是当前连接,应该可以,你测试一下。
zjcxc 2003-08-22
  • 打赏
  • 举报
回复
表里没这个字段,加上就可以了.

--在表中添加此字段
alter table 表名 add xh int identity(1,1)

如果你只是在查询时生成,就用临时表:
select xh=identity(int,1,1),stuname into #tb from table
select * from #tb
drop table #tb
Oer 2003-08-22
  • 打赏
  • 举报
回复
那如果我真正在ASP上用的时候,总不能这样子来访问这个局部临时表#temp吧!!
wwwsoftware 2003-08-22
  • 打赏
  • 举报
回复
tj_dns(愉快的登山者) 的方法是可行的,你是不是在具体使用时
没作好。简单说明一下:

select identity(int,1,1) xh, * into #temp from table
/*该语句将表table的全部数据放到局部临时表#temp中,并在临时表生成自动
加1的字段xh。如需要全局临时表,将#temp改为##temp即可*/

select * from #temp
/*该语句从上文生成的临时表中提取你所需要的数据*/
Oer 2003-08-22
  • 打赏
  • 举报
回复
都不行啊
caiyunxia 2003-08-22
  • 打赏
  • 举报
回复
SELECT (SELECT COUNT(*) FROM TABLE WHERE stuname<=A.stuname) AS XH,stuname
FROM TABLE A
愉快的登山者 2003-08-22
  • 打赏
  • 举报
回复
select identity(int,1,1) xh, stuname into #t from [table]
select * from #t

22,209

社区成员

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

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