简体   繁体   English

如何检查主表中是否已存在ID

[英]How to check if there already exists an id in a main table

I was wondering if there's a way to check an existing id in the main table when using temporary table,then use another id for inserting records.Since it may cause error for using the same id in the main table. 我想知道使用临时表时是否有办法检查主表中的现有ID,然后使用另一个ID插入记录。因为在主表中使用相同ID可能会导致错误。 Is there a way to do that?I'm using access and works fine with temp table,but i need to check if there's an answer. 我正在使用access并与temp表一起正常工作,但是我需要检查是否有答案。

Have you considered using a GUID? 您是否考虑过使用GUID? May be what you need. 可能就是您所需要的。

however you might try something like this: SQL: 但是,您可以尝试执行以下操作:SQL:

declare @id int set @id=1 声明@id int set @ id = 1

while exists (select 1 from TABLE1 where id=@id) begin set @id = @id+1 end 存在时(从TABLE1中选择1,其中id = @ id)开始设置@id = @ id + 1结束

I hope this helps, -Thomas RosSQL.blogspot.com 希望对您有所帮助-Thomas RosSQL.blogspot.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM