• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5094
  • Last Modified:

Export MS SQL Server results to CSV File

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
bpfsr
Asked:
bpfsr
  • 6
  • 4
1 Solution
 
lundnakCommented:
You need to use the BCP command to export the query results to a CSV file.
0
 
bpfsrAuthor Commented:
can you be more specific please
0
 
lundnakCommented:
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
bpfsrAuthor Commented:
i'm sorry, I'm looking for the actual query
0
 
Louis01Commented:
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
 
bpfsrAuthor Commented:
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
 
bpfsrAuthor Commented:
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
 
bpfsrAuthor Commented:
Hi, is anybody going to add to this string? If not I have to delete the question and ask again...
0
 
lundnakCommented:
--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
 
bpfsrAuthor Commented:
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
 
lundnakCommented:
SELECT *
into ISBNEXPORT
FROM @ISBN
ORDER BY 1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now