|
|
Question : Help with exporting data using BCP
|
|
Hi, I am trying to use BCP to export data from a table into multiple CSV files based on the telephone number. I run the query in QA and what I get back is:
usage: BCP {dbtable | query} {in | out | queryout | format} datafile [-m maxerrors] [-f formatfile] [-e errfile] [-F firstrow] [-L lastrow] [-b batchsize] [-n native type] [-c character type] [-w wide character type] [-N keep non-text native] [-V file format version] [-q quoted identifier] [-C code page specifier] [-t field terminator] [-r row terminator] [-i inputfile] [-o outfile] [-a packetsize] [-S server name] [-U username] [-P password] [-T trusted connection] [-v version] [-R regional enable] [-k keep null values] [-E keep identity values] [-h "load hints"] NULL
In the grid view and (12 row(s) affected) x 212 in the messages view. There are 212 distinct telephone numbers so the loop is working correctly if I use PRINT i get
BCP "select Call_Orig from Abrient..[0800] where Call_Orig='08009705582' queryout "Number_08009705582.CSV" -c -S ***.***.*.** -U **** -P **********
And that all looks ok, but it's not producing any files, I have added myself to sysadmin, I have also ran directly on the server machine, I have also tried several variations of the code below but with no success. ****************************************************************************************** DECLARE @CallOrigID VARCHAR(11) DECLARE @BCPString VARCHAR(8000)
DECLARE cCallOrig CURSOR FOR SELECT DISTINCT Call_Orig FROM [0800]
OPEN cCallOrig
FETCH NEXT FROM cCallOrig INTO @CallOrigID
WHILE @@FETCH_STATUS = 0 BEGIN SET @BCPString = 'BCP "' SET @BCPString = @BCPString + 'select Call_Orig from Abrient..[0800] where Call_Orig=''' + @CallOrigID + '''' SET @BCPString = @BCPString + ' queryout ' SET @BCPString = @BCPString + '"C:\OutputData\Number_' + @CallOrigID + '.CSV" ' SET @BCPString = @BCPString + ' -c -S ***.***.**.*** -U **** -P *********'
exec master.dbo.xp_cmdshell @BCPString
FETCH NEXT FROM cCallOrig INTO @CallOrigID END
CLOSE cCallOrig DEALLOCATE cCallOrig ******************************************************************
If anybody could help me with this that would be great.
Cheers Peter
|
Answer : Help with exporting data using BCP
|
|
You are not closing the quotation mark at the end of the query
this is the line (with the closing mark):
SET @BCPString = @BCPString + 'select Call_Orig from Abrient..[0800] where Call_Orig=''' + @CallOrigID + '''"'
|
|
|
|
|