相当于 SQL Server 中 Oracle 的 RowID
SQL Server 中 Oracle 的 RowID 相当于什么?
What's the equivalent of Oracle's RowID in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
SQL Server 中 Oracle 的 RowID 相当于什么?
What's the equivalent of Oracle's RowID in SQL Server?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
来自 Oracle 文档
SQL Server 中与此最接近的等效项是
rid
,它具有三个组件File:Page:Slot
。在 SQL Server 2008 中,可以使用未记录且不受支持的
%%physloc%%
虚拟列来查看这一点。 这会返回一个binary(8)
值,前四个字节中包含页面 ID,然后是文件 ID 的 2 个字节,最后是页面上插槽位置的 2 个字节。标量函数
sys.fn_PhysLocFormatter
或sys.fn_PhysLocCracker
TVF 可用于将其转换为更易读的形式示例输出
请注意,查询处理器不会利用它。 虽然可能在
WHERE
子句中使用它,但SQL Server将不会直接查找指定的行。 相反,它会进行全表扫描,评估每一行的
%%physloc%%
并返回匹配的行(如果有)。要反转前面提到的 2 个函数执行的过程并获取与已知 File、Page、Slot 值相对应的
binary(8)
值,可以使用以下命令。From the Oracle docs
The closest equivalent to this in SQL Server is the
rid
which has three componentsFile:Page:Slot
.In SQL Server 2008 it is possible to use the undocumented and unsupported
%%physloc%%
virtual column to see this. This returns abinary(8)
value with the Page ID in the first four bytes, then 2 bytes for File ID, followed by 2 bytes for the slot location on the page.The scalar function
sys.fn_PhysLocFormatter
or thesys.fn_PhysLocCracker
TVF can be used to convert this into a more readable formExample Output
Note that this is not leveraged by the query processor. Whilst it is possible to use this in a
WHERE
clauseSQL Server will not directly seek to the specified row. Instead it will do a full table scan, evaluate
%%physloc%%
for each row and return the one that matches (if any do).To reverse the process carried out by the 2 previously mentioned functions and get the
binary(8)
value corresponding to known File,Page,Slot values the below can be used.我必须对一个包含许多列的非常大的表进行重复数据删除,速度很重要。 因此,我使用适用于任何表的方法:
I have to dedupe a very big table with many columns and speed is important. Thus I use this method which works for any table:
如果您想唯一标识表中的一行而不是结果集,那么您需要考虑使用 IDENTITY 列之类的东西。 请参阅 SQL Server 帮助中的“IDENTITY 属性”。 SQL Server 不会像 Oracle 那样为表中的每一行自动生成 ID,因此您必须费力创建自己的 ID 列并在查询中显式获取它。
编辑:对于结果集行的动态编号,请参见下文,但这可能与 Oracle 的 ROWNUM 等效,并且我从页面上的所有评论中假设您需要上面的内容。
对于 SQL Server 2005 及更高版本,您可以使用新的排名函数函数来实现行的动态编号。
例如,我对我的查询执行此操作:
会给您:
还有一篇关于 support.microsoft.com 关于动态编号行。
If you want to uniquely identify a row within the table rather than your result set, then you need to look at using something like an IDENTITY column. See "IDENTITY property" in the SQL Server help. SQL Server does not auto-generate an ID for each row in the table as Oracle does, so you have to go to the trouble of creating your own ID column and explicitly fetch it in your query.
EDIT: for dynamic numbering of result set rows see below, but that would probably an equivalent for Oracle's ROWNUM and I assume from all the comments on the page that you want the stuff above.
For SQL Server 2005 and later you can use the new Ranking Functions function to achieve dynamic numbering of rows.
For example I do this on a query of mine:
Will give you:
There's also an article on support.microsoft.com on dynamically numbering rows.
查看新的 ROW_NUMBER 函数。 它的工作原理如下:
Check out the new ROW_NUMBER function. It works like this:
上面的几个答案将解决缺乏对特定行的直接引用的问题,但是如果表中的其他行发生更改,将不起作用。 这就是我对答案在技术上不足的标准。
Oracle ROWID 的一个常见用途是提供一种(某种程度上)稳定的方法来选择行,然后返回该行来处理它(例如,更新它)。 查找行的方法(复杂联接、全文搜索或逐行浏览并对数据应用程序测试)可能无法轻松或安全地重新用于限定 UPDATE 语句。
SQL Server RID 似乎提供相同的功能,但不提供相同的性能。 这是我看到的唯一问题,不幸的是,保留 ROWID 的目的是避免重复昂贵的操作来查找行(例如,在一个非常大的表中)。 尽管如此,许多情况下的性能是可以接受的。 如果微软在未来的版本中调整优化器,性能问题可能会得到解决。
也可以简单地使用 FOR UPDATE 并在过程程序中保持 CURSOR 打开。 然而,这在大型或复杂的批处理中可能会很昂贵。
警告:如果 DBA 在 SELECT 和 UPDATE 之间重建数据库,即使 Oracle 的 ROWID 也不稳定,因为它是物理行标识符。 因此,ROWID 设备只能在范围明确的任务中使用。
Several of the answers above will work around the lack of a direct reference to a specific row, but will not work if changes occur to the other rows in a table. That is my criteria for which answers fall technically short.
A common use of Oracle's ROWID is to provide a (somewhat) stable method of selecting rows and later returning to the row to process it (e.g., to UPDATE it). The method of finding a row (complex joins, full-text searching, or browsing row-by-row and applying procedural tests against the data) may not be easily or safely re-used to qualify the UPDATE statement.
The SQL Server RID seems to provide the same functionality, but does not provide the same performance. That is the only issue I see, and unfortunately the purpose of retaining a ROWID is to avoid repeating an expensive operation to find the row in, say, a very large table. Nonetheless, performance for many cases is acceptable. If Microsoft adjusts the optimizer in a future release, the performance issue could be addressed.
It is also possible to simply use FOR UPDATE and keep the CURSOR open in a procedural program. However, this could prove expensive in large or complex batch processing.
Caveat: Even Oracle's ROWID would not be stable if the DBA, between the SELECT and the UPDATE, for example, were to rebuild the database, because it is the physical row identifier. So the ROWID device should only be used within a well-scoped task.
如果您想对表中的行进行永久编号,请不要使用 SQL Server 的 RID 解决方案。 它的性能比旧 386 上的 Access 差。对于 SQL Server,只需创建一个 IDENTITY 列,并使用该列作为聚集主键。 这将在表上放置一个永久、快速的整数 B 树,更重要的是每个非聚集索引将使用它来定位行。 如果您尝试像 Oracle 一样在 SQL Server 中进行开发,您将创建一个性能很差的数据库。 您需要针对引擎进行优化,而不是假装它是不同的引擎。
另外,请不要使用 NewID() 用 GUID 填充主键,否则会降低插入性能。 如果必须使用 GUID,请使用 NewSequentialID() 作为列默认值。 但INT仍然会更快。
另一方面,如果您只想对查询结果的行进行编号,请使用 RowNumber Over() 函数作为查询列之一。
If you want to permanently number the rows in the table, Please don't use the RID solution for SQL Server. It will perform worse than Access on an old 386. For SQL Server simply create an IDENTITY column, and use that column as a clustered primary key. This will place a permanent, fast Integer B-Tree on the table, and more importantly every non-clustered index will use it to locate rows. If you try to develop in SQL Server as if it's Oracle you'll create a poorly performing database. You need to optimize for the engine, not pretend it's a different engine.
also, please don't use the NewID() to populate the Primary Key with GUIDs, you'll kill insert performance. If you must use GUIDs use NewSequentialID() as the column default. But INT will still be faster.
If on the other hand, you simply want to number the rows that result from a query, use the RowNumber Over() function as one of the query columns.
如果您只想为小数据集进行基本行编号,那么像这样的东西怎么样?
if you just want basic row numbering for a small dataset, how about someting like this?
来自http://vyaskn.tripod.com/programming_faq.htm#q17:
From http://vyaskn.tripod.com/programming_faq.htm#q17:
ROWID 是 Oracle 表上的隐藏列,因此,对于 SQL Server,请构建您自己的列。 添加一个名为 ROWID 的列,默认值为
NEWID()
。如何做到这一点: 添加列,使用默认值值,到 SQL Server 中的现有表
ROWID is a hidden column on Oracle tables, so, for SQL Server, build your own. Add a column called ROWID with a default value of
NEWID()
.How to do that: Add column, with default value, to existing table in SQL Server
请参阅 http://msdn.microsoft.com/ en-us/library/aa260631(v=SQL.80).aspx
在 SQL Server 中,时间戳与日期时间列不同。 它用于唯一标识数据库中的一行,不仅仅是一个表,而是整个数据库。
这可以用于乐观并发。 例如
UPDATE [Job] SET [Name]=@Name, [XCustomData]=@XCustomData WHERE ([ModifiedTimeStamp]=@Original_ModifiedTimeStamp AND [GUID]=@Original_GUID
ModifiedTimeStamp 确保您正在更新原始数据,并且如果另一个更新已更新,则会失败发生在行上。
Please see http://msdn.microsoft.com/en-us/library/aa260631(v=SQL.80).aspx
In SQL server a timestamp is not the same as a DateTime column. This is used to uniquely identify a row in a database, not just a table but the entire database.
This can be used for optimistic concurrency. for example
UPDATE [Job] SET [Name]=@Name, [XCustomData]=@XCustomData WHERE ([ModifiedTimeStamp]=@Original_ModifiedTimeStamp AND [GUID]=@Original_GUID
the ModifiedTimeStamp ensures that you are updating the original data and will fail if another update has occurred to the row.
我从 MS SQL 示例中获取了这个示例,您可以看到 @ID 可以与整数或 varchar 或其他值互换。 这与我正在寻找的解决方案相同,因此我正在分享它。 享受!!
I took this example from MS SQL example and you can see the @ID can be interchanged with integer or varchar or whatever. This was the same solution I was looking for, so I am sharing it. Enjoy!!
您可以使用以下方法获取 ROWID:
1.创建一个新表,其中包含自动递增字段
2.使用 Row_Number 分析函数根据您的要求获取序列。我更喜欢这个,因为它在您需要的情况下有所帮助您想要特定字段或字段组合的 row_id 按升序还是降序排列
Sample:Row_Number() Over(Partition by Deptno order by sal desc)
上面的示例将根据每个部门的最高工资为您提供序列号。Partition by 是可选的,您可以根据您的要求删除它
You can get the ROWID by using the methods given below :
1.Create a new table with auto increment field in it
2.Use Row_Number analytical function to get the sequence based on your requirement.I would prefer this because it helps in situations where you are you want the row_id on ascending or descending manner of a specific field or combination of fields
Sample:Row_Number() Over(Partition by Deptno order by sal desc)
Above sample will give you the sequence number based on highest salary of each department.Partition by is optional and you can remove it according to your requirements
请尝试
选择 NEWID()
来源:https://learn.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql
Please try
select NEWID()
Source: https://learn.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql