如何获得排序数据库视图的效果?
我希望能够(有效地)对数据库视图进行排序 - 我知道数据库视图中的概念顺序是无效的,但我有以下场景需要处理:
- 第三方遗留应用程序,从数据库读取数据使用 select(*) from tablename 语句的表
- 遗留应用程序对记录的顺序非常敏感
- 我编写的应用程序是为了允许用户更轻松地管理表中的数据,但从表中插入和删除自然会令人不安记录的顺序。
将旧应用程序中的语句更改为 select (*) from tablename order by field 可以解决我的问题,但这不是一个选项。
因此 - 我设置了一个临时表,可以按正确的顺序将数据导出到其中,但这是一个资源匮乏的选项,意味着数据在遗留应用程序中不是“实时”的,并且是额外的工作用户。
我希望能够获得具有这些约束的表格的有序版本。 有什么想法吗?
更新 - 我正在使用 Sybase 12.5,但我想避免与特定 RDBMS 紧密耦合的解决方案 - 它可能会改变。
I'd like to be able to (effectively) sort a database view - I know that conceptually order in a db view is invalid, but I have the following scenario to deal with:
- a third-party legacy application, that reads data from database tables using a select(*) from tablename statement
- the legacy application is very sensitive to the order of the records
- an application I've written to allow users to manage the data in the tables more easily, but inserts and deletes from the table naturally upset the order of the records.
Changing the statement in the legacy application to select (*) from tablename order by field would fix my problem, but isn't an option.
So - I've set up a staging table into which the data can be exported in the right order, but this is a resource-hungry option, means that the data isn't 'live' in the legacy application and is additional work for users.
I'd like to be able to get at an ordered version of the table with these contraints. Any ideas how?
Update - I'm working with Sybase 12.5, but I'd like to avoid a tightly coupled solution with a specific RDBMS - it might change.
I cannot add an 'order by' clause to a view, because of SQL standards as referred to in this Wikipedia entry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,我以前曾参与过此类项目,这确实是个麻烦事。 节哀顺变。
这有点遥远,但如果您的 DBMS 支持它,也许您可以创建一个用户定义的表函数,从您的旧表中执行有序选择,然后设置您的视图以从 UDTF 中进行选择。 但这不是我以前做过的事情。
First off, I've had to work on this type of project before and it's truly a bitch. My condolences.
This is a little out there, but if your DBMS supports it, perhaps you could create a user defined table function that does an ordered select from your legacy table, then set up your view to select from the UDTF. It's not anything I've ever done before though.
您可以尝试表值函数。 您没有指定数据库供应商,但在 TSQL (Sql Server) 中执行此操作的方法如下:
You might try a table-valued function. You didn't specify your database vendor, but here's how you would do it in TSQL (Sql Server):
这不太好,但它有效
It's not nice, but it works
如果我理解正确,你可以通过以下方式解决这个问题:
1) 重命名原表
2) 使用旧应用程序查询的表的名称创建视图。
3) 将视图定义为一个查询,该查询以旧版应用程序期望的方式对记录进行排序。
If I'm understanding this correctly, you could solve this by:
1) Renaming the original table
2) Creating a view with the name of the table that the legacy app queries.
3) Define the view to be a query that orders the records in the way that legacy app expects.
在 MS Sql Server 中,我们可以破坏标准并创建一个视图,例如:
这可以让我们解决这样的问题。 由于 Sybase 和 Sql Server 共享 T-SQL,我认为您也很有可能做到这一点。
或者,您可以在应该作为排序依据的字段上设置聚集索引。 这将强制存储顺序,这将*返回为“自然顺序”。
In MS Sql Server, we can bastardize the standards and create a view such as:
which lets us get around issues like this. Since Sybase and Sql Server share T-SQL, I'd think there's a good chance you could do that as well.
Alternatively, you can set a clustered index on the field that it should be ordered by. This will force storage order, which will* return in that as "natural order".
根据你们提供的信息,看起来我无法在 Sybase ASE 12.5 上做我想做的事情。
MSSQL Server 和 Sybase ASE 15.x 应该可以完成所需的工作 - 希望我能够安排一些事情。 不太确定要接受哪一个,直到我有一些工作,但我会回来接受答案。
Following up on the info you guys have provided, looks like I can't do what I want to on Sybase ASE 12.5.
MSSQL Server and Sybase ASE 15.x should do what's needed - hopefully I'll be able to arrange something. Not really sure which one to accept til I've got something working, but I'll come back and accept an answer then.