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

唯一标识的问题

楼主zhjihui_080220()2005-08-03 12:28:33 在 MS-SQL Server / 基础类 提问

我在库中想录入一个唯一能标识的字段,但是不知道怎么确定,我到了可以加入一列id,然后根据id确定唯一标识,可是我不知道再插入一条记录时,怎么把这条的id取出来.因为往往都是插入之后才知道id是多少,我现在是想在插入时就想知道要插入的id值. 问题点数:20、回复次数:11Top

1 楼xiaonvjing(飞扬)回复于 2005-08-03 12:39:16 得分 5

你可以这样做:  
   
  建立测试环境:  
   
  create   table   a   (id   int   identity,name   varchar(20))  
  go  
  insert   a   values('chenguang')  
  go  
  select   @@identity   --得到刚刚插入的id   值  
   
  Top

2 楼hdhai9451(☆新人类☆)回复于 2005-08-03 12:44:16 得分 0

create   table   a   (id   int   identity(1,1),name   varchar(20))  
  go  
  ..........Top

3 楼weiminghanquan(未明寒泉)回复于 2005-08-03 12:45:42 得分 0

欢迎进入3764722   群讨论学习SQLTop

4 楼xiaonvjing(飞扬)回复于 2005-08-03 12:49:32 得分 0

你也可以通过创建存储过程,或是触发器都是可以做到的Top

5 楼zhjihui_080220()回复于 2005-08-03 13:01:05 得分 0

select   @@identity   from   tablename  
  可以吗,为什么总是那个值呀,不管哪个表Top

6 楼vivianfdlpw()回复于 2005-08-03 13:15:02 得分 5

select   ident_current('tablename')Top

7 楼zhjihui_080220()回复于 2005-08-03 13:40:19 得分 0

我用.net作程序.  
  string   sel="select   ident_current('tableid')   as   iCount";  
  SqlCommand   cmd=new   SqlCommand(sel,myConnection);  
  cmd.Connection.Open();  
  SqlDataReader   dr=cmd.ExecuteReader();  
  while(dr.Read())  
  {  
  bh_new=Int32.Parse(dr["iCount"].ToString());  
   
  }  
  dr.Close();  
  cmd.Connection.Close();  
  为什么说输入字符串格式不正确呢Top

8 楼chenyuandxm(一剑平江湖)回复于 2005-08-03 14:50:20 得分 0

创建触发器,从inserted表中可以取得。Top

9 楼topdogXP(心残)回复于 2005-08-03 15:46:30 得分 5

string   sel="select   ident_current('tableid')   as   iCount";  
  SqlCommand   cmd=new   SqlCommand(sel,myConnection);  
  cmd.Connection.Open();  
  int   Curr_ID=cmd.ExecuteScalar();  
  cmd.Connection.Close();Top

10 楼feitianbianfu16()回复于 2005-08-03 16:57:00 得分 0

select   top   1   id   from   tablename   order   by   descTop

11 楼feitianbianfu16()回复于 2005-08-03 16:58:46 得分 5

上面是插入了以后才知道的sql语句,如果你想插入之前知道,语句如下:  
  select   top   1   id+1   from   tablename   order   by   descTop

相关问题

  • 怎么取得CPU的唯一标识?
  • 网卡ID是否唯一标识
  • 如何获得计算机的一些唯一标识符
  • 有没有函数可以取得“全球唯一标识号”??
  • 如何通过唯一标识访问表中的指定行
  • 急呀!如何获得CPU的唯一标识呀!
  • 如何对表设置唯一标识列?
  • 如何判断cpu是否具有唯一标识!谢谢!
  • 怎么取得PC机上的一个硬件唯一标识???
  • 请教:如何生成唯一标识那?

关键词

  • 插入
  • 标识
  • ident
  • cmd
  • sel
  • icount
  • sqlcommand
  • dr
  • 知道
  • current

得分解答快速导航

  • 帖主:zhjihui_080220
  • xiaonvjing
  • vivianfdlpw
  • topdogXP
  • feitianbianfu16

相关链接

  • SQL Server类图书

广告也精彩

反馈

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