bpfsr
asked on
Export MS SQL Server results to CSV File
I just accepted a solution for the following problem:
https://www.experts-exchange.com/questions/23895455/Help-building-database-in-MS-SQL-Server.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.
https://www.experts-exchange.com/questions/23895455/Help-building-database-in-MS-SQL-Server.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.
You need to use the BCP command to export the query results to a CSV file.
ASKER
can you be more specific please
ASKER
i'm sorry, I'm looking for the actual query
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
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
ASKER
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.
ASKER
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]))+ltri m(rtrim([C ONDITION]) )) 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
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]))+ltri
)
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
ASKER
Hi, is anybody going to add to this string? If not I have to delete the question and ask again...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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]))+ltri m(rtrim([C ONDITION]) )) 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'.
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]))+ltri
)
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'.
SELECT *
into ISBNEXPORT
FROM @ISBN
ORDER BY 1
into ISBNEXPORT
FROM @ISBN
ORDER BY 1