简体   繁体   中英

SQL Server Query Alter ID field to make it incremental in a View

I have created a view from two tables morning shift and night shift employees

在此处输入图像描述

My view looks like this:

USE Employees
 GO
 CREATE VIEW EmployeesShift
 SELECT MEID AS ID, MEName AS Name, Status, Address
 FROM MorningEmployees
 UNION ALL
 SELECT NEID AS ID, NEName AS Name, Status, Address
 FROM NightEmployees

The resulting table looks like this

在此处输入图像描述

However I want the values of ID field to look like this

在此处输入图像描述

Can Anyone help please?

https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-ver15

You can use a windowing function to generate a unique ID for every row on the fly

select * from (
SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 10000)) AS id FROM dbo.EmployeesShift) a
order by a.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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