棘手的问题:如何返回一个不重复的id值?

magnetmoon 2008-07-11 10:23:17
棘手的问题:如何返回一个不重复的id值?,
注意:是返回不重复的id值,不要用存储过程,请用函数实现
假设函数名是gen_id(),
如果第1次执行select gen_id(),返回一个id值
如果第2次执行select gen_id(),返回一个id值
……
但每次返回的id都不能和前面的相同,用存储过程可以实现,
但是无法在“select dbo.gen_id(),* from 表”中使用,所以请用函数实现
下面是我写的脚本,但是调用时不能通过,为何???
--
if object_id('rid_table')>0 drop table rid_table
go
create table dbo.rid_table (rid_id int identity(1,1))
go
--
if object_id('rid_insert_row')>0 drop proc rid_insert_row
go
create proc rid_insert_row
as
insert into rid_table default values
--
go
if object_id('gen_id')>0 drop function gen_id
go
create function dbo.gen_id()
returns int
as
begin
declare @r int
exec rid_insert_row
select @r=ident_current('rid_table')
return @r
end
--
go
select dbo.gen_id(),* from master.dbo.systypes

...全文
270 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhiguo2008 2008-07-11
  • 打赏
  • 举报
回复
select checksum(newid()) as aa,* from master.dbo.systypes
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 GDC_ZhaoYZ0304360 的回复:]
引用 5 楼 GDC_ZhaoYZ0304360 的回复:
SQL codeselect abs(checksum(NewID())),* from master.dbo.systypes


这样就是数值了


和5楼很相似啊
[/Quote]


sorry,一样的,刚才没有看到,谢谢了
律己修心 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 magnetmoon 的回复:]
引用 26 楼 arrow_gx 的回复:
主要是返回一个id值,就象newid()一样,只是要返回int类型的 ??

哦,明白了,LZ 是想弄一个 相当于 newid() 但是 返回int类型的函数,


是的,好象函数实现不了,我们现在是用存储过程来实现的,
[/Quote]

你没有看5楼写的那个啊?
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 arrow_gx 的回复:]
试试

SQL codeselect checksum(newid()),* from master.dbo.systypes
[/Quote]


這個函數基本可以,我試了一個15萬筆數據的表,但是仍有2筆重復的
看來只有先這樣了,謝謝!
律己修心 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 GDC_ZhaoYZ0304360 的回复:]
SQL codeselect abs(checksum(NewID())),* from master.dbo.systypes



这样就是数值了
[/Quote]

和5楼很相似啊
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
试试
select checksum(newid()),* from master.dbo.systypes 
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
建议用 rand() 试试,把范围放大一些,在一定程度上能避免重复,但不是绝对的
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 arrow_gx 的回复:]
主要是返回一个id值,就象newid()一样,只是要返回int类型的 ??

哦,明白了,LZ 是想弄一个 相当于 newid() 但是 返回int类型的函数,
[/Quote]

是的,好象函数实现不了,我们现在是用存储过程来实现的,
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 magnetmoon 的回复:]
引用 17 楼 arrow_gx 的回复:
用 output 存储过程来实现


SQL codecreate proc rid_insert_row
@id int output
as
insert into rid_table default values
set @id=@@IDENTITY
--
go

declare @id int
exec rid_insert_row @id output
select @id,* from master.dbo.systypes


[/Quote]


你的語句,執行結果如下:
1 bigint
1 binary
1 bit
….
我要的是下面的執行結果
1 bigint
2 binary
3 bit
….
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
主要是返回一个id值,就象newid()一样,只是要返回int类型的 ??

哦,明白了,LZ 是想弄一个 相当于 newid() 但是 返回int类型的函数,
pxpsoft 2008-07-11
  • 打赏
  • 举报
回复
up
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 arrow_gx 的回复:]
这样不行,返回的值都是相同的,我要的是返回不同的值 ???

你的应用和你的描述,好像偏差比较大,不理解,能说得更清楚一些吗??你要实现什么呢 ???
[/Quote]

主要是返回一个id值,就象newid()一样,只是要返回int类型的,
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 arrow_gx 的回复:]
啊,都写到这样子了,你都不会用啊,用动态语句不就可以了吗??
[/Quote]

你的语句是返回的值都是相同的,我要的是返回不同的值
用动态语句?能否详细点?
前面也说了:用存储过程可以实现,
但是无法在“select dbo.gen_id(),* from 表”中使用,所以请用函数实现
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
这样不行,返回的值都是相同的,我要的是返回不同的值 ???

你的应用和你的描述,好像偏差比较大,不理解,能说得更清楚一些吗??你要实现什么呢 ???
-狙击手- 2008-07-11
  • 打赏
  • 举报
回复
。。
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
啊,都写到这样子了,你都不会用啊,用动态语句不就可以了吗??
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 arrow_gx 的回复:]
用 output 存储过程来实现


SQL codecreate proc rid_insert_row
@id int output
as
insert into rid_table default values
set @id=@@IDENTITY
--
go

declare @id int
exec rid_insert_row @id output
select @id,* from master.dbo.systypes
[/Quote]

这样不行,返回的值都是相同的,我要的是返回不同的值
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复

[Quote=引用 17 楼 arrow_gx 的回复:]
用 output 存储过程来实现


SQL codecreate proc rid_insert_row
@id int output
as
insert into rid_table default values
set @id=@@IDENTITY
--
go

declare @id int
exec rid_insert_row @id output
select @id,* from master.dbo.systypes
[/Quote]



谢谢,但是存储过程不能在select dbo.gen_id(),* from master.dbo.systypes中使用
这点不方便
arrow_gx 2008-07-11
  • 打赏
  • 举报
回复
用 output 存储过程来实现

create proc rid_insert_row 
@id int output
as
insert into rid_table default values
set @id=@@IDENTITY
--
go

declare @id int
exec rid_insert_row @id output
select @id,* from master.dbo.systypes
magnetmoon 2008-07-11
  • 打赏
  • 举报
回复
难道函数实现不了这样的功能吗?
加载更多回复(15)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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