Creating a Format File for Bulk Import - sql

I am trying to create a Format File to bulk import a .csv file but i, am getting an error.
Query I used
"BCP -SMSSQLSERVER01.[Internal_Checks].[Jan_Flat] format out -fC:\Desktop\exported data\Jan_FlatFormat.fmt -c -T -Uasda -SMSSQLSERVER01 -PPASSWORD"
I am getting an error
"A valid table name is required for in, out, or format options."
This is the error. can anyone suggest what need to do.

According to the bcp Utility documentation the first parameter should be a [Database.]Schema.{Table | View | "query"}, so don't put -SMSSQLSERVER01 where you've got it. Also use format nul instead of format out.
Try using:
bcp.exe [Internal_Checks].[Jan_Flat] format nul "-fC:\Desktop\exported data\Jan_FlatFormat.fmt" -c -SMSSQLSERVER01 -T -Uasda -PPASSWORD
Note the quotes " around the -f switch because your path name contains space characters.
Also note that the -c switch causes single-byte characters (ASCII/OEM/codepage with SQLCHAR) to be written out. If your table contains nchar, nvarchar or ntext columns you should consider using the -w switch instead so as to write out UTF-16 encoded data (using SQLNCHAR).

Related

How export CSV with SQLCMD in UTF-16?

I am trying to export the data from different queries by SQLCMD, my files must be exported in UTF-16, currently they do it in UTF8 BOM and I have been able to even in UTF8, but I cannot get it to export in UTF16.
Reviewing the Encoding of the file in Notepad++ this is the format in which it should be exported (UTF16 LE BOM), in the list selection it appears with the name UCS-2 Little Endian
Encoding to export
Encoding in options
SET #cmd= 'sqlcmd -s"-" -f i:1252 -Q "SELECT * FROM Machines" -f o:65001 -o "C:\CSV\report_machines.csv"'
EXEC master..xp_cmdshell #cmd
Check the page (https://www.example-code.com/sql/load_text_file_using_code_page.asp) and tried code 1200, however SQL Server returns the following message:
Sqlcmd: The code page <1200> specified in option -f is invalid or not installed on this system.
I want to avoid using another program, if anyone knows how to solve this problem I would greatly appreciate it.

Pass byte[] as parameter to sql insert script

I am trying to upload the binary[] of a Zip folder to my database. I used Get-Content -Encoding Byte -ReadCount 0 to read the data into a variable. I want to use this variable in an INSERT statement. Unfortunately, sqlcmd doesn't like the size of the variable, and gives me this error:
Program 'SQLCMD.EXE' failed to run: The filename or extension is too longAt line:1 char:1.
I have tried using the -Q option to run the query, and also -i to run a sql file.
DECLARE #data varbinary(MAX)
SET #data = '$(data_stuff)'
INSERT INTO MyTable
(v1,v2,v3,v4,v5)
VALUES
(v1,v2,v3,v4,#data)
sqlcmd -S servername -E -i .\file.sql -v data = "$binarydata"
Is there a workaround for doing this?
In a SQL query/batch/.sql file, binary/varbinary/image literal data values must be in hexadecimal format with a 0x prefix:
INSERT INTO tableName ( binaryColum ) VALUES ( 0x1234567890ABCDEF )
I don't know what the maximum length of a binary literal is, but I suspect things might stop working, or be very slow, if you exceed more than a few hundred kilobytes.
I recommend using ADO.NET directly via PowerShell, which will also let you use binary parameter values (SqlParameter): How do you run a SQL Server query from PowerShell?

POSTGRES. error: extra data after last expected column. one double quote?

I have a massive database, and I found an error, when the migration is a single ", returns the error.
ERROR: extra data after last expected column
my data is
...
0,direccion N"16, 109, 420000
0,otra direccion N"32", 109, 320000
...
my command to migrate
$ psql -U user sat -c "copy table FROM '/file.csv' WITH (FORMAT CSV, DELIMITER(','));"
The strange thing is that when I erase the double quotes and can migrate, there will be some way to escape or ignore "
Double quotes are the default quotation character for the COPY command. Use the QUOTE option to modify this:
psql -U user sat -c "copy table FROM '/file.csv' WITH (QUOTE '~', FORMAT CSV, DELIMITER(','));"
See PostgreSQL COPY Documentation

BCP Import error "Invalid character value for cast specification"

All
I am using BCP for import export and getting "Invalid character value for cast specification" error for only 1(first row of export) row while trying to import back.
Table Structure
Col1 -- Numeric(19,0)
Col2 -- NVARCHAR(400)
Col3 -- NVARCHAR(400)
I am using following commands
FOR Export
EXEC master..xp_cmdshell 'bcp "SELECT TOP 10 Col1, Col2, Col3 FROM Server.dbo.TableName" queryout C:\Data\File.dat -S Server -T -t"<EOFD>" -r"<EORD>" -w'
Same way I am generating a FORMAT file
EXEC master..xp_cmdshell 'BCP Server.dbo.TableName format nul -S Server -T -w -f "C:\Data\File.fmt" -t"<EOFD>" -r"<EORD>" '
Now when I try importing data back into SQL Server table I am getting error "Invalid character value for cast specification"
Error logs shows me something like this
## Row 1, Column 1: Invalid character value for cast specification ##
?1000 Mytestdataunicoded nothing
Now from where this ? added in starting of my column data is still unknown.
I am able to import successfully when trying importing with format file, also able to import successfully when using switch -c, but for some purposes we must use -w switch to do that.
I am using BCP for import export and getting "Invalid
character value for cast specification" error for only 1(first row of
export) row while trying to import back.
Does the first row of your export file contain column definition information?
If so, use -F2.
https://learn.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-ver15#F
When using -w option, I believe BCP ignores any -t or -r option and uses \t and \n and field and row terminators.
From MS docs:
-w Performs the bulk copy operation using Unicode characters. This option does not prompt for each field; it uses nchar as the storage
type, no prefixes, \t (tab character) as the field separator, and \n
(newline character) as the row terminator. -w is not compatible with
-c.

bcp utility asking me to enter different parameters i do not undertand

I am using BCP To export data from sqlserver 2008R2 Database Name Health,and a table name patient.The out of the query should be save in a textfile:ApplicantsName.txt located at:
C:\Users\meuser\Desktop ApplicantsName.txt -C -T
After running the following query on the command prompt:
bcp "Select FirstName,LastName,PatientNumber from Health.dbo.Patient order by FirstName" queryout "C:\Users\meuser\Desktop ApplicantsName.txt" -C -T
It prompted me this:
Enter the file storage type of fiedl FirstName [char]:varchar
and then this:
Enter prefix-length of field FirstName[2]:FirstName
I have been entering some values but i think the best is to know how it works.After some time of research on the internet, know using bcp utility is one fastest way to export or import data between instance to a file.I follow exactly the samples provided by MS here but i think i need some practical explanation. Can some guide me how to go about this and a little bit of explanation or relevant ref. will be appreciated too.
#one angry researcher's solution of adding '-C RAW' did not work in my particular case but adding lower-case '-c' did. It performs the operation using a character data type
For instance:
bcp mydb.mytable out c:/temp/data.txt -T -c
You need to add a value for the -C parameter (capital C!). If you do not know what you're using it for, you probably won't be needing it and can omitt it.
Refer to the official documentation: http://msdn.microsoft.com/en-us/library/ms162802.aspx
edit: you could, for example, use
bcp "Select FirstName,LastName,PatientNumber from Health.dbo.Patient order by FirstName" queryout "C:\Users\meuser\Desktop\ApplicantsName.txt" -C RAW -T
You will need to fix your output directory too (seems you forgot a backslash there).
heres the sample bcp command with query and credentials (param)
bcp "SELECT * from yourtable" queryout c:\StockItemTransactionID_c.txt -c -Uusername -Pdbpassword -Sinstance -dYourDBName
Note: -U -P -S are case sensitive.