1

I have looked at this question which is essentially the same but the OP accepted 'using other tools' as an answer. Basically, I am trying to export data from a table to a delimited file but because of the 8192 character limit, the output text is getting truncated. I could not find any good approaches that work (for me at least).

If SSMS and sqlcmd (not sure if the limitation applies to this as well. I am trying at the moment) are my only options, is there a way to workaround this limitation and dump text columns of higher widths?

Community
  • 1
  • 1
Legend
  • 113,822
  • 119
  • 272
  • 400

1 Answers1

2

Have you considered using a simple PowerShell script or a command-line app using C#? If it can SELECT all of the data then it should certainly be able to write it all to a file.

EDIT 2011-08-03

I was unable to get this working with PowerShell or sqlcmd. The former seems to truncate output at 4000 characters and the latter at 256. However my good old friend VBScript once again proved that new and shiny is not always the best... the code is much more tedious to write, but it works. Save as .vbs file, make the appropriate changes to lines 2, 3 and 6 (and 9 if you want more columns, transformation of the data, different delimiters, etc).

set conn = createobject("ADODB.Connection")
conn.open "provider=SQLOLEDB.1;data source=.;user id=user;password=pass;"
set rs = conn.execute("SELECT VarcharMaxCol FROM dbo.TableName")

set fso = createobject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("c:\path\from_vbs.csv", true)

do while not rs.eof
    f.writeline vbCrLf & rs(0)
    rs.movenext
loop

rs.close   : set rs   = nothing
conn.close : set conn = nothing
f.close    : set f    = nothing
set fso  = nothing

I will post back here if I get any answers from my PowerShell guru friends about how they deal with > 4K.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • +1 Thank you. I accepted this as the solution but could you please give me an example on what you mean by using a PowerShell script? The whole table is very big so what I am doing is creating some numbers in a `#TEMP` table and joining them to the original table to get the result set. – Legend Aug 02 '11 at 18:58
  • @Legend I have updated the answer with a solution that works in VBScript (I know, *shudder*). – Aaron Bertrand Aug 03 '11 at 20:29