Solved

Export OSQL Query Result to a Test File

Posted on 2014-09-30
4
163 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
Comment Utility
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 45

Expert Comment

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

Open in new window

0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
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

What Security Threats Are You Missing?

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 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

11 Experts available now in Live!

Get 1:1 Help Now