0

I am using SQL Server 2008, I want to generate a CSV file using a select query. I am aware of SQL CMD and BCP but below are some of my constraints with it.

  • There are a lot of parameters in my query and I have multiple queries that need to run sequentially/ and ability to rollback.
  • I am running a query from within a Workflow product which doesn't provide an option to run a command See here(enter image description here )
  • Don't have the luxury of visual studio or coding

I found an example for MySQL below, I'm looking one for a SQL Server 2008.

select * into outfile 'd:/report.csv' fields terminated by ',' from tableName;

Thanks in advance. D

Doyizode
  • 1
  • 1
  • 2
  • Does this former question help? http://stackoverflow.com/questions/6354130/is-there-a-select-into-outfile-equivalent-in-sql-server-management-studio I'm not sure what you mean by `I am running a query from within a WF product` – Paul Maxwell Mar 18 '16 at 05:30
  • Can you use a batch file? Cause without running cmd commands or using management studio there aren't many more options. – aguertin Mar 18 '16 at 05:30
  • @Used_By_Already That looks right to me - but maybe I don't understand the question. – aguertin Mar 18 '16 at 05:33
  • Oops, sorry I used an abbreviation. I meant Workflow, I have a step within the workflow product to run queries. – Doyizode Mar 18 '16 at 05:49
  • @Doyizode try my answer it is working on my side – Heemanshu Bhalla Mar 18 '16 at 05:58
  • What is the workflow product? Or Tell us that workflow is capable of, so we know what to suggest. – Paul Maxwell Mar 18 '16 at 06:11
  • The workflow product is called Laserfiche. It can run queries using a workflow activity but unfortunately there is no way to run a command. There is an activity to run script where you can write code but the whole point is to not use code and explore if CSV can be generated with just SELECT query (code is my last resort).Thanks again for taking interest guys. – Doyizode Mar 18 '16 at 06:15

2 Answers2

0

Steps To Export Query Result to Comma Separated File

  • Open SQL Server Management Studio

  • Connect to Server

  • Then Go to Tools Menu > Click on Options > Click on Query Results > Click on SQL Server > Results To Text . See in Image Below -

enter image description here

  • On Top there is Label Out format: and on top right corner you can see a combobox Select Comma delimited from combobox

  • Then click ok

  • This will output query result to .rpt file and This file has comma
    separated data . You Can open this file with Microsoft Excel

Recommendation :

  • After applying these steps close all already opened tabs and Then Open a new tab to test it works or not

  • For Testing Write a query like 'select * from tablename' then Press
    F5 to run query

  • This open opne a popup to save query result to file , give it a name and it will be save with filename.rpt to desired location . You can
    open it with excel

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
0

I found this query, it helped.

DECLARE @SQL NVARCHAR(4000)

SET @SQL = 'bcp "SELECT * FROM table" queryout "c:\FILENAME.csv" -t, -c -T'

EXEC MASTER..XP_CMDSHELL @SQL

Thanks again..

Doyizode
  • 1
  • 1
  • 2