0

I have a bunch of very simple SELECT statements. I would like to output them all to the same resultset (UI table or file). Here is the most recent thing I tried:

@export on;
@export set filename="c:\test.csv";
@export set CsvColumnDelimiter=",";
SELECT TOP 1 * FROM TableName WHERE ID = 1;
SELECT TOP 1 * FROM TableName WHERE ID = 2;
SELECT TOP 1 * FROM TableName WHERE ID = 3;
SELECT TOP 1 * FROM TableName WHERE ID = 4;
SELECT TOP 1 * FROM TableName WHERE ID = 5;
@export off;

Obviously the CSV file only contains ID 5 because it's just overwriting. Is there any way to append? Or, is there any SQL option outside of DBVis that will allow me to execute all these SQL queries into one result set?

THE JOATMON
  • 16,761
  • 34
  • 110
  • 200

3 Answers3

2

One way to achieve this is to return one result set, using UNION ALL.

SELECT TOP 1 * FROM TableName WHERE ID = 1
UNION ALL
SELECT TOP 1 * FROM TableName WHERE ID = 2
UNION ALL
SELECT TOP 1 * FROM TableName WHERE ID = 3
UNION ALL
SELECT TOP 1 * FROM TableName WHERE ID = 4
UNION ALL
SELECT TOP 1 * FROM TableName WHERE ID = 5;
Community
  • 1
  • 1
devlin carnate
  • 7,947
  • 7
  • 46
  • 77
  • So I did `UNION` and it seems to work. What would be the difference? – THE JOATMON Dec 07 '15 at 17:20
  • To clarify, with `UNION` I get 5 results, as expected. What would/should I see with UNION ALL? Would there be any difference in this example? – THE JOATMON Dec 07 '15 at 17:21
  • 1
    An ordinary UNION eliminates duplicate rows (all values identical) from the result. A UNION ALL preserves duplicate rows in the result. http://docs.intersystems.com/cache20152/csp/docbook/DocBook.UI.Page.cls?KEY=RSQL_union#RSQL_B65538 – DAiMor Dec 07 '15 at 17:33
  • 1
    I linked `UNION ALL` in my answer with the explanation of the difference. I'm assuming since you're selecting * by ID that the results would never have duplicates. Thus, the UNION ALL. – devlin carnate Dec 07 '15 at 17:55
2

Scott, try:

@export on;
@export set filename="<outputfile.csv>" appendfile="true";
select * from tab;
@export set CsvIncludeColumnHeader="false";
select * from tab;
select * from tab;

This will export the first result set with column headers and the following result sets without column headers.

roger
  • 611
  • 4
  • 7
0

In IQ database the client can set an option

set option isql_show_multiple_result_sets = 'on';

2 Questions:

  1. which database is this?
  2. where in the multiple results sets can be returned?