We help IT Professionals succeed at work.

MS SQL Server write query result to text file

Hi, Experts

suppose I have 3 tables in MS SQL Server (2008 R2). Is it possible to write query result of multiple tables to a single text file.

the pseudo code is like below:

open a text file
select top 1 * from table_A
select top 2 * from table_B
select top 3 * from table_C
close text file

* tables are all in different schema.

thanks in advance
Comment
Watch Question

Gary ColtharpSr. Systems Engineer

Commented:
Maybe you could use Union ALL, something like:


select top 1 * from table_A
Union ALL
select top 2 * from table_B
Union ALL
select top 3 * from table_C

Author

Commented:
hi, gcoltharp

my table are all in different schema. I think UNION ALL does not work in this case.
Senior Database Administrator
CERTIFIED EXPERT
Commented:
Hi,

How automated do you want the response?

You can always simply save the results from one or more queries as a text file.

Or to automate something you could use SSIS.

By specifying the columns and converting types where necessary, you _can_ use union all.

Regards
  David

Author

Commented:
thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.