Solved

osql question

Posted on 2003-10-21
26
3,686 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:africaoh
  • 13
  • 6
  • 5
  • +2
26 Comments
 
LVL 29

Expert Comment

by:leonstryker
ID: 9591436
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
 

Author Comment

by:africaoh
ID: 9591456
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9591946
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
 

Author Comment

by:africaoh
ID: 9592314
-s |    => pipe delimited not tab delimited
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 9592439
isn't a tab a char(9)?


 
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9592443
The only other thing I can think of is try -s \t  <-- without the quotes
0
 

Author Comment

by:africaoh
ID: 9592451
Allready did try that, not working... then I end up with  \  as my delimiter
Lowfatspread - Im not sure what your getting at ?
0
 
LVL 29

Expert Comment

by:leonstryker
ID: 9592680
Try just -c  without a switch.  I think tab may be a default setting
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9592766
Try this:
     osql [Other Parameters] -c "^P"

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

Hope this helps,
Dex*
0
 

Author Comment

by:africaoh
ID: 9593551
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
 
LVL 19

Expert Comment

by:Dexstar
ID: 9594031
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
 
LVL 19

Accepted Solution

by:
Dexstar earned 105 total points
ID: 9594068
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
 

Author Comment

by:africaoh
ID: 9598592
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 19

Expert Comment

by:Dexstar
ID: 9600179
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600215
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600217
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600220
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600225
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600236
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600243
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9600254
sorry - every one for the multiple posts, something wrong with our servers
0
 
LVL 19

Assisted Solution

by:Dexstar
Dexstar earned 105 total points
ID: 9600329
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
 
LVL 29

Assisted Solution

by:leonstryker
leonstryker earned 20 total points
ID: 9600361
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
 

Author Comment

by:africaoh
ID: 9600371
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
 

Expert Comment

by:H4ri
ID: 9611815
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
 
LVL 29

Expert Comment

by:leonstryker
ID: 9612184
H4ri,

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now