CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

SQL添加到数据库后,如何返回该行自动生成的字节信息

楼主hhb518(白丁)2004-05-01 22:11:47 在 .NET技术 / ASP.NET 提问

SQL添加到数据库后,如何返回该行自动生成的字节信息  
  例如:添加的内容是姓名hhb,性别男到数据库中后该行中编号自动生成是23  
  那么如何在添加后返回编号23呢?(因为添加前不知道自动生成编号23啊) 问题点数:100、回复次数:7Top

1 楼bitsbird(一瓢 在路上...)回复于 2004-05-01 22:23:00 得分 50

根据你添加的条件再select一次,比如说select   top   1   Usernumber   from   table1   order   by    
  Usernumber   descTop

2 楼CMIC(大象)回复于 2004-05-01 22:26:28 得分 50

经典问题,用IDENT_CURRENT,在你插入记录后使用下面的语句就可以得到id号  
  select   IDENT_CURRENT('你的表名')Top

3 楼citylamp(Johnson)回复于 2004-05-01 22:58:07 得分 0

我以前是重查数据库的Top

4 楼wangsaokui(无间道III(终极无间)C#MVP)回复于 2004-05-01 23:10:23 得分 0

@@IDENTITY  
  Returns   the   last-inserted   identity   value.  
   
  Syntax  
  @@IDENTITY  
   
  Return   Types  
  numeric  
   
  Examples  
  This   example   inserts   a   row   into   a   table   with   an   identity   column   and   uses   @@IDENTITY   to   display   the   identity   value   used   in   the   new   row.  
   
  INSERT   INTO   jobs   (job_desc,min_lvl,max_lvl)  
  VALUES   ('Accountant',12,125)  
   
  SELECT   @@IDENTITY   AS   'Identity'  
   
   
  混点分了  
   
  大象的说明如下  
  IDENT_CURRENT  
  Returns   the   last   identity   value   generated   for   a   specified   table   in   any   session   and   any   scope.    
   
  Syntax  
  IDENT_CURRENT('table_name')  
   
  Arguments  
  table_name  
   
  Is   the   name   of   the   table   whose   identity   value   will   be   returned.   table_name   is   varchar,   with   no   default.  
   
  Return   Types  
  sql_variant  
   
  Remarks  
  IDENT_CURRENT   is   similar   to   the   Microsoft®   SQL   Server™   2000   identity   functions   SCOPE_IDENTITY   and   @@IDENTITY.   All   three   functions   return   last-generated   identity   values.   However,   the   scope   and   session   on   which   'last'   is   defined   in   each   of   these   functions   differ.    
   
  IDENT_CURRENT   returns   the   last   identity   value   generated   for   a   specific   table   in   any   session   and   any   scope.  
   
  @@IDENTITY   returns   the   last   identity   value   generated   for   any   table   in   the   current   session,   across   all   scopes.  
   
  SCOPE_IDENTITY   returns   the   last   identity   value   generated   for   any   table   in   the   current   session   and   the   current   scope.    
   
  Examples  
  This   example   illustrates   the   different   identity   values   returned   by   IDENT_CURRENT,   @@IDENTITY,   and   SCOPE_IDENTITY.  
   
  USE   pubs  
  DROP   TABLE   t6  
  DROP   TABLE   t7  
  GO  
  CREATE   TABLE   t6(id   int   IDENTITY)  
  CREATE   TABLE   t7(id   int   IDENTITY(100,1))  
  GO  
  CREATE   TRIGGER   t6ins   ON   t6   FOR   INSERT    
  AS  
  BEGIN  
        INSERT   t7   DEFAULT   VALUES  
  END  
  GO  
  --end   of   trigger   definition  
   
  SELECT       *   FROM   t6  
  --id   is   empty.  
   
  SELECT       *   FROM   t7  
  --id   is   empty.  
   
  --Do   the   following   in   Session   1  
  INSERT   t6   DEFAULT   VALUES  
  SELECT   @@IDENTITY              
  /*Returns   the   value   100,   which   was   inserted   by   the   trigger.*/  
   
  SELECT   SCOPE_IDENTITY()        
  /*   Returns   the   value   1,   which   was   inserted   by   the    
  INSERT   stmt   2   statements   before   this   query.*/  
   
  SELECT   IDENT_CURRENT('t7')  
  /*   Returns   value   inserted   into   t7,   i.e.   in   the   trigger.*/  
   
  SELECT   IDENT_CURRENT('t6')  
  /*   Returns   value   inserted   into   t6,   which   was   the   INSERT   statement   4   stmts   before   this   query.*/  
   
  --   Do   the   following   in   Session   2  
  SELECT   @@IDENTITY  
  /*   Returns   NULL   since   there   has   been   no   INSERT   action    
  so   far   in   this   session.*/  
   
  SELECT   SCOPE_IDENTITY()  
  /*   Returns   NULL   since   there   has   been   no   INSERT   action    
  so   far   in   this   scope   in   this   session.*/  
   
  SELECT   IDENT_CURRENT('t7')  
  /*   Returns   the   last   value   inserted   into   t7.*/Top

5 楼bitsbird(一瓢 在路上...)回复于 2004-05-01 23:13:52 得分 0

无间道兄,有时间打电话陪陪嫂子啊,今天过节呐:)Top

6 楼CMIC(大象)回复于 2004-05-01 23:25:07 得分 0

同意楼上的Top

7 楼bitsbird(一瓢 在路上...)回复于 2004-05-01 23:26:06 得分 0

谢谢大象兄同意  
  ^_^Top

相关问题

  • 如何把SQL数据库生成脚本
  • 生成数据库的sql语句为什么总报错呢?
  • 如何使用sql语句生成 数据库
  • 已经有sql文件,如何在mysql中生成数据库?
  • 已经有sql文件,如何在mysql中生成数据库?
  • 数据库自动生成sql的问题
  • oracle数据库如何生成sql语句?
  • SQL语言可以查询SQL数据库中的字节提示吗?
  • SQL 数据库
  • sql数据库

关键词

  • 数据库
  • 添加
  • 生成
  • identity
  • 返回
  • 到数据库
  • current
  • insert
  • table
  • select

得分解答快速导航

  • 帖主:hhb518
  • bitsbird
  • CMIC

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

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