Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

osql question

Posted on 2003-10-21
26
Medium Priority
?
3,795 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 420 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
 
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 420 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 80 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

877 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