12
返回列表 发新帖
楼主: landlubber

获取唯一编号的存储过程如何写?

[复制链接]
论坛徽章:
28
授权会员
日期:2005-10-30 17:05:332011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:362012新春纪念徽章
日期:2012-02-13 15:11:36马上有车
日期:2014-02-19 11:55:14
11#
发表于 2004-6-25 09:22 | 只看该作者
/***********得到最小缺号函数***********
唯一编号要求:需要是int类型
--------by /gmlxf/csdn/2003.11.20/-----******/

if exists(select 1 from dbo.sysobjects where id=object_id(N'dbo.f_getMinShortNum') and objectproperty(id,N'IsInlineFunction')=0)
        drop function f_getMinShortNum
go
create function f_getMinShortNum() returns int
as
begin
        declare @MinShortNum int
        select @MinShortNum=min(id)+1 from (select id from test union select 0) a where not exists (select 1 from test where id=a.id+1)
        --也可:select @MinShortNum=min(id)+1 from (select id from test union select 0) a where id not in (select id-1 from test)
        return @MinShortNum
end
go

--示例(创建一个表test,里面的id号码存在缺号:1,2,3,5,7,9,11...)
create table test(id int)
insert test
select 1
union select 2
union select 3
union select 5
union select 7
union select 9
union select 11
go
select id from test

--调用
select dbo.f_getMinShortNum()
--插入数据
insert test values(dbo.f_getMinShortNum())        --插入了id=4的一行
insert test values(dbo.f_getMinShortNum())        --插入了id=6的一行
select id from test order by id
drop table test

使用道具 举报

回复
论坛徽章:
0
12#
发表于 2004-6-25 14:50 | 只看该作者
收茯

使用道具 举报

回复
论坛徽章:
0
13#
 楼主| 发表于 2004-6-26 09:37 | 只看该作者
非常感谢各位,

特别是 lynx286陈永仁

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表