osql question

Hey everyone,

Simple question here...
How do I specify my delimiter with osql command (-s) if I would like to create a "tab-delimited" text file?
I tried  -s "\t"  didnt seem to work.

Please let me know.
africaohAsked:
Who is Participating?
 
DexstarConnect With a Mentor Commented:
Actually, you don't even need that variable.  You can do it like this:

EXEC master..xp_cmdshell 'osql -SMyServer -Umyuser -Pmypwd -Q"select * from products" -dNorthwind  -w"1000" -oc:\MyText.txt -s"      "'

(Note the character between the quotes the follow the -s is just a tab)

Enjoy,
Dex*
0
 
leonstrykerCommented:
How to write values to a Text file

To write a result of a SELECT query in a text file :-

master..xp_cmdshell 'osql -SMyServer -Umyuser -Pmypwd
-Q"select * from products" -dNorthwind  -w"1000" -oc:\MyText.txt'

If you are running it from the prompt take out master..xp_cmdshell ' , and the final '  parts.

0
 
africaohAuthor Commented:
I know that, I just need to specify the correct delimiter for my text file that is the  -s  argument, do you know what to set -s to in order to generate tab delimited text file ?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
leonstrykerCommented:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/coprompt/cp_osql_1wxl.asp

-s col_separator

Specifies the column-separator character, which is a blank space by default. To use characters that have special meaning to the operating system (for example, | ; & < >), enclose the character in double quotation marks (").

Based on this I would say:     -s "|"
0
 
africaohAuthor Commented:
-s |    => pipe delimited not tab delimited
0
 
LowfatspreadCommented:
isn't a tab a char(9)?


 
0
 
leonstrykerCommented:
The only other thing I can think of is try -s \t  <-- without the quotes
0
 
africaohAuthor Commented:
Allready did try that, not working... then I end up with  \  as my delimiter
Lowfatspread - Im not sure what your getting at ?
0
 
leonstrykerCommented:
Try just -c  without a switch.  I think tab may be a default setting
0
 
DexstarCommented:
Try this:
     osql [Other Parameters] -c "^P"

Enter the ^P by hitting CTRL+P when typing the command line.

Hope this helps,
Dex*
0
 
africaohAuthor Commented:
The deafult is a space.

"^P" that is only from command line, I need to do this from my sp.

Any other ideas guys ?
0
 
DexstarCommented:
It seems to work if you build your command line dynamically to include the Tab char explicitly, and then execute it like this:

DECLARE @strCommand VARCHAR(1000)

SET @strCommand =  'osql -SMyServer -Umyuser -Pmypwd -Q"select * from products" -dNorthwind  -w"1000" -oc:\MyText.txt -s"' + CHAR(9) + '"'

EXEC master..xp_cmdshell @strCommand

Hope this helps,
Dex*
0
 
africaohAuthor Commented:
Hi Dexstar,

Your solution works fine, only one more question, when I import this tab-delimted textfile into excel, the fields have alot of blank spaces (equal to the total field lenght) appended to it, any way one can get rid of the trailing spaces?
0
 
DexstarCommented:
africaoh,

I don't see a way to prevent osql from adding those trailing spaces.  So, I think the best solution is to strip them off in Excel, after you've imported.  This could be done with a simple macro that TRIMs each cell.  I don't know your proficiency level with Excel, so maybe you can handle that on your own.  If not, I'll try to help you.

Hope my suggestion helps,
Dex*
0
 
leonstrykerCommented:
africaoh

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
africaoh

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
africaoh

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
africaoh,

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
africaoh,

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
africaoh,

If you are importing the results into Excel anyway, why not use comma as your deliminator and save the file as a "MyText.csv"  Excel will automatically parse the result for you.
0
 
leonstrykerCommented:
sorry - every one for the multiple posts, something wrong with our servers
0
 
DexstarConnect With a Mentor Commented:
Commas are a bad idea unless you are 100% sure that you will never, never need to have a comma in your database fields.  And since you can't predict the future, its better to play it safe.

You know, for all this trouble, you could write a simple VBscript to execute the query and write the fields into a text file EXACTLY the way that you want them.

Dex*
0
 
leonstrykerConnect With a Mentor Commented:
Anything is a bad idea if your deliminator is also used in your data.  Better yet go to Excel and use either VBA or SQLRequest function to pull your data into the spreadsheet.

Leon
0
 
africaohAuthor Commented:
Thanks for all the ideas guys, really appreciate all your efforts. Will assign the credits now and try and sort it out based on your thoughts. Ragards.
0
 
H4riCommented:
Hallo i'm new member in this mailing list and i need hellp u,

what must i do to prevent on the SQL server 2000 ?

My condition Server is,
==================================================================
SQL Server Enterprise Manager

A Coonection could not be established to ServerX

Reason: General network error, Check your network documentation.
CoonectionOpen (PreloginHandshake())..

Please verify SQL Server is running and check your SQL Server
registration properties (by right-cliking on the ServerX node) and
try again.
==================================================================

My condition on Client,
==================================================================
SQL server stopped and can't akses again for client

- For client ping to Server SQL server it's oke
- From server not responding for login on SQL server Ent. Manager --
> SQL server Group.
- show,the Test on ODBC diclient ,

Connection failed:
SQLState:'01000'
SQL Server Error: 10060
[Microsoft}[ODBC SQL Server Driver][DBNETLIB]ConnectionOpen
(PreLoginHandshake()).
Connection failed:
SQlState:'08001'
SQL Server Error: 11
[Microsoft}[ODBC SQL Server Driver][DBNETLIB]General Network error.
Check your network documentation.
====================================================================
- Sometimes 2 on hours.
- To become normal windows server,ussualy must be restart the Server.

PS : data server,SQL Server 2000 Standard dengan Windows Server 2000
Sp 4

Please Help Me

Regards
Hp

0
 
leonstrykerCommented:
H4ri,

This question is closed.  If you want to ask a question please open a new one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.