First, create the csv file and prepare the column header:
master..xp_cmdshell 'echo EmpID,LastName,FirstName > C:\temp\Employees.csv'It creates a file c:\temp\Employees.csv with 3 columns.
Then, append the data using OPENROWSET:
INSERT INTO OPENROWSET('Microsoft.Jet.OleDB.4.0',Open the file c:\temp\Employees.csv, you'll see there are 9 rows appended.
'Text;Database=C:\temp',
[Employees#csv])
select EmployeeID, LastName, FirstName from NOrthwind..Employees
(9 row(s) affected)
Some points here are: 1) Database=C:\temp (need to put the folder here only), and 2) [Employees#csv] (need to replace . with # as the table name.
OK, it's similar to read the file back:
select * from OPENROWSET('Microsoft.Jet.OleDB.4.0',At the end, I'll show another way to read the data in csv file:
'Text;Database=C:\temp', [Employees#csv])
1 Davolio Nancy
...
9 Dodsworth Anne
select * from OpenRowset('MSDASQL',It seems you cannot append to csv using MSDASQL.
'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=C:\temp;',
'select top 3 * from "Employees.csv"')
No comments:
Post a Comment