Add Identity column to a view in SQL Server 2008(将标识列添加到 SQL Server 2008 中的视图)
问题描述
这是我的观点:
Create View [MyView] as
(
Select col1, col2, col3 From Table1
UnionAll
Select col1, col2, col3 From Table2
)
我需要添加一个名为 Id
的新列,我需要这个列是唯一的,所以我想添加新列作为身份.我必须提到这个视图返回了大量数据,所以我需要一种具有良好性能的方法,而且我使用了两个带有联合的选择查询,我认为这可能有些复杂,那么您的建议是什么?
I need to add a new column named Id
and I need to this column be unique so I think to add new column as identity. I must mention this view returned a large of data so I need a way with good performance, And also I use two select query with union all I think this might be some complicated so what is your suggestion?
推荐答案
在 SQL Server 2008 中使用 ROW_NUMBER()
函数.
Use the ROW_NUMBER()
function in SQL Server 2008.
Create View [MyView] as
SELECT ROW_NUMBER() OVER( ORDER BY col1 ) AS id, col1, col2, col3
FROM(
Select col1, col2, col3 From Table1
Union All
Select col1, col2, col3 From Table2 ) AS MyResults
GO
这篇关于将标识列添加到 SQL Server 2008 中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将标识列添加到 SQL Server 2008 中的视图


基础教程推荐
- Sql Server 字符串到日期的转换 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01