Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TSQL To export to a CSV file

Posted on 2006-06-27
4
Medium Priority
?
2,195 Views
Last Modified: 2011-10-08
Hi

I need to import data from a CSV in to a table, alter the table, add some info and then export it back into a CSV again.  I can do all up to exporting it back into a CSV useing the Script below:-

Create Table EANUPLOAD
(EAN Varchar (20),
COLOUR varchar (8),
SIZE Varchar(8),
SPITNO Varchar (20))

go

Bulk insert BrassoLat.dbo.[EANUPLOAD]
From '\\BANKFUTURA\Futura\UploadFiles\EANUPLOAD.csv'
WITH
      (
         FIELDTERMINATOR = ','
      )

ALTER TABLE EANUPLOAD ADD FuturaBC int
GO

update EANUPLOAD

set EANUPLOAD.FuturaBC =  FuturERS.dbo.ARTIKEL.ART_REFNUMMER

FROM  dbo.EANUPLOAD INNER JOIN  FuturERS.dbo.ARTIKEL ON
dbo.EANUPLOAD.COLOUR = FuturERS.dbo.ARTIKEL.ART_EIGENSCHAFT
AND dbo.EANUPLOAD.[SIZE]= FuturERS.dbo.ARTIKEL.ART_EINHEIT
AND dbo.EANUPLOAD.SPITNO = FuturERS.dbo.ARTIKEL.ART_LFID_NUMMER

Go

Can any one help

Cheers

Si
0
Comment
Question by:brasso_42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 16997412
To export to CSV in T-SQL its probably easiest to use something like this:

EXEC master.dbo.xp_cmdshell 'BCP BrassoLat.dbo.EANUPLOAD OUT D:\EANUPLOAD.csv -Slocalhost -T'


which uses the xp_cmdshell (elevated rights required) stored procedure to run BCP.

BCP is a command line tool used tom import and export data.


The BCP command line may need a little work, your best bet is to open a command prompt and experiment with it and get it working, then substitute it into the xp_cmdshell statement.
0
 
LVL 1

Author Comment

by:brasso_42
ID: 16999474
Hi When I run this it comes up with

        output
1      NULL
2      Enter the file storage type of field EAN [char]:

Any Ideas??


Cheers
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 2000 total points
ID: 16999583
Forgot a parameter


EXEC master.dbo.xp_cmdshell 'BCP BrassoLat.dbo.EANUPLOAD OUT D:\EANUPLOAD.csv -Slocalhost -T -N'


-N means use native data types - don't ask for a format file.

0
 
LVL 1

Author Comment

by:brasso_42
ID: 17000092
Hi

Geat That worked a treat .... well needed a bit of tweeking. Had to use FQDN and change the outupt type so it opened nicely in excel.  The script I used in the end was


EXEC master.dbo.xp_cmdshell 'BCP BrassoLat.dbo.EANUPLOAD OUT \\BANKFUTURA\Futura\UploadFiles\EANCHECK.csv -Slocalhost -T -w'


Thanks for your help


Si
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

721 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