Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2006-08-15 : 09:56:09
|
you can specify quoted fields( only char fields) in BCP?Basically, I have to generate a text file looks like:"Detail","SSS-A01 ","1110","A01","06-26","06/30/2006","N",265727,0,"Revenue" |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-15 : 10:26:36
|
Yes, if you use a format file.Peter LarssonHelsingborg, Sweden |
 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2006-08-15 : 10:30:42
|
thanks... can you show me how to create a format file? or |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-08-15 : 11:15:48
|
seehttp://www.nigelrivett.net/SQLTsql/BCP_quoted_CSV_Format_file.htmlFor your requirement it woul dbe easier to create the structure ion an SP and bcp it using queryout.create s_FormatSPasselect '"DETAIL"'+ ',"' + coalesce(col1,'') + '"'+ ',"' + coalesce(convert(varchar(20),col2),'') + '"'+ ....from tblgoexec master..xp_cmdshell 'bcp "exec mydb..s_FormatSP" queryout c:\myfile.txt -c'==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2006-08-15 : 11:26:54
|
thank you |
 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2006-08-15 : 12:00:36
|
nr, what is the reson to use coalesce in your solution? can you explain to me? |
 |
|
KenW
Constraint Violating Yak Guru
391 Posts |
Posted - 2006-08-15 : 13:24:15
|
Coalesce() is to handle NULL values. The way Nigel used it is to use the field contents if the field is not null, or an empty string if it is, surrounded by double quotes.Ken |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-08-15 : 15:45:40
|
If one of the values s null then as we are concatenating strings it would end up with a null string which would be bcp'd out as a blank line.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2006-08-15 : 16:12:57
|
thansk,nr here is the code(I've modified your code little bit to generate 20000 records each based on the unit_abbr ) to generate the text files,but it looks like it runs forever.. #a table has only about 80000 records..not sure why it takes forever... I checked the CPU time and it's a big number 2450141. I have an index on unit_abbr column in the GL_Export table.Do you have any ideas why this is taking such a long time even though there is only 80000 records to process?--Here is the codeselect * from #aselect unit_abbr, num = count(*)into #afrom GL_Exportgroup by unit_abbrorder by unit_abbrdeclare @sql varchar(8000)declare @unit varchar(10), @lastunit varchar(10), @num int, @nextunit varchar(10)select @lastunit = ''while @lastunit < (select max(unit_abbr) from #a)beginselect @unit = min(unit_abbr) from #a where unit_abbr > @lastunitselect @num = num from #a where unit_abbr = @unitselect @nextunit = @unit, @lastunit = @unitwhile @num < 20000beginselect @lastunit = @nextunitselect @nextunit = unit_abbr, @num = @num + num from (select top 1 * from #a where unit_abbr > @nextunit order by unit_abbr) aendendbeginselect @sql = 'bcp "select ''Batch'','''',''IC'',''PR'',''N''union allselect ''\"''+DETAIL+''\"'''+ ', ''\"''+coalesce(ltrim(rtrim(unit_abbr)),'''') + ''\"'''+ ', ''\"''+coalesce(ltrim(rtrim(transaction_gl_code)),'''') + ''\"'''+ ', coalesce(ltrim(rtrim(replace( convert(numeric(18,2),debit ), ''.00'', ''''))),'''')'+ ', coalesce(ltrim(rtrim(replace( convert(numeric(18,2),credit ), ''.00'', ''''))),'''')'+ ' from GL_Export unit between ''' + @unit + ''' and ''' + @lastunit + '''" queryout c:\GL_Export\' + @unit + '.txt -c -S TESTBOX -Usa -P sasa '--select @sqlexec master..xp_cmdshell @sqlend |
 |
|
|
|
|
|
|