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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
DexstarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
DexstarCommented:
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
leonstrykerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.