简体   繁体   English

将标识列添加到 SQL Server 2008 中的视图

[英]Add Identity column to a view in SQL Server 2008

This is my view:这是我的观点:

Create View [MyView] as
(
Select col1, col2, col3 From Table1
UnionAll
Select col1, col2, col3 From Table2
)

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.我需要添加一个名为Id的新列,我需要这个列是唯一的,所以我想添加新列作为身份。 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?我必须提到这个视图返回了大量数据,所以我需要一种具有良好性能的方法,而且我使用两个带有联合的选择查询,我认为这可能有些复杂,所以你的建议是什么?

Use the ROW_NUMBER() function in SQL Server 2008.在 SQL Server 2008 中使用ROW_NUMBER()函数。

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

The view is just a stored query that does not contain the data itself so you can add a stable ID.该视图只是一个不包含数据本身的存储查询,因此您可以添加一个稳定的 ID。 If you need an id for other purposes like paging for example, you can do something like this:如果您需要一个 id 用于其他目的,例如分页,您可以执行以下操作:

create view MyView as 
(
    select row_number() over ( order by col1) as ID, col1 from  (
        Select col1 From Table1
        Union All
        Select col1 From Table2
    ) a
)

There is no guarantee that the rows returned by a query using ROW_NUMBER() will be ordered exactly the same with each execution unless the following conditions are true:除非满足以下条件,否则无法保证使用 ROW_NUMBER() 的查询返回的行在每次执行时的排序完全相同:

  1. Values of the partitioned column are unique.分区列的值是唯一的。 [partitions are parent-child, like a boss has 3 employees][ignore] 【分区是亲子的,就像一个老板有3个员工】【忽略】
  2. Values of the ORDER BY columns are unique. ORDER BY 列的值是唯一的。 [if column 1 is unique, row_number should be stable] [如果第 1 列是唯一的,row_number 应该是稳定的]
  3. Combinations of values of the partition column and ORDER BY columns are unique.分区列和 ORDER BY 列的值的组合是唯一的。 [if you need 10 columns in your order by to get unique... go for it to make row_number stable]" [如果您的订单中需要 10 列来获得唯一性...去使 row_number 稳定]”

There is a secondary issue here, with this being a view.这里有一个次要问题,这是一个观点。 Order By's don't always work in views (long-time sql bug). Order By 并不总是在视图中工作(长期的 sql 错误)。 Ignoring the row_number() for a second:暂时忽略 row_number() :

create view MyView as 
(
    select top 10000000 [or top 99.9999999 Percent] col1 
    from  (
        Select col1 From Table1
        Union All
        Select col1 From Table2
    ) a order by col1
)

Using "row_number() over ( order by col1) as ID" is very expensive.使用“row_number() over (order by col1) as ID”是非常昂贵的。 This way is much more efficient in cost:这种方式在成本上更有效:

Create View [MyView] as
(
    Select ID = isnull(cast(newid() as varchar(40)), '')
           , col1
           , col2
           , col3 
    From Table1
    UnionAll
    Select ID = isnull(cast(newid() as varchar(40)), '')
           , col1
           , col2
           , col3 
    From Table2
)

use ROW_NUMBER() with "order by (select null)" this will be less expensive and will get your result.将 ROW_NUMBER() 与“order by (select null)”一起使用,这样成本会更低,并且会得到您的结果。

Create View [MyView] as
SELECT ROW_NUMBER() over (order by (select null)) as id, *
FROM(
    Select col1, col2, col3 From Table1
    Union All
    Select col1, col2, col3 From Table2 ) R
GO

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

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