BCP command insert text file to SQL table - sql

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

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)

Using variables when executing single command in PSQL

When using PSQL's variables, I can run it as follows:
psql -d database -v var="'123'"
And I will then have access to the variable var when I type the following in the PSQL terminal:
select * from table where column = :var;
This variable feature also works when the SQL is read from a file:
psql -d database -v var="'123'" -f file.sql
But when I try to run the SQL as a single command:
psql -d database -v var="'123'" -c "select * from table where column = :var;"
I can't access the variable and get the following error:
ERROR: syntax error at or near ":"
Is it possible to pass variables to single SQL commands in PSQL?
It turns out that, as man psql explains, the -c command is limited to SQL that "contains no psql-specific features":
-c command, --command=command
Specifies that psql is to execute one command string, command, and then exit. This is useful in shell
scripts. Start-up files (psqlrc and ~/.psqlrc) are ignored with this option.
command must be either a command string that is completely parsable by the server (i.e., it contains no
psql-specific features), or a single backslash command. Thus you cannot mix SQL and psql meta-commands
with this option. To achieve that, you could pipe the string into psql, for example: echo '\x \\ SELECT
* FROM foo;' | psql. (\\ is the separator meta-command.)
It looks like I can do what I want by passing in the SQL using stdin:
echo "select * from table where column = :var;" | psql -d database -v var="'123'"

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 ''

How to extract results with column name in shell from postgres

I would like to extract results with title of the column from postgres. I am using shell script to do the same. Please find following code which is giving only result without header.
#!/bin/sh
DATABASE=retail
USERNAME=root
HOSTNAME=localhost
export PGPASSWORD=root
psql -h $HOSTNAME -U $USERNAME $DATABASE << EOF
COPY (select name,rollno,mark from student';')
EOF
echo "Hi \n Please find student report " | mutt -a "/tmp/query1.csv" -s " Alert" -- abc_email#gmail.com
COPY statement has option HEADER. Use it. Another issue in your example is missing target in COPY statement (and other syntax error). From security reasons, it should be stdout in this case (I do export of pg_class table columns relname, and relpages):
psql -c "COPY pg_class(relname,relpages) TO stdout CSV HEADER" postgres > /tmp/query1.csv
With this syntax you will get valid CSV file.

Specify rowterminator when creating a file with SQLCMD

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...