插入随机唯一值且介于1到3000个sql server之间 [英] insert random unique value and between 1 and 3000 sql server

查看:139
本文介绍了插入随机唯一值且介于1到3000个sql server之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要

insert unique value and between 1 and 3000 sql server



怎么做\?

 声明  @ st  int = 1
声明  @ rn   int 
同时( @ st < =  3000  )
 开始
  set  @ rn = ABS(Checksum(NewId())% 3000 );
  set   @ st  = @ st + 1;
 插入 插入 HumanResourceSchema.Employee(EmpNo,EmpFname,EmpLname,DeptNo) @ rn  Smith''  Jane''   d1')
 结束 



错误说

Violation of UNIQUE KEY constraint ''c3''. Cannot insert duplicate key in object ''HumanResourceSchema.Employee''. The duplicate key value is (<NULL>)

解决方案

请阅读我对问题的评论.这是一个主意: SQL:生成无冲突的随机整数 [declare @st int=1 declare @rn int while(@st <= 3000) begin set @rn= ABS(Checksum(NewId()) % 3000); set @st = @st+1; insert into HumanResourceSchema.Employee (EmpNo,EmpFname,EmpLname,DeptNo)values(@rn,'Smith','Jane','d1') end



the error say

Violation of UNIQUE KEY constraint ''c3''. Cannot insert duplicate key in object ''HumanResourceSchema.Employee''. The duplicate key value is (<NULL>)

解决方案

Please, read my comment to the question. Here is an idea: SQL: Generate random integers without collisions[^]


这篇关于插入随机唯一值且介于1到3000个sql server之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆