473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Writing multiple queries to a file with BCP

Hi

I am trying to write two Select * statements to the same text file
using bcp (from a stored procedure).

But cannot find a way of appending to a file using bcp.

Does anyone know if this is possible or is there another way of writing
multiple queries to a file from a stored procedure?

Thanks
Caro

Jul 23 '05 #1
2 6885
caro (ca***@orange.net) writes:
I am trying to write two Select * statements to the same text file
using bcp (from a stored procedure).

But cannot find a way of appending to a file using bcp.

Does anyone know if this is possible or is there another way of writing
multiple queries to a file from a stored procedure?


If the queries have the same structure on their result sets, you could
combine then with UNION:

SELECT ....
UNION ALL
SELECT ...

However, assuming that you want all the rows from the first query to
come before all rows in the second query, you need an ORDER BY clause
to handle this. The standard trick is to add an extra column:

SELECT queryno = 1, ....
UNION ALL
SELECT 2, ...
ORDER BY queryno

But this would mean in your case that you get an extra column in the
file. You could circumvent this by creating a temp table with an
IDENTITY column, and then SELECT from this temp table ordering by
this IDENTITY column, without including that column itself.

A much simpler solution could be to bulk copy to separate files,
and then use the COPY command to combine the files into one. Do
COPY /? at the command prompt for more information.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Hi

Thanks for the suggestions! I think I will just copy the files together
(as origninally planned - since thats really straight forward.

Thanks again

Caro

Erland Sommarskog wrote:
caro (ca***@orange.net) writes:
I am trying to write two Select * statements to the same text file
using bcp (from a stored procedure).

But cannot find a way of appending to a file using bcp.

Does anyone know if this is possible or is there another way of writing
multiple queries to a file from a stored procedure?


If the queries have the same structure on their result sets, you could
combine then with UNION:

SELECT ....
UNION ALL
SELECT ...

However, assuming that you want all the rows from the first query to
come before all rows in the second query, you need an ORDER BY clause
to handle this. The standard trick is to add an extra column:

SELECT queryno = 1, ....
UNION ALL
SELECT 2, ...
ORDER BY queryno

But this would mean in your case that you get an extra column in the
file. You could circumvent this by creating a temp table with an
IDENTITY column, and then SELECT from this temp table ordering by
this IDENTITY column, without including that column itself.

A much simpler solution could be to bulk copy to separate files,
and then use the COPY command to combine the files into one. Do
COPY /? at the command prompt for more information.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Geoff Berrow | last post by:
This is puzzling. If I run $sql = "INSERT INTO customer (custid,username,password,title,firstname,lastname,street,towncity,postcode,telno,mobile,email,glasses) VALUES...
3
by: Gord | last post by:
I would like to create a summary report from the results of 11 queries (based on 2 tables). All the queries have the same format and return 3 numbers (Count, Current Year Bal, Last Year Bal.)...
4
by: Dave Edwards | last post by:
I understand that I can fill a datagrid with multiple queries, but I cannot figure out how to fill a dataset with the same query but run against multiple SQL servers, the query , table structure...
1
by: mattcatmattcat | last post by:
I have a VB7 aspx file I am creating that requires multiple queries each dependant on the previous queries results. If I run these queries in foxpro, I just run a query then create a cursor with...
8
by: beretta819 | last post by:
Ok, so I apologize in advance for the wordiness of what follows... (I am not looking for someone to make this for me, but to point me in the right direction for the steps I need to take.) I was...
7
by: vaiism | last post by:
I am creating a report that outputs the contact information and details about a water treatment plant, and needs to include information about people who work there. If I tie all the information...
10
by: Terry Olsen | last post by:
I need to be able to write to a file simultaneously from approximately 4 different threads. I'm working on a program that will download parts of a file and combine the parts. Each thread will have...
4
by: Akhenaten | last post by:
I am currently using enterprise manager to run multiple queries on a single table in a DB. I refresh these queries every few minutes. Due to the huge number of them I was looking for a better way...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.