简体   繁体   English

从几个表之一获取数据 SQL

[英]Get data from one of several tables SQL

Currently we have 4 tables containing information about a particular record:目前我们有 4 个表,其中包含有关特定记录的信息:

  • AUT contains incomplete record information AUT 包含不完整的记录信息
  • REP contains record information that isn't cleaned REP 包含未清理的记录信息
  • KWN contains complete record information KWN 包含完整的记录信息
  • UPD contains records that are updates UPD 包含更新的记录

All records contain the following data:所有记录都包含以下数据:

record = {
   param1?: string;
   param2?: number;
   param3?: boolean;
}

However, depending on which table the record came from, we may have additional information, such as a timestamp, a signature of the inserter, a counter of some sort, etc. which could be stored in 1 additional column or several.但是,根据记录来自哪个表,我们可能会有额外的信息,例如时间戳、插入器的签名、某种类型的计数器等,这些信息可以存储在 1 个或多个附加列中。 (in the example below, I've shown one extra param for each table for simplicity) (在下面的示例中,为简单起见,我为每个表显示了一个额外的参数)

Right now, if we want to search for all information about a specific record across all four tables, we have a large IF/ELSE block with very WET code, such as:现在,如果我们想在所有四个表中搜索有关特定记录的所有信息,我们有一个带有非常 WET 代码的大型 IF/ELSE 块,例如:

PROCEDURE dbo.myProcedure
         @search_id VARCHAR(36)
AS  
     DECLARE @source VARCHAR(3)
     SET @source = 'unk'

     -- FIGURE OUT WHICH SOURCE: 'kwn', 'upd', 'rep', 'aut'

     IF @source == 'upd'
     BEGIN
        SELECT u.param1, u.param2, u.param3, u.updOnlyParam
        FROM upd_table u
        WHERE u.search_id == @search_id
     END
     ELSE IF @source == 'kwn'
     BEGIN
        SELECT k.param1, k.param2, k.param3, k.kwnOnlyParam
        FROM kwn_table k
        WHERE k.search_id == @search_id
     END
     ELSE IF @source == 'rep'
     BEGIN
        SELECT r.param1, r.param2, r.param3, r.repOnlyParam
        FROM rep_table r
        WHERE r.search_id == @search_id
     END
     ELSE IF @source == 'aut'
     BEGIN
        SELECT a.param1, a.param2, a.param3, a.autOnlyParam
        FROM aut_table a
        WHERE a.search_id == @search_id
     END

What would be ideal is if it were possible to do something like:理想的情况是,如果可以执行以下操作:

PROCEDURE dbo.myProcedure
         @search_id VARCHAR(36)
AS  
     DECLARE @source VARCHAR(3)
     SET @source = 'unk'

     -- FIGURE OUT WHICH SOURCE: 'kwn', 'upd', 'rep', 'aut'

     SELECT 
        t.param1, 
        t.param2, 
        t.param3, 
        t.updOnlyParam, -- column only present when t is upd_table
        t.kwnOnlyParam, -- column only present when t is kwn_table
        t.repOnlyParam, -- column only present when t is rep_table
        t.autOnlyParam  -- column only present when t is aut_table
     FROM 
        CASE 
           WHEN @source == 'upd' THEN upd_table t
           WHEN @source == 'kwn' THEN kwn_table t
           WHEN @source == 'rep' THEN rep_table t
           ELSE aut_table t
        END
     WHERE t.search_id == @search_id

However, this doesn't work on two accounts:但是,这不适用于两个帐户:

  1. Putting a case statement in a FROM clause isn't supported不支持将 case 语句放在 FROM 子句中
  2. In the SELECT clause, if a column doesn't exist, it throws an error在 SELECT 子句中,如果列不存在,则会引发错误

I am aware that the code above is non-functional and may not be 'fixable' I am wondering whether there exists a better way of querying off of multiple tables in a single statement similar to the above?我知道上面的代码不起作用并且可能无法“修复”我想知道是否存在一种更好的方法来在类似于上面的单个语句中查询多个表?

The most obvious solution is to combine all four tables to have a single table with all columns from all tables present with an extra column to identify which "case" the row pertains to.最明显的解决方案是将所有四个表组合成一个表,其中所有表中的所有列都存在一个额外的列,以识别该行属于哪个“案例”。 However: that is NOT what I am asking.但是:这不是我要问的。 I am asking how, keeping the tables separate, I could query off of multiple tables with similar column members, and some special columns for each table.我在问如何,保持表分开,我可以查询具有相似列成员的多个表,以及每个表的一些特殊列。

Thank you for your input.谢谢您的意见。

The best way, create a view and there's no need to define new columns for every table.最好的方法是创建一个视图,而无需为每个表定义新列。 like below:如下所示:

SELECT 'aut' as source, a.param1, a.param2, a.param3, a.autOnlyParam as OnlyParam, a.search_id
FROM aut_table a
 union all
SELECT 'rep' as source, r.param1, r.param2, r.param3, r.repOnlyParam as OnlyParam, r.search_id
FROM rep_table r
union all
SELECT 'kwn' as source, k.param1, k.param2, k.param3, k.kwnOnlyParam as OnlyParam, k.search_id
FROM kwn_table k
union all
SELECT 'upd' as source, u.param1, u.param2, u.param3, u.updOnlyParam as OnlyParam, u.search_id
  FROM upd_table u

after creating this view, you just need to use this view below:创建此视图后,您只需要在下面使用此视图:

  SELECT k.param1, k.param2, k.param3, k.OnlyParam
     FROM your_named_view_name k
    WHERE k.search_id = @search_id and source = @source

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM