Solved

Export OSQL Query Result to a Test File

Posted on 2014-09-30
4
189 Views
Last Modified: 2014-09-30
I have the following OSQL command which I need to run.  

My quesiton is how do I export this result into a CSV file?

Use clintrial
SELECT     [PatientID], [PackNo]
FROM       Subject_Treatment_Visits INNER JOIN Trials ON
Subject_Treatment_Visits.TrialID = Trials.TrialID
Where protocoldesc = '00001'
Go
0
Comment
Question by:DHPBilcare
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40352385
Use Export Data Wizard.
In SSMS right-click on database name, Task, Export Data...
Follow the wizard and in place of table name just paste the query.
0
 

Author Comment

by:DHPBilcare
ID: 40352465
We have a validated system that is using using Microsoft SQL Server Desktop Engine.

Therefroe I need to run the query from the command promt using osql.

I dont have any frontend user interface utility I can use on this server.

Thus I need to add the export command to the query in my first post.
0
 
LVL 49

Expert Comment

by:Vitor Montalvão
ID: 40352488
Then use SQLCMD instead of OSQL (old stuff).
sqlcmd -S ServerName -E -i FileName -o OutputFile

Open in new window

0
 
LVL 49

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40352498
If you need to configure comma as column separator:
sqlcmd -S ServerName -E -i FileName -o OutputFile -s","

Open in new window

NOTE: FileName is the file with your query but you can substitute it with your full query with -Q option:
sqlcmd -S ServerName -E -Q "SELECT  [PatientID], [PackNo] FROM clintrial.dbo.Subject_Treatment_Visits INNER JOIN clintrial.dbo.Trials ON  Subject_Treatment_Visits.TrialID = Trials.TrialID Where protocoldesc = '00001'" -o OutputFile -s","

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help needed in sql query 4 28
SQL eating up memory? 16 40
sql query 5 44
SQL Get Store Procedure Column Name As Row 5 41
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

749 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