Specify rowterminator when creating a file with SQLCMD - sql

How do you specify the the row terminator when outputting query results using sqlcmd?
bcp is not an option.
sqlcmd -E -s" " -Q "Select * From SomeTable" -o C:\Output.txt -W
What is the default row terminator?

You can only choose column separator with -s. There is not option to specify row terminator.
So it is CR + LF because the output goes to command line. However, data may be truncated so you have to control the output width.
See sqlcmd for -s, -w, -W, -y, -Y etc
bcp may not be an option, as you say, but neither is row terminator in sqlcmd...

Related

BCP detects CRLF as data and not as Row ending

I am using bcp to export and import data, for example:
bcp "exec db.dbo.procedure" queryout "C:\Users\I\Desktop\ps.dat" -c -r "*" -t; -S -U -P
bcp db.dbo.table in C:\Users\I\Desktop\ps.dat -e "C:\Users\I\Desktop\ps_error.dat" -c -r "*" -t; -m1000000 -S -U -P
If I execute these statements without -r, bcp uses the default CRLF as end of row. Later, the import fails with right data truncation.
After so many attempts I have seen that CRLF is detected as two bytes of data, and it does not fit the table format. When I use the above statements it works perfectly.
Why is this happening? Is this a bcp bug, or is the expected behaviour?
According to MS that is the expected behaviour:
https://learn.microsoft.com/en-us/sql/tools/bcp-utility
this article explains all the parameters and for this case these are the ones we are interested in:
-c
Performs the operation using a character data type. This option does not prompt for each field; it uses char as the storage type, without prefixes and with \t (tab character) as the field separator and \r\n (newline character) as the row terminator. -c is not compatible with -w.
-r
row_term
Specifies the row terminator. The default is \n (newline character). Use this parameter to override the default row terminator. For more information, see Specify Field and Row Terminators (SQL Server).
So it seems that by removing -r which sets the row terminator to \n (LF) , -c is taking over and setting the row terminator to \r\n (CRLF)

Save column value to file SQL Server

How can I save first column from first row from query to file without additional character?
When I save data like this:
EXEC xp_cmdshell 'BCP "SELECT ''xxx'' " queryout D:\file.txt -w -T -S OMD-MG\SQL2008R2'
I've got:
additional \r\n at the end of file
When I save data like that:
EXEC xp_cmdshell 'BCP "SELECT ''xxx'' " queryout D:\file.txt -N -T -S OMD-MG\SQL2008R2'
I've got:
additional characters at front of file I think this is length
I try many parameter without satisfied result
Is there other option to save data to file from query without designer or management studio with correct data?
-N is a native binary format where any nullable or variable length fields are preceeded by their length. If you use -N with a non nullable, fixed-width field it will not be preceeded by its length.
If you want text data without the newlines you could try -r '' to specify the row terminator which is \n by default, e.g.:
bcp "select 'xxx'" queryout test.txt -c -t '' -r ''
..at least in SQL Server 2016 CTP I'm seeing that BCP tries to add padding to varchar columns. If you convert to text it seems to work alright:
bcp "select convert(text, col) from table" queryout file -c -t '' -r ''

BCP command insert text file to SQL table

I am trying to import into SQL a text file using this BCP command:
bcp test.dbo.bcp2 in C:\Test\test.txt -c -t -SSQServer -U user -P
test1 -t \t -r\n -e C:\Test\error.txt
The text.txt file has \t as column delimiter and \n as row delimiter.
The error received is Unexpected EOF.
I can confirm that the SQL table has the right table definition so there should not be any conversion errors.
i think no need to put any delimiter have text which is well arranged example if you have data in the excel copy and paste it in the text file and run the command
BCP tablename in c:\test.txt -S server name -Uuserid -Ppassword -c

Specify column separator for BCP

How do I specify column separator with "," for bcp or this code below
bcp [wwtest].[accesscontrol].[Roles] out C:\test\bcp_outputTable.csv -SPC01 -T -c
Try adding -t, . See following documentation : https://msdn.microsoft.com/en-us/library/ms191485.aspx and https://msdn.microsoft.com/en-us/library/ms162802.aspx
The command lines below illustrate how to export data using a comma (,) or vertical pipe (|) as a field separator:
Export using comma:
BCP [Database].[Schema].[Table] out C:\FOLDERNAME\FILENAME.DAT -c -t, -S SERVERNAME -T
Export using vertical pipe:
BCP [Database].[Schema].[Table] out C:\FOLDERNAME\FILENAME.DAT -c -t^| -S SERVERNAME -T
The vertical pipe is different because it must be escaped. Quoting -t| will not work.
I hope this helps.
Please note command line switches are case sensitive.

Sqlcmd to generate file without dashed line under header, without row count

Using the following sqlcmd script:
sqlcmd -S . -d MyDb -E -s, -W -Q "select account,rptmonth, thename from theTable"
> c:\dataExport.csv
I get an csv output file containing
acctnum,rptmonth,facilname
-------,--------,---------
ALLE04,201406,Allendale Community for Senior Living-LTC APPL02,201406,Applewood Estates
ARBO02,201406,Arbors Care Center
ARIS01,201406,AristaCare at Cherry
Hill
. . .
(139 rows affected)
Is there a way to get rid of the dashed line under the column headers : -------,--------, but keep the column headers?
and also a way to get rid of the two lines used for the row count on the bottom?
I tries using parm -h-1 but that got rid of the column headers as well as the dashed line.
Solutions:
1) To remove the row count ("(139 rows affected)") you should use SET NOCOUNT ON statement. See ref.
2) To remove column headers you should use -h parameter with value -1. See ref (section Formatting Options).
Examples:
C:\Users\sqlservr.exe>sqlcmd -S(local)\SQL2012 -d Test -E -h -1 -s, -W -Q "set nocount on; select * from dbo.Account" > d:\export.txt.
or
C:\Users\sqlservr.exe>sqlcmd -S(local)\SQL2012 -d Test -E -h -1 -s, -W -Q "set nocount on; select * from dbo.Account" -o "d:\export2.txt"
The guy with the top answer didn't answer how to remove the dashed line. This is my awesome solution.
First include -h -1 which removes both the dashed line and header
Then before your select statement manually inject the header string that you need with a PRINT statement. So in your case PRINT 'acctnum,rptmonth,facilname' select..*...from...
Sorry I'm 4 years and 9 months late.
Use the following;
sqlcmd -S . -d MyDb -E -s, -h-1 -W -Q "set nocount on;select 'account','rptmonth', 'thename';select account,rptmonth, thename from theTable"
> c:\dataExport.csv
remove the header -h-1
remove row count [set nocount on;]
add header select [select 'account','rptmonth', 'thename';]
add your select [select account,rptmonth, thename from theTable;]
To remove the Row Count:
Add the below to your SQL statement
SET NOCOUNT ON;
To remove the hyphen row try the following upon successful execution:
findstr /v /c:"---" c:\dataExport.csv > c:\finalExport.csv
I use "---" as all my columns are over 3 characters and I never have that string in my data but you could also use "-,-" to reduce the risk further or any delimiter based on your data in place of the ",".
In my case worked well as :
type Temp.txt | findstr /v -- > DestFile.txt
In addition, if you want to query out all records in a table, you can code as
SET NOCOUNT ON;
SELECT SUBSTRING((SELECT ','+ COLUMN_NAME FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=N'%table_name%' FOR XML
PATH('') ), 2, 9999);
SELECT * FROM %table_name%
Assign the above queries into a variable %query%. The the command will be looks like as below.
SQLCMD -h -1 -W -E -S %sql_server% -d %sql_dabase% -Q %query% -s"," -o output_file.csv
This is the one line solution, without doing anything inside the stored procedure to append the column headers:
sqlcmd -S . -d MyDb -E -s, -W -Q "select account,rptmonth, thename from theTable"
| findstr /v /c:"-" /b > "c:\dataExport.csv" & exit 0
What this does is it intercepts all console output and replaces the "-" char BEFORE it redirects to the output file. There is NO need to output to intermediary file. And you will need a one-liner command if you use an agent to run these commands remotely on the sql server machines, which most of the times are locked from hosting *.bat files (which you'd need for multiline commands).
I added the "exit 0" at the end to not fail the caller application overall. You may remove it starting "& exit 0" if you don't care about that.
This one liner is why I chose sqlcmd over bcp out, by the way. BCP, although optimized for speed, cannot output column headers unless doing the ugly trick within the stored proc, to append them there as a union all.
Just in case you have access to writing a bat file that contains this one liner, you MUST add #ECHO OFF before it. Otherwise the console output will also have the actual command.
Hope it helps.
With SQL Server 2017 (14.x) and later you can print header with:
SELECT string_agg(COLUMN_NAME, ', ') within group (order by ORDINAL_POSITION asc) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='YOUR_TABLE_NAME'
1.Create the file first with the header columns
2.Apprend the sqlcmd output to the file using the option -h-1
echo acctnum,rptmonth,facilname > c:\dataExport.csv
sqlcmd -S . -d MyDb -E -s, -h-1 -W -Q "select account,rptmonth, thename from theTable" >> c:\dataExport.csv
I used another solution to solve the issue of removing the dashed line below the header.
DECLARE #combinedString VARCHAR(MAX);
SELECT #combinedString = COALESCE(#combinedString + '|', '') + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YOUR_TABLE_NAME'
Then just use
Print #combinedString above your select statement.
I used pipe delimiter.