<bdo id='a6faE'></bdo><ul id='a6faE'></ul>

  • <i id='a6faE'><tr id='a6faE'><dt id='a6faE'><q id='a6faE'><span id='a6faE'><b id='a6faE'><form id='a6faE'><ins id='a6faE'></ins><ul id='a6faE'></ul><sub id='a6faE'></sub></form><legend id='a6faE'></legend><bdo id='a6faE'><pre id='a6faE'><center id='a6faE'></center></pre></bdo></b><th id='a6faE'></th></span></q></dt></tr></i><div id='a6faE'><tfoot id='a6faE'></tfoot><dl id='a6faE'><fieldset id='a6faE'></fieldset></dl></div>

        <tfoot id='a6faE'></tfoot>
      1. <small id='a6faE'></small><noframes id='a6faE'>

        <legend id='a6faE'><style id='a6faE'><dir id='a6faE'><q id='a6faE'></q></dir></style></legend>

      2. 如何选择 * 但没有“列名在每个视图中必须唯一"

        How to SELECT * but without quot;Column names must be unique in each viewquot;(如何选择 * 但没有“列名在每个视图中必须唯一)

        <small id='63gzg'></small><noframes id='63gzg'>

          <legend id='63gzg'><style id='63gzg'><dir id='63gzg'><q id='63gzg'></q></dir></style></legend>
          • <tfoot id='63gzg'></tfoot>
              <tbody id='63gzg'></tbody>

                <i id='63gzg'><tr id='63gzg'><dt id='63gzg'><q id='63gzg'><span id='63gzg'><b id='63gzg'><form id='63gzg'><ins id='63gzg'></ins><ul id='63gzg'></ul><sub id='63gzg'></sub></form><legend id='63gzg'></legend><bdo id='63gzg'><pre id='63gzg'><center id='63gzg'></center></pre></bdo></b><th id='63gzg'></th></span></q></dt></tr></i><div id='63gzg'><tfoot id='63gzg'></tfoot><dl id='63gzg'><fieldset id='63gzg'></fieldset></dl></div>
                  <bdo id='63gzg'></bdo><ul id='63gzg'></ul>
                  本文介绍了如何选择 * 但没有“列名在每个视图中必须唯一"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要封装一组我们经常在供应商的数据库服务器上使用的表 JOIN.我们在提取等的许多地方重用了相同的 JOIN 逻辑,似乎 VIEW 允许在一个地方定义和维护 JOIN.

                  I need to encapsulate a set of tables JOINs that we freqently make use of on a vendor's database server. We reuse the same JOIN logic in many places in extracts etc. and it seemed a VIEW would allow the JOINs to be defined and maintained in one place.

                  CREATE VIEW MasterView
                  AS
                  SELECT *
                  FROM entity_1 e1
                  INNER JOIN entity_2 e2 ON e2.parent_id = entity_1.id
                  INNER JOIN entity_3 e3 ON e3.parent_id = entity_2.id
                  /* other joins including business logic */
                  etc.
                  

                  问题在于供应商对数据库进行了定期更改(列添加、名称更改),我希望这些更改自动反映在MasterView"中.

                  The trouble is that the vendor makes regular changes to the DB (column additions, name changes) and I want that to be reflected in the "MasterView" automatically.

                  SELECT * 允许这样做,但基础表都有 ID 列,所以我收到每个视图中的列名必须唯一"错误.

                  SELECT * would allow this, but the underlying tables all have ID columns so I get the "Column names in each view must be unique" error.

                  我特别想避免列出表中的列名,因为 a) 它需要频繁维护 b) 每个表有数百列.

                  I specifically want to avoid listing the column names from the tables because a) it requires frequent maintenance b) there are several hundred columns per table.

                  有什么方法可以实现 SELECT * 的动态但有效地排除某些列(即 ID 列)

                  Is there any way to achieve the dynamism of SELECT * but effectively exclude certain columns (i.e. the ID ones)

                  谢谢

                  推荐答案

                  我最终采用了这个方法,基于 Madhivanan 的建议.它类似于 t-clausen.dk 后来建议的(感谢您的努力),尽管我发现 xml 路径样式比游标/等级分区更优雅.

                  I had gone with this in the end, building off of Madhivanan's suggestion. It's similar to what t-clausen.dk later suggested (thanks for your efforts) though I find the xml path style more elegant than cursors / rank partitions.

                  以下在运行时重新创建 MasterView 定义.基础表中的所有列都以表名开头,因此默认情况下我可以在视图中包含两个名称相似的列.仅此一项就解决了我原来的问题,但我还包含了WHERE column_name NOT IN"子句来专门排除某些永远不会在 MasterView 中使用的列.

                  The following recreates the MasterView definition when run. All columns in the underlying tables are prepended with the table name, so I can include two similarly named columns in the view by default. This alone solves my original problem, but I also included the "WHERE column_name NOT IN" clause to specifically exclude certain columns that will never be used in the MasterView.

                  create procedure Utility_RefreshMasterView 
                  as
                  begin
                  
                      declare @entity_columns varchar(max)
                      declare @drop_view_sql varchar(max)
                      declare @alter_view_definition_sql varchar(max)
                  
                      /* create comma separated string of columns from underlying tables aliased to avoid name collisions */
                      select @entity_columns = stuff((
                          select ','+table_name+'.['+column_name+'] AS ['+table_name+'_'+column_name+']' 
                          from information_schema.columns
                          where table_name IN ('entity_1', 'entity_2')
                          and column_name not in ('column to exclude 1', 'column to exclude 2')
                          for xml path('')), 1, 1, '')
                  
                  
                      set @drop_view_sql = 'if exists (select * from sys.views where object_id = object_id(N''[dbo].[MasterView]'')) drop view MasterView'
                  
                      set @alter_view_definition_sql = 
                      'create view MasterView as select ' + @entity_columns + '
                      from entity_1
                      inner join entity_2 on entity_2 .id = entity_1.id
                      /* other joins follow */'
                  
                      exec (@drop_view_sql)
                      exec (@alter_view_definition_sql)
                  
                  end
                  

                  这篇关于如何选择 * 但没有“列名在每个视图中必须唯一"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  sql group by versus distinct(sql group by 与不同)
                  How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)
                  Count number of records returned by group by(统计分组返回的记录数)
                    <tbody id='G8MWk'></tbody>

                  <small id='G8MWk'></small><noframes id='G8MWk'>

                      <bdo id='G8MWk'></bdo><ul id='G8MWk'></ul>

                      <tfoot id='G8MWk'></tfoot>
                    • <i id='G8MWk'><tr id='G8MWk'><dt id='G8MWk'><q id='G8MWk'><span id='G8MWk'><b id='G8MWk'><form id='G8MWk'><ins id='G8MWk'></ins><ul id='G8MWk'></ul><sub id='G8MWk'></sub></form><legend id='G8MWk'></legend><bdo id='G8MWk'><pre id='G8MWk'><center id='G8MWk'></center></pre></bdo></b><th id='G8MWk'></th></span></q></dt></tr></i><div id='G8MWk'><tfoot id='G8MWk'></tfoot><dl id='G8MWk'><fieldset id='G8MWk'></fieldset></dl></div>
                        <legend id='G8MWk'><style id='G8MWk'><dir id='G8MWk'><q id='G8MWk'></q></dir></style></legend>