snippetsqlMinor
How can I output more than 256 characters to a file?
Viewed 0 times
canfilethanmore256outputhowcharacters
Problem
I am using the following command to write the output from a stored procedure to a file for further processing:
The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the
The file consists of a header row followed by the data rows and should look something like this:
With the
With
```
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems"
DECLARE @shellCommand VARCHAR(2000)
SET @shellCommand = 'SQLCMD -S ' + @@SERVERNAME + ' -d ' + @dbName + ' -U ' + @UserName + ' -P ' + @Password + ' -Q "' + @sqlCommand + '" -s "," -h -1 -k 1 -W -o "' + @outputfileName + '"'@sqlCommand is set correctly, to "EXEC StoredProcedureName parameterValue" and when run independently apparently produces the correct data.The problem I'm having is that one of the fields contains text greater than 256 characters long and is getting truncated when fed through the SQLCMD command.
On reading the documentation I see that the default column width is 256 characters. So I'm looking at using the
-y or -Y option to allow more than 256 characters to be output. However, these options aren't compatible with the -W option which I was using to remove trailing spaces from the output. I started by using -y 0 (despite the performance warning - once I get the output working I can look at the performance). However, that produces some strange results.The file consists of a header row followed by the data rows and should look something like this:
First,ABC,Number,String,ReallyLongString,...
A,0123,14.99,"Short string","Longish string",...
B,0456,23.99,"Normal string","Really, really long string that's causing problems",...With the
-W option the file format is correct but the really long string is truncated. The reason we found it was because the trailing " was missing and the file wasn't being read properly.With
-y 0 the really long string is getting output but large numbers of spaces are being added to apparently random columns. We're getting something like this:```
First,ABC ,Number [...] ,String,ReallyLongString,...
A ,0123,14.99 [...] ,"Short string","Longish string",...
B ,0456,23.99 [...] ,"Normal string","Really, really long string that's causing problems"
Solution
While trying out the various suggestions in the comments I have stumbled across a solution.
I replaced:
with:
and all of the text was output.
However, using a variable:
didn't.
What did work was declaring the variable with a specific length:
So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
This is the how. I still don't really know the why.
I replaced:
'"' + Column4 + '"' as ReallyLongString,with:
'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,and all of the text was output.
However, using a variable:
DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,didn't.
What did work was declaring the variable with a specific length:
DECLARE @longText NVARCHAR(4000)So, as the text in this case is never going to be longer than 2000 characters (the device reading the file can't cope with text that long) I convert all the text to 2000 character long strings:
SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE This is the how. I still don't really know the why.
Code Snippets
'"' + Column4 + '"' as ReallyLongString,'"' + REPLICATE('O', 4000) + '"' AS ReallyLongString,DECLARE @longText NVARCHAR(MAX)
SET @longText = REPLICATE('O', 4000);
'"' + @longText + '"' AS ReallyLongString,DECLARE @longText NVARCHAR(4000)SELECT
'A' as First,
Column1 as ABC,
REPLACE(FORMAT(Column2, 'N2', 'en-GB'), ',', '') as Number, -- to get the format correct
'"' + CAST(Column3 as NVARCHAR(2000)) + '"' as String,
'"' + CAST(Column4 as NVARCHAR(2000)) + '"' as ReallyLongString,
....
FROM Table
WHERE <condition>Context
StackExchange Database Administrators Q#165429, answer score: 2
Revisions (0)
No revisions yet.