Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Output file

 
   Database Help (Home) -> MSEQ RSS
Next:  REMOTE CONNECTION FOR MSDE  
Author Message
Monica

External


Since: Jul 08, 2008
Posts: 4



(Msg. 1) Posted: Wed Jul 30, 2008 3:13 pm
Post subject: Output file
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

I have an osql statement that queries a database.

If the query returns any rows I want them written to a file, so I redirected
the output of my osql statement to a file (results.txt). But if there are no
rows that comply with the condition of the query I do not want any files
created. The way I have it now an empty file is created.

This is my osql statement:
osql -U sa -S myserver -d db_Where -n -i c:\SQLquery.txt -o c:\results.txt

Is there a way I can write this so that no output file is created when no
rows comply with the query condition?

I was thinking of checking the file size and if it is zero then delete the
file, but I do not know how to do that either from a .bat file. Can it be
done?

 >> Stay informed about: Output file 
Back to top
Login to vote
Jonathan Psaila-Depasqual

External


Since: Jul 31, 2008
Posts: 2



(Msg. 2) Posted: Thu Jul 31, 2008 5:23 pm
Post subject: Re: Output file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Monica,

I've taken the course of action you suggested in the last paragraph and
wrote you a small dos script that carries this out for you. Note that you
should append the osql command to the beginning of this script. The script
assumes that you send your data to a text file called tempresults.txt. You
use that name to check for file size. If the file size = 0 bytes you delete
the file. If the file size > 0 bytes you rename the file to results.txt.
Note that you have to run the script from the directory in which the file
tempresults.txt resides. Otherwise please adapt the script to include the
necessary directory paths.

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

echo off

if not exist tempresults.txt GOTO EXITTO

set "filename=tempresults.txt"

for %%A in (%filename%) do set "filesize=%%~zA"

if "%filesize%"=="0" GOTO ZEROFILESIZE
rename tempresults.txt results.txt
echo File was greater than 0 bytes
GOTO EXITTO

:ZEROFILESIZE
del tempresults.txt
echo File was 0 bytes

:EXITTO

echo on

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

Any questions please let me know.

Regards,
Jonathan



"Monica" wrote in message

>I have an osql statement that queries a database.
>
> If the query returns any rows I want them written to a file, so I
> redirected
> the output of my osql statement to a file (results.txt). But if there are
> no
> rows that comply with the condition of the query I do not want any files
> created. The way I have it now an empty file is created.
>
> This is my osql statement:
> osql -U sa -S myserver -d db_Where -n -i c:\SQLquery.txt -o c:\results.txt
>
> Is there a way I can write this so that no output file is created when no
> rows comply with the query condition?
>
> I was thinking of checking the file size and if it is zero then delete the
> file, but I do not know how to do that either from a .bat file. Can it be
> done?

 >> Stay informed about: Output file 
Back to top
Login to vote
Monica

External


Since: Jul 08, 2008
Posts: 4



(Msg. 3) Posted: Thu Jul 31, 2008 5:23 pm
Post subject: Re: Output file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thanks Jonathan,

This worked OK. Though I do not understand what the command for %%A in
(%filename%) do set "filesize=%%~zA" does. Could you explain?

Regards,

Monica

"Jonathan Psaila-Depasquale" wrote:

> Hi Monica,
>
> I've taken the course of action you suggested in the last paragraph and
> wrote you a small dos script that carries this out for you. Note that you
> should append the osql command to the beginning of this script. The script
> assumes that you send your data to a text file called tempresults.txt. You
> use that name to check for file size. If the file size = 0 bytes you delete
> the file. If the file size > 0 bytes you rename the file to results.txt.
> Note that you have to run the script from the directory in which the file
> tempresults.txt resides. Otherwise please adapt the script to include the
> necessary directory paths.
>
> --------------------
>
> echo off
>
> if not exist tempresults.txt GOTO EXITTO
>
> set "filename=tempresults.txt"
>
> for %%A in (%filename%) do set "filesize=%%~zA"
>
> if "%filesize%"=="0" GOTO ZEROFILESIZE
> rename tempresults.txt results.txt
> echo File was greater than 0 bytes
> GOTO EXITTO
>
> :ZEROFILESIZE
> del tempresults.txt
> echo File was 0 bytes
>
> :EXITTO
>
> echo on
>
> --------------------
>
> Any questions please let me know.
>
> Regards,
> Jonathan
>
>
>
> "Monica" wrote in message
>
> >I have an osql statement that queries a database.
> >
> > If the query returns any rows I want them written to a file, so I
> > redirected
> > the output of my osql statement to a file (results.txt). But if there are
> > no
> > rows that comply with the condition of the query I do not want any files
> > created. The way I have it now an empty file is created.
> >
> > This is my osql statement:
> > osql -U sa -S myserver -d db_Where -n -i c:\SQLquery.txt -o c:\results.txt
> >
> > Is there a way I can write this so that no output file is created when no
> > rows comply with the query condition?
> >
> > I was thinking of checking the file size and if it is zero then delete the
> > file, but I do not know how to do that either from a .bat file. Can it be
> > done?
>
>
>
>
 >> Stay informed about: Output file 
Back to top
Login to vote
Jonathan Psaila-Depasqual

External


Since: Jul 31, 2008
Posts: 2



(Msg. 4) Posted: Thu Jul 31, 2008 7:42 pm
Post subject: Re: Output file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi Monica,

Basically this line could be translated into the following pseudocode:

For every file in the array represented by the variable filename set the
variable filesize to the size of the file.

Two things to note here:
1. Even though you only entered one filename, the script is treating the
variable as an array of filenames, hence the "for every file do a command"
syntax.
2. %%~zA returns the size of the file.

I hope this explains it better.

Regards,
Jonathan



"Monica" wrote in message

> Thanks Jonathan,
>
> This worked OK. Though I do not understand what the command for %%A in
> (%filename%) do set "filesize=%%~zA" does. Could you explain?
>
> Regards,
>
> Monica
>
> "Jonathan Psaila-Depasquale" wrote:
>
>> Hi Monica,
>>
>> I've taken the course of action you suggested in the last paragraph and
>> wrote you a small dos script that carries this out for you. Note that
>> you
>> should append the osql command to the beginning of this script. The
>> script
>> assumes that you send your data to a text file called tempresults.txt.
>> You
>> use that name to check for file size. If the file size = 0 bytes you
>> delete
>> the file. If the file size > 0 bytes you rename the file to results.txt.
>> Note that you have to run the script from the directory in which the file
>> tempresults.txt resides. Otherwise please adapt the script to include
>> the
>> necessary directory paths.
>>
>> --------------------
>>
>> echo off
>>
>> if not exist tempresults.txt GOTO EXITTO
>>
>> set "filename=tempresults.txt"
>>
>> for %%A in (%filename%) do set "filesize=%%~zA"
>>
>> if "%filesize%"=="0" GOTO ZEROFILESIZE
>> rename tempresults.txt results.txt
>> echo File was greater than 0 bytes
>> GOTO EXITTO
>>
>> :ZEROFILESIZE
>> del tempresults.txt
>> echo File was 0 bytes
>>
>> :EXITTO
>>
>> echo on
>>
>> --------------------
>>
>> Any questions please let me know.
>>
>> Regards,
>> Jonathan
>>
>>
>>
>> "Monica" wrote in message
>>
>> >I have an osql statement that queries a database.
>> >
>> > If the query returns any rows I want them written to a file, so I
>> > redirected
>> > the output of my osql statement to a file (results.txt). But if there
>> > are
>> > no
>> > rows that comply with the condition of the query I do not want any
>> > files
>> > created. The way I have it now an empty file is created.
>> >
>> > This is my osql statement:
>> > osql -U sa -S myserver -d db_Where -n -i c:\SQLquery.txt -o
>> > c:\results.txt
>> >
>> > Is there a way I can write this so that no output file is created when
>> > no
>> > rows comply with the query condition?
>> >
>> > I was thinking of checking the file size and if it is zero then delete
>> > the
>> > file, but I do not know how to do that either from a .bat file. Can it
>> > be
>> > done?
>>
>>
>>
>>
 >> Stay informed about: Output file 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Determining which tables are in which file group via T-SQL - I'm trying to figure out how to list what file group every table is in. I thought this query was it : select sys.tables.name,sys.data_spaces.name from sys.tables,sys.data_spaces where sys.tables.lob_data_space_id = sys.data_spaces.data_space_id order...

index on a view - Hello, I have a table (TAB) and A View with alias (VIEW) Table cod varchar 3 descr carchar 60 my view cod alias COd1 descr alias DES Now i need a index on view with key COD1 I can't create it. Can..

Creating an alias - I have an alias that can get very long because it maintains a information of where it came from. Is there a way to alias an alias? My first thought was: Declare @T1 varchar(128) Set T1 = 'MyAlias' .... Inner Join Table1 as @T1 This of course does no...

Formatting numeric fields in select-clause - This is propably a very simple question, but I can“t seem to find the answer to it in the documentation. I want to format a numeric field so the result is right justified and zero-filled. ex select 1 will give the result 01 How do I manage this..

Simple Query problem - Sample table as follows Order ID Stock Code Status --------- ------- ------- 203 STK1 3 203 STK2 2 203 STK4 3 204 STK1 3 204 STK5 3 205 ..
   Database Help (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]