?
Solved

Export MS SQL Server results to CSV File

Posted on 2008-11-11
11
Medium Priority
?
5,075 Views
Last Modified: 2012-05-05
I just accepted a solution for the following problem:

http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Q_23895455.html

However, I forgot to ask to have the results of the query exported to a CSV file. (That would be all three columns - right now if I open Excel and do a Data -> Import External Data it only imports the original ISBN column, I need the whole thing in the CSV), thank you.
0
Comment
Question by:bpfsr
  • 6
  • 4
11 Comments
 
LVL 7

Expert Comment

by:lundnak
ID: 22936343
You need to use the BCP command to export the query results to a CSV file.
0
 

Author Comment

by:bpfsr
ID: 22936376
can you be more specific please
0
 
LVL 7

Expert Comment

by:lundnak
ID: 22936420
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:bpfsr
ID: 22936445
i'm sorry, I'm looking for the actual query
0
 
LVL 11

Expert Comment

by:Louis01
ID: 22937375
There is no BULK EXPORT TO command like the BULK INSERT FROM used in your reference.
The bcp utility (solution proposed by ludnak) or using SSIS (http://msdn.microsoft.com/en-us/library/ms140052(SQL.90).aspx) is the popular SQL Server solutions.

You could also use a class like the one used  in http://www.codeproject.com/KB/database/Cs_CSV_import_export.aspx
0
 

Author Comment

by:bpfsr
ID: 22940454
Okay, I don't know if I am wording this wrong or what but let me try again. I have no problem with using the bcp utility, what I am looking for is the query that would produce the result I am looking for, be it with the bcp or any other method. In reading the link provided above for utilizing the bcp utility it says it can be used to export data from a table to a data file. However, if you refer to the link I placed in the question you'll see the results of that query do not actually write to the table. I want the results either written to the table or direct to a csv file, either using the bcp utility or any other method.
0
 

Author Comment

by:bpfsr
ID: 22940993
Here is what I am using, I am getting an error:

Msg 102, Level 15, State 1, Line 39
Incorrect syntax near 'bcp'.

Here is the query (please note the import file is a simple list of ISBN numbers. I have attached one here as an Excel file since I can't import CSV to EE. You can just save and convert it if you want to use it for testing)

CREATE TABLE ISBNIMPORT
(ISBN VARCHAR(200))
 
BULK INSERT  ISBNIMPORT
FROM 'C:\Documents and Settings\My Documents\ISBN HDC Test 20.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
 
DECLARE  @ISBN  TABLE(
      [ISBN] [nvarchar](50) NOT NULL,
      [Condition] [nvarchar](50) NULL,
      [SKU]  AS (ltrim(rtrim([ISBN]))+ltrim(rtrim([CONDITION]))) PERSISTED
)  
DECLARE @TABLECONDITION TABLE (
      [Condition] [nvarchar](50)
)
 
INSERT INTO @TABLECONDITION
SELECT  'New'  
UNION ALL
SELECT 'Used Like New'  
UNION ALL
SELECT 'Used Very Good'  
UNION ALL
SELECT 'Used Good'  
UNION ALL
SELECT  'Used Acceptable'  
 
------------------------------------
INSERT INTO @ISBN
SELECT ISBNIMPORT.ISBN, T.* FROM ISBNIMPORT
CROSS JOIN @TABLECONDITION  T
-----------------------------------
SELECT * FROM @ISBN
ORDER BY 1
bcp ISBNimport out "TestCSV.CSV"


ISBN-HDC-Test-20.xls
0
 

Author Comment

by:bpfsr
ID: 22946049
Hi, is anybody going to add to this string? If not I have to delete the question and ask again...
0
 
LVL 7

Accepted Solution

by:
lundnak earned 2000 total points
ID: 22946103
--Insert the records into a global temporary table
select *
into ISBNEXPORT
from ORDER BY 1

--I stole the following code from the URL below
--http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
declare @sql varchar(8000)

exec xp_cmshell "bcp dbname.dbo.SBNimport out c:\TestCSV.CSV "
select @sql = 'bcp dbname.dbo.SBNimport out
c:\TestCSV.CSV -c t, -T -S' + @@servername

exec master..xp_cmdshell @sql
0
 

Author Comment

by:bpfsr
ID: 22946355
I'm sorry - I've tried that a few different way and get errors every time:

CREATE TABLE ISBNIMPORT
(ISBN VARCHAR(200))
 
BULK INSERT  ISBNIMPORT
FROM 'C:\Documents and Settings\My Documents\ISBN HDC Test 20.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
 
DECLARE  @ISBN  TABLE(
      [ISBN] [nvarchar](50) NOT NULL,
      [Condition] [nvarchar](50) NULL,
      [SKU]  AS (ltrim(rtrim([ISBN]))+ltrim(rtrim([CONDITION]))) PERSISTED
)  
DECLARE @TABLECONDITION TABLE (
      [Condition] [nvarchar](50)
)
 
INSERT INTO @TABLECONDITION
SELECT  'New'  
UNION ALL
SELECT 'Used Like New'  
UNION ALL
SELECT 'Used Very Good'  
UNION ALL
SELECT 'Used Good'  
UNION ALL
SELECT  'Used Acceptable'  
 
------------------------------------
INSERT INTO @ISBN
SELECT ISBNIMPORT.ISBN, T.* FROM ISBNIMPORT
CROSS JOIN @TABLECONDITION  T
-----------------------------------
SELECT * FROM @ISBN
ORDER BY 1
into ISBNEXPORT


Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'into'.

select *
into ISBNEXPORT
from ORDER BY 1

Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'ORDER'.
0
 
LVL 7

Expert Comment

by:lundnak
ID: 22946382
SELECT *
into ISBNEXPORT
FROM @ISBN
ORDER BY 1
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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Viewers will learn how the fundamental information of how to create a table.
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.
Suggested Courses

864 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