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.
bpfsrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.