简体   繁体   English

合并多个选择语句输出

[英]Combining multiple select statements output

I have list of view in an excel. 我在Excel中有视图列表。 I have to check whether they are present in the DB or not. 我必须检查它们是否存在于数据库中。 Is there any way to write an query to get this done. 有什么办法可以编写查询来完成此任务。

Right now I am querying ALL_VIEWS to know whether a view is present or not. 现在,我正在查询ALL_VIEWS以了解是否存在视图。

select VIEW_NAME from ALL_VIEWS where VIEW_NAME = 'ABC_V_DEF';

Do I need to execute the above statement for all the view present with me or there is query? 我是否需要为存在的所有视图执行以上语句,还是有查询? I am thinking to combine select query output to get this done. 我正在考虑结合select查询输出来完成此操作。 The output may look like... Please suggest... 输出可能看起来像...请建议...

+-----------------------------
|  `VIEW_NAME`|`View Present?|
+----------------------------+
|  ABC_V_DEF  |    Yes       |
|  ABC_V_XCV  |     No       |
|      .      |     .        |
|      .      |     .        |
+-------------+--------------+

You can construct the select queries inside the o inline view from the list of views in excel and then run it like this. 您可以从excel视图列表中的o内联视图内部构造选择查询,然后像这样运行它。

SELECT o.view_name,
       CASE
         WHEN EXISTS (SELECT 1
                      FROM   all_views a
                      WHERE  a.view_name = o.view_name) THEN 'Yes'
         ELSE 'No'
       END "View Present?"
FROM   (SELECT 'ABC_V_DEF' VIEW_NAME
        FROM   dual
        UNION ALL
        SELECT 'ABC_V_DEF2' VIEW_NAME
        FROM   dual
        UNION ALL
        SELECT 'ABC_V_DEF3' VIEW_NAME
        FROM   dual
        UNION ALL
        SELECT 'ABC_V_DEF4' VIEW_NAME
        FROM   dual) o;  

You can use an IN statement for this. 您可以为此使用IN语句。 I usually use an Excel formula to build the IN statement like, for example: 我通常使用Excel公式来构建IN语句,例如:

="'" & A1 & "', "

with A1 being the View Name if your Excel file. 如果您的Excel文件为A1,则视图名称为A1。

That way it's easy to make the query: 这样,进行查询就很容易了:

select VIEW_NAME from ALL_VIEWS where VIEW_NAME IN ('ABC_V_DEF', 'NAME_2', 'NAME_3', 'ETC');

That will return only the views that are in your db from your list. 这将仅返回列表中数据库中的视图。

The second option is to create a temp table with all of your possible views from your Excel file. 第二个选项是创建一个临时表,其中包含来自Excel文件的所有可能的视图。 Then you can do: 然后,您可以执行以下操作:

SELECT T.VIEW_NAME, CASE WHEN AV.VIEW_NAME IS NULL THEN 'No' ELSE 'Yes' END "View Present?" FROM TEMP_TABLE T LEFT OUTER JOIN ALL_VIEWS AV ON T.VIEW_NAME = AV.VIEW_Name

Take your list of views, create a flat file and an EXTERNAL TABLE definition for the file and then run the following query; 获取视图列表,创建一个平面文件和该文件的EXTERNAL TABLE定义,然后运行以下查询;

SELECT evl.view_name,
       CASE WHEN av.view_name IS NULL THEN 'No' ELSE 'Yes' END AS "View Present?"
FROM   ext_table_views evl
       LEFT OUTER JOIN
       all_views av ON av.view_name = evl.view_name

The advantage of this is that you can have a list as long as you like and running with a new list simply involves replacing the file. 这样做的好处是,您可以根据需要拥有一个列表,而使用新列表运行只需替换文件即可。

I guess that it really depends on how man view names you will be checking. 我想这实际上取决于您将要检查的人如何看待姓名。

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

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