Solved

Export OSQL Query Result to a Test File

Posted on 2014-09-30
4
195 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 50

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 50

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 50

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

705 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