本文介紹了將標識列添加到 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 中的視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!