简体   繁体   English

是否可以编写包含一对多关系的数据库视图?

[英]Is it possible to write a database view that encompasses one-to-many relationships?

So I'm not necessarily saying this is even a good idea if it were possible, since the schema of the view would be extremely volatile, but is there any way to represent a has-many relationship in a single view? 所以我不一定说如果可能的话,这甚至是个好主意,因为视图的模式会非常不稳定,但有没有办法在单个视图中表示有多个关系?

For example, let's say I have a customer that can have any number of addresses in the database. 例如,假设我的客户可以在数据库中拥有任意数量的地址。 Is there any way to list out each column of each address with perhaps a number as a part of the alias (eg, columns like Customer Id, Name, Address_Street_1, Address_Street_2, etc)? 有没有办法列出每个地址的每一列,可能有一个数字作为别名的一部分(例如,像Customer Id,Name,Address_Street_1,Address_Street_2等列)?

Thanks! 谢谢!

Yes - use: 是的 - 使用:

CREATE VIEW customer_addresses AS
    SELECT t.customer_id,
           t.customer_name,
           a1.street AS address_street_1,
           a2.street AS address_street_2
      FROM CUSTOMER t
 LEFT JOIN ADDRESS a1 ON a1.customer_id = t.customer_id
 LEFT JOIN ADDRESS a2 ON a2.customer_id = t.customer_id

If you provided more info, it'd be easier to give you a better answer. 如果您提供了更多信息,那么更容易为您提供更好的答案。 It's possible you're looking to pivot data (turn rows into columns). 您可能正在寻找数据透视(将行转换为列)。

Simply put, no. 简单地说,不。 Not without dynamically recreating the view every time you want to use it at least, that is. 每次你想要使用它时动态重新创建视图,就是这样。

But, what you can do is predefine, say, 4 address columns in your view, then populate the first four results of your one-to-many relation into those columns. 但是,您可以做的是预定义,例如,视图中的4个地址列,然后将您的一对多关系的前四个结果填充到这些列中。 It's not quite the dynamic view you want, but it's also much more stable/usable in my opinion. 这不是你想要的动态视图,但在我看来它也更加稳定/可用。

Not really - you really are doing a dynamic pivot. 不是真的 - 你真的在做一个动态的支点。 It's possible to use OPENROWSET to get to a dynamically generated query, but whether that's advisable, it's hard to say without seeing more about the business case. 可以使用OPENROWSET来获取动态生成的查询,但是否这是可取的,如果不了解更多关于业务案例的话,很难说。

First make a stored proc which does the dynamic pivot like I did on the StackExchange Data Explorer. 首先创建一个存储过程,它像我在StackExchange数据资源管理器上那样进行动态数据透视

Basically, you generate dynamic SQL which builds the column list. 基本上,您生成用于构建列列表的动态SQL。 This can only really be done in a stored proc. 这只能在存储过程中完成。 Which is fine for applciation calls. 哪个适用于applciation调用。

But what about if you want to re-use that in a lot of different joins or ad hoc queries? 但是,如果你想在很多不同的连接或即席查询中重复使用它呢?

Then, have a look at this article : "Using SQL Servers OPENROWSET to break the rules" 然后,看看这篇文章 :“使用SQL Server OPENROWSET打破规则”

You can now call your stored proc by looping back into the server and then getting the results into a rowset - this can be in a view! 您现在可以通过循环回到服务器然后将结果放入行集来调用存储过程 - 这可以在视图中!

The late Ken Henderson has some good examples of this in his excellent book : "The Guru's Guide to SQL Server Stored Procedures, XML, and HTML" (you got to love the little "Covers .NET!" on the cover which captures well the zeitgeist for 2002!). 已故的Ken Henderson在他出色的书中有一些很好的例子:“大师的SQL Server存储过程指南,XML和HTML”(你必须喜欢封面上的小“封面.NET!”) 2002年的时代精神!)。

He only covers the loopback part (with views and user-defined functions), the less verbose PIVOT syntax was not available until 2005, but PIVOTs can also be generated using a CASE statement as a characteristic function . 他只涉及环回部分(使用视图和用户定义的函数),直到2005年才能获得较不详细的PIVOT语法,但也可以使用CASE语句作为特征函数生成PIVOT。

Obviously, this technique has caveats (I can't even do this on our production server). 显然,这种技术有一些警告(我甚至无法在我们的生产服务器上执行此操作)。

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

相关问题 具有一对多关系的多表联接 - Multiple Table Joins With One-to-Many Relationships BizTalk WCF SQL适配器:如何从具有一对多关系的数据库接收记录 - BizTalk WCF SQL Adapter: How to receive records from a database with one-to-many relationships 如何代表具有几个一对多关系的表 - How to represent a table with several one-to-many relationships 在SQL Server中联接3个具有一对多和多对多关系的表 - Joining 3 tables having one-to-many and many-to-many relationships in SQL Server SQL Server:获取具有一对一和一对多关系的所有外键 - SQL Server: Get All foreign keys with one-to-one and one-to-many Relationships SQL 视图,性能和计数从一对多的关系 - SQL view, performance and count from one-to-many relationship 一对多关系:如何返回子表单行的几列? - One-to-many relationships: how to return several columns of a single row of the child table? 一对多加入 - One-to-Many Join 实体框架代码具有指向单个表的多个一对多关系的第一个配置 - Entity Framework Code First configuration with multiple one-to-many relationships pointing to a single table 如何为要与dataTable插入的一对多关系表编写插入语句 - how to write insert statements for one to many relationships tables to insert with dataTable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM