Solved

Export OSQL Query Result to a Test File

Posted on 2014-09-30
4
170 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
  • 3
4 Comments
 
LVL 46

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 46

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 46

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

911 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

23 Experts available now in Live!

Get 1:1 Help Now