截取guid..........

曲军昌 2013-01-31 05:00:24
我现在要写一个存储过程,输出一个值(输出的是是newid的前8位)
不过要和一个表里的code进行对比,如果存在就继续截取,不存在就直接输出。
改怎么写啊???
if OBJECT_ID('code','u') is not null
drop table code
go
create table code
(
ID int identity(1,1) not null,
Code varchar(8) not null,
VGUID uniqueidentifier default(newid()) not null
)
insert into code(Code) values (LEFT(NEWID(),8))
...全文
248 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanpeng1711 2013-02-01
  • 打赏
  • 举报
回复
declare @guid varchar(8) set @guid=left(newid(),8) while 1=1 begin if exists ( select 1 from guidTest where guid=@guid ) begin print 'exists:'+@guid set @guid=left(newid(),8) end else begin print 'notExists:'+@guid insert into guidTest values(@guid) break end end
tanpeng1711 2013-02-01
  • 打赏
  • 举报
回复
declare @guid varchar(8), @count int set @guid=left(newid(),8) set @count =1 while @count>0 begin select @count=count(*) from guidTest where guid=@guid if @count=0 begin insert into guidTest(guid) values(@guid) end else begin set @guid=left(newid(),8) end end
叶子 2013-02-01
  • 打赏
  • 举报
回复
我觉得 #3 即可。
chuifengde 2013-02-01
  • 打赏
  • 举报
回复
DECLARE @a VARCHAR(8)

A:
SELECT @a=left(NEWID(),8)
IF EXISTS(SELECT 1 FROM Code WHERE code=@a)
	GOTO A
INSERT Code(Code) SELECT @a
shulei521 2013-01-31
  • 打赏
  • 举报
回复
create procedure test as declare @str char(8) declare @a int set @a=1 select @str=SUBSTRING( NEWID(),0,9) while(@a=1) begin if exists (select * from code where code=@str) begin select @str=SUBSTRING( NEWID(),0,9) end else begin set @a=2 end end select @str
shoppo0505 2013-01-31
  • 打赏
  • 举报
回复
大致如下: declare @id uniqueidentifier = newid() , @idtemp uniqueidentifier set @idtemp = (select newid()) while ((select LEFT(@idtemp,8)) <> (select LEFT(@id,8))) begin set @idtemp = (select newid()) end select @id, @idtemp 提示,这个循环几乎不太可能结束。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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