简体   繁体   English

在SQL Server中为null的情况下填充自动增量INT 5位数字值

[英]Populate auto-increment INT 5 digit value in case of null in SQL Server

I have a scenario where I need to auto generate the value of a column if it is null. 我有一种情况,如果该列为null,则需要自动生成该列的值。

Ex: employeeDetails : 例如: employeeDetails

empName empId  empExtension
  A      101   null
  B      102   987
  C      103   986
  D      104   null
  E      105   null

employeeDepartment : employeeDepartment

deptName  empId   
   HR      101
   ADMIN   102
   IT      103
   IT      104
   IT      105

Query 询问

SELECT 
    empdt.empId, empdprt.deptName, empdt.empExtension 
FROM 
    employeeDetails empdt
LEFT JOIN 
    employeeDepartment empdprt ON empdt.empId = empdprt.empId

Output: 输出:

empId deptName empExtension
 101    HR          null
 102    ADMIN       987
 103    IT          986
 104    IT          null
 105    IT          null

Now my question is I want to insert some dummy value which replaces null and auto-increments starting from a 5 digit INT number 现在我的问题是我想插入一些虚拟值,以替换从5位数INT编号开始的null和自动增量

Expected output: 预期产量:

empId     deptName    empExtension
 101    HR          12345
 102    ADMIN       987
 103    IT          986
 104    IT          12346
 105    IT          12347

Constraints : I cannot change existing tables structure or any column's datatypes. 约束:我无法更改现有表的结构或任何列的数据类型。

You should be able to do that with a CTE to grab ROW_NUMBER , and then COALESCE to only use that number where the value is NULL: 您应该能够使用CTE来捕获ROW_NUMBER ,然后使用COALESCE来仅使用该值为NULL的数字来做到这一点:

WITH cte AS(
  SELECT empId, empExtension, ROW_NUMBER() OVER(ORDER BY empExtension, empId) rn
  FROM employeeDetails
)
SELECT cte.empId, deptName, COALESCE(empExtension, rn + 12344) empExtension
FROM cte LEFT JOIN employeeDepartment
  ON cte.empID = employeeDepartment.empID
ORDER BY cte.empId

Here's an SQLFiddle . 这是一个SQLFiddle

If you just want to create a unique random 5 digit number for those empExtension column values are null, then 如果您只想为这些empExtension列的值创建一个唯一的5位随机数字, empExtension null,然后

Query 询问

;with cte as
(
    select rn = row_number() over
    (
        order by empId
    ),*
    from employeeDetails
)
select t1.empId,t2.deptName,
case when t1.empExtension is null 
then t1.rn + (convert(numeric(5,0),rand() * 20000) + 10000) 
else t1.empExtension end as empExtension
from cte t1
left join employeeDepartment t2
on t1.empId = t2.empId;

SQL Fiddle SQL小提琴

 declare @rand int = (rand()* 12345);
SELECT empdt.empId, empdprt.deptName, 
        isnull(empdt.empExtension,row_number() over(order by empdt.empId)+@rand)
   FROM employeeDetails empdt
LEFT JOIN employeeDepartment empdprt
   ON empdt.empId = empdprt.empId

Incase the empExtension, get the row_number + a random number. 如果是empExtension,则获取row_number +一个随机数。

Just an idea, can you save result of that query into temp table 只是一个想法,您可以将查询结果保存到临时表中吗

SELECT empdt.empId, empdprt.deptName, empdt.empExtension
INTO #TempEmployee
FROM employeeDetails empdt
LEFT JOIN employeeDepartment empdprt
ON empdt.empId = empdprt.empId

And after that just do the update of #TempEmployee? 之后,只需更新#TempEmployee?

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

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