473,394 Members | 1,703 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,394 software developers and data experts.

Can you specify Query results file name in query itself?

I am running MS SQL Server 2008.

I frequently need to manually run a large quantity of query files at a time. I need to log the results of the queries to a file without having to enter a new file name with each query executed.

I wanted to automate the running of the batch of queries through the OSQL command specifying an output file, but
each query builds on the query executed prior to itself so if the query has errors, I need to fix the errors before proceeding to the next query.

If I could include the results file name within the queries themselves, it would allow me to run through the batch much quicker.

Is there a command to specify the results file name within the query itself? Or is there a way to specify a static results file name within the Management studio?

Any help is appreciated!

Thanks,

MarkLafferty
Jan 8 '13 #1
2 2255
ck9663
2,878 Expert 2GB
There are a couple of solutions that I can think of:
1. SSIS. I have not tried to make it to spit out to a dynamic file name, but I heard it's possible.
2. Create a short SP that dynamically build your query and export using BCP.

I believe either one can do error handling.

Good Luck!!!

~~ CK
Jan 8 '13 #2
Thanks for the suggestion CK.

After running through a number of different scenarios, I ended up with the following batch file process:

echo Release <Job Name> SQL Scritps Executed> <Location of Log Files>\<Log File Name>.txt
echo ------------------------------------->> <Location of Log Files>\<Log File Name>.txt

call osql -E -i <Location of Script>\<Script1 Name>.SQL
if %errorlevel% neq 0 echo SQL Error occurred - close window to cancel or hit any key to continue - %errorlevel% 1>&2 & pause
if %errorlevel% neq 0 echo <Script1 Name>.SQL had errors that were resolved >> <Location of Log Files>\<Log File Name>.txt
if %errorlevel% equ 0 echo <Script1 Name>.SQL - Query executed Successfully.>> <Location of Log Files>\<Log File Name>.txt

call osql -E -i <Location of Script>\<Script2 Name>.SQL
if %errorlevel% neq 0 echo SQL Error occurred - close window to cancel or hit any key to continue - %errorlevel% 1>&2 & pause
if %errorlevel% neq 0 echo <Script2 Name>.SQL had errors that were resolved >> <Location of Log Files>\<Log File Name>.txt
if %errorlevel% equ 0 echo <Script2 Name>.SQL - Query executed Successfully.>> <Location of Log Files>\<Log File Name>.txt

|
|
\/
call osql -E -i <Location of Script>\<Script100 Name>.SQL
if %errorlevel% neq 0 echo SQL Error occurred - close window to cancel or hit any key to continue - %errorlevel% 1>&2 & pause
if %errorlevel% neq 0 echo <Script100 Name>.SQL had errors that were resolved >> <Location of Log Files>\<Log File Name>.txt
if %errorlevel% equ 0 echo <Script100 Name>.SQL - Query executed Successfully.>> <Location of Log Files>\<Log File Name>.txt

The pause allows me to check the error encountered and kill the process if necessary or continue to the next script.
Jan 9 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: pepin_labulle | last post by:
I'm a newbie with XQuery and I'm trying to execute a query with Saxon. If I don't specify the XML source file name in the query, Saxon raises the following error :...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
0
by: Chris Ashley | last post by:
How can I specify a different file name for my mailattachment? EG: newMailMessage.Attachments.Add(new MailAttachment(strLocation)); Say the file is named "243 - soc.pdf", I want to attach it...
7
by: Bernard D | last post by:
Hi, I'm using Access 97. I can run a query that finds a number of records, I then need to access these results to build a formatted text file containing all of the results. There will be 25...
2
by: Matt | last post by:
Hi All, I am currently outputting the results of a query to an Excel Workbook using the following line of code: DoCmd.OutputTo acOutputQuery, queryName, acFormatXLS, , True What this is...
10
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
2
by: Marisol2 | last post by:
I have some queries in Access 2003 db that I have setup to display as pivots. I can go into design pivot table view and click on and then click on Export to Office MicroSoft Excel. The problem is I...
3
by: zahmbonnie | last post by:
I have created two different queries and I would like take the results from one and subtract the results from the other. I can not figure out how to write the saved query name in my new query. Saved...
0
by: nilesh tayde | last post by:
how can i pass parameter to print access report by giving parameter as Report File Name,Query,PrinterName,Number of copies to be print my code is Function fPrintRemoteReport(strMDB As...
1
by: Dave Mallett | last post by:
very new to Access. Trying to export query results via macro and transfertext, but keep getting error message stating "Microsoft Jet Engine cannot find the object 'HRQ-DM_Prd1_qtr.txt'. Make sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.