CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  应用实例

求一SQL语句,为一个表添加一个临时编号?

楼主geniusqing(依帆)2005-06-04 11:04:54 在 MS-SQL Server / 应用实例 提问

create   table   test(clothid   varchar(10),number   varchar(10),bagid   varchar(10))  
  insert   into   test  
  select   '11-11','56','1'  
  union   all   select   '11-12-1','45','1'  
  union   all   select   '11-12-2','46','1'  
  union   all   select   '11-12-3','50','2'  
  union   all   select   '11-13-1','60','2'  
  union   all   select   '11-13-2','32','2'  
  union   all   select   '11-14','34','3'  
  union   all   select   '11-15-1','45','3'  
  union   all   select   '11-15-2','52','3'  
  结果如下:  
  id   number   bagid     clothid                                    
  1     56       1 11-11  
  2     45       1 11-12-1  
  3     46               1 11-12-2  
  1     50       2 11-12-3  
  2     60       2 11-13-1  
  3     32       2 11-13-2  
  1     34       3 11-14  
  2     45       3 11-15-1    
  3     52       3 11-15-2  
   
  问题点数:20、回复次数:4Top

1 楼zjcxc(邹建)回复于 2005-06-04 11:23:16 得分 5

create   table   test(clothid   varchar(10),number   varchar(10),bagid   varchar(10))  
  insert   into   test  
  select   '11-11','56','1'  
  union   all   select   '11-12-1','45','1'  
  union   all   select   '11-12-2','46','1'  
  union   all   select   '11-12-3','50','2'  
  union   all   select   '11-13-1','60','2'  
  union   all   select   '11-13-2','32','2'  
  union   all   select   '11-14','34','3'  
  union   all   select   '11-15-1','45','3'  
  union   all   select   '11-15-2','52','3'  
  go  
   
  select   id=(select   count(*)   from   test   where   clothid<=a.clothid),  
  number,bagid,clothid  
  from   test   a  
  order   by   id  
  go  
   
  drop   table   test  
   
  /*=--结果  
   
  id                     number           bagid             clothid          
  -----------   ----------   ----------   ----------    
  1                       56                   1                     11-11  
  2                       45                   1                     11-12-1  
  3                       46                   1                     11-12-2  
  4                       50                   2                     11-12-3  
  5                       60                   2                     11-13-1  
  6                       32                   2                     11-13-2  
  7                       34                   3                     11-14  
  8                       45                   3                     11-15-1  
  9                       52                   3                     11-15-2  
   
  (所影响的行数为   9   行)  
  --*/Top

2 楼xluzhong(Ralph)回复于 2005-06-04 11:25:06 得分 5

select   sid=identity(int,1,1),*   into   #t   from   test  
  select  
  id=(select   count(*)   from   #t   where   bagid=a.bagid   and   sid<=a.sid),  
  number,  
  bagid,  
  clothid  
  from   #t   a  
  Top

3 楼hsj20041004(光芒)回复于 2005-06-04 11:31:09 得分 5

To:   zjcxc(邹建)    
  你是不是看错题了!!!  
  结果如下:  
  id   number   bagid     clothid                                    
  1     56       1 11-11  
  2     45       1 11-12-1  
  3     46               1 11-12-2  
  1     50       2 11-12-3  
  2     60       2 11-13-1  
  3     32       2 11-13-2  
  1     34       3 11-14  
  2     45       3 11-15-1    
  3     52       3 11-15-2Top

4 楼xluzhong(Ralph)回复于 2005-06-04 11:55:23 得分 5

create   table   test(clothid   varchar(10),number   varchar(10),bagid   varchar(10))  
  insert   into   test  
  select   '11-11','56','1'  
  union   all   select   '11-12-1','45','1'  
  union   all   select   '11-12-2','46','1'  
  union   all   select   '11-12-3','50','2'  
  union   all   select   '11-13-1','60','2'  
  union   all   select   '11-13-2','32','2'  
  union   all   select   '11-14','34','3'  
  union   all   select   '11-15-1','45','3'  
  union   all   select   '11-15-2','52','3'  
  go  
   
  select   sid=identity(int,1,1),*   into   #t   from   test  
  select  
  id=(select   count(*)   from   #t   where   bagid=a.bagid   and   sid<=a.sid),  
  number,  
  bagid,  
  clothid  
  from   #t   a  
  drop   table   test,#tTop

相关问题

  • 如何用sql语句删除一个临时表?
  • 急问:动态执行Sql语句与临时表?
  • 求教判断临时表存在的SQL语句。。。。急
  • sql语句,临时表操作基础问题
  • sql语句 求分隔字符串转临时表
  • 我的QUERY抱错,关于用SQL语句向临时表中写数据。
  • 高分请教如何将一sql语句动态放入临时表,并修改临时表部分属性。
  • 在创建视图的sql语句中,是否可以先创建个临时表,再在主select语句中left join 这个临时表
  • 请教各位如何在sql后台用sql语句增加这个临时表的列和列的数据
  • 修改表的sql语句

关键词

  • bagid
  • clothid
  • union all select
  • varchar
  • number

得分解答快速导航

  • 帖主:geniusqing
  • zjcxc
  • xluzhong
  • hsj20041004
  • xluzhong

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo