Run multiple queries from 1 SQL file showing result in multiple tables

和自甴很熟 提交于 2020-01-24 16:32:52

问题


I have a set of queries for regression testing. These are basically select statements stored in different files.

File  Query
A      SELECT a ...
B      SELECT b ...
C      SELECT c ...

This means that I have to open up multiple files and run the queries. Is it possible to put these SELECT statements in the same file but show the results in different tables and/or in different result sets?

I want to have a file D showing the result from the SELECT statements a,b and c in different tables. (It would be preferable just to include the files A,B,C but I can of course copy paste it). Is this possible? In case it is possible then how do I do it?


回答1:


You can just append all select queries in one output file. Put below code in .bat file and use bcp command.

select * from table1 >> textfile1.txt 
select '================' >> textfile1.txt
select * from table2 >> textfile1.txt 
select '================' >> textfile1.txt
select * from table3 >> textfile1.txt 
select '================' >> textfile1.txt



回答2:


It would be easiest to copy and paste the queries in one file and do something like:

create table temp_db.table_name_a as(
    select * from queryATable
)with data primary index(indexes);

create table temp_db.table_name_b as(
    select * from queryBTable
)with data primary index(indexes);

create table temp_db.table_name_c as(
    select * from queryCTable
)with data primary index(indexes);

create table temp_db.combined_table_D as(

    select fields, 'tableAResult Set' as source from temp_db.table_name_a

union all

    select fields, 'tableBResult Set' as source from temp_db.table_name_b

union all 

    select fields, 'tableCResult Set' as source from temp_db.table_name_c

)with data primary index(indexes);



回答3:


without knowing what tools you have access to this is difficult to answer.

However one of the simplest ways to get multiple answer sets is using SQL Assistant.

for example: in your query window type or paste the following

SELECT * FROM dbc.DBCInfo;
SELECT * FROM dbc.DBCInfo;
SELECT * FROM dbc.DBCInfo;

in your menu select file ->export results

hit F5 (or the green feet) it will prompt you for a location and filename enter where you want and file name.

it will then save the results like below RELEASE,14.10.02.11 VERSION,14.10.02.12 LANGUAGE SUPPORT MODE,Standard

 --------------------------------------------------------------------------------

 RELEASE,14.10.02.11
 VERSION,14.10.02.12
 LANGUAGE SUPPORT MODE,Standard

 --------------------------------------------------------------------------------

 RELEASE,14.10.02.11
 VERSION,14.10.02.12
 LANGUAGE SUPPORT MODE,Standard

You can also set in you options to save the sql with each query etc.

hope this helps.




回答4:


In SQL developer the easiest way to run multiple queries at one time and get the results in 1 result instead of multiple tabs is, select all the queries you want to run and instead of running "run statement", run "Run Script" or (F5). This will return the results something like this:


Query Result here


Query Result here





回答5:


If you want multiple responses you can create a stored procedure.

With a stored procedure, you can make one call and it return multiple data sets:

CREATE PROCEDURE Foo     
AS   
    SELECT * AS Table1
    SELECT * AS Table2;
    SELECT * AS Table3;
GO

EXECUTE Foo;

MSDN stored procedure



来源:https://stackoverflow.com/questions/30475865/run-multiple-queries-from-1-sql-file-showing-result-in-multiple-tables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!