带有 ID 列的 SQL 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8067959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:12:04  来源:igfitidea点击:

SQL View with ID column

sqlview

提问by GabrielVa

I'm working with a SQL view that I created but I want to add in an ID column (identity seed) as the current one has none. How can I accomplish this in SQL View?

我正在使用我创建的 SQL 视图,但我想添加一个 ID 列(身份种子),因为当前没有。如何在 SQL 视图中完成此操作?

enter image description here

在此处输入图片说明

回答by GreyFairer

If there is no identity column in the underlying table, you can generate one with pseudo-columns.

如果底层表中没有标识列,则可以使用伪列生成一个。

In SQL server: SELECT ROW_NUMBER() OVER (ORDER BY FiscalYear, FiscalMonth), FiscalYear, FiscalMonth, ... FROM ... See http://msdn.microsoft.com/en-us/library/ms186734.aspx

在 SQL 服务器中:SELECT ROW_NUMBER() OVER (ORDER BY FiscalYear, FiscalMonth), FiscalYear, FiscalMonth, ... FROM ... 见http://msdn.microsoft.com/en-us/library/ms186734.aspx

In Oracle: SELECT ROWNUM, FiscalYear, FiscalMonth, ... FROM ... . In oracle, ROWNUM uses the order in the result set.

在 Oracle 中:SELECT ROWNUM, FiscalYear, FiscalMonth, ... FROM ... 。在oracle 中,ROWNUM 使用结果集中的顺序。

回答by RedFilter

You must add the identity column to the underlying table, if it does not exist already. Then you can update the view to include this column.

您必须将标识列添加到基础表中(如果它尚不存在)。然后您可以更新视图以包含此列。

回答by Icarus

If you don't actually care about actually being an identity seed. You could use ROW_NUMBER()to generate a superficial id.

如果您实际上并不关心实际上是身份种子。您可以使用ROW_NUMBER()生成一个表面 id。

回答by Arash

You can simply use the below to automatically add GUID to your VIEW:

您可以简单地使用以下内容自动将 GUID 添加到您的 VIEW:

CREATE VIEW VIEW_Name
AS
   NEWID() AS ID,
   your other columns here
FROM 
   dbo.YourTable