I am trying to import TXT file into the postgreSQL database table, but I am getting an error:
ERROR:
missing data for column "bts_name"
SQL state: 22P04
My code is:
COPY indicadores2g (
Daily,
BTS_NAME,
SITE_CODE
)
FROM 'C:\Users\Public\Documents\GEO_2G_CELL.txt'
WITH CSV HEADER DELIMITER ' ' NULL AS '' ;
I know that the problem is in the txt file. In the txt file the last two line are blank (example), and when I remove them, the SQL run without problem.enter image description here
My problem is I need to import every day. Is there any rule to put in my SQL code to run without problems?
Another way to run without problems is: Open TXT in excel and save as CSV. Can I do this automatically?
Create simple batch (for example inpfixer.bat):
#echo off
for /f "delims=" %%a in (%1) do (
echo %%a
)
Then
COPY indicadores2g (
Daily,
BTS_NAME,
SITE_CODE
)
FROM PROGRAM 'inpfixer.bat C:\Users\Public\Documents\GEO_2G_CELL.txt'
WITH CSV HEADER DELIMITER ' ' NULL AS '' ;
Surely, inpfixer.bat should be available by PATH.
Disclaimer: Tested on the Wine.
Related
This question already has answers here:
What does %date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2% mean?
(2 answers)
Closed 4 years ago.
I want to copy a file to another file that contains the time and date
in its name.
I use the statement below but the problem is for time values earlier than 10 AM (for which the hour value is only a single digit) there is a blank character instead of a leading zero, which I want.
copy "M:\Production Schedule.xlsm" m:\gsdBackups\ProductionSchedule%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%%time:~6,0%.xlsm
In the example above when I run it at 9:50 I get the resulting name:
GSDProductionSchedule20180509_ 950.xlsm
I do not understand all of the formatting that is going on in the above copy command. Rather than the " 950" below I'd like to have "0950"
As a recurring task this should be a batch file hiding the details.
use wmic or PowerShell to get date/time in a user settings /locale independent format
name the batch file to any name with the extension .bat or .cmd and place it in a folder which is in the path
:: Q:\Test\2018\05\09\SO_50256566.cmd
#echo off
Set "Src=M:\Production Schedule.xlsm"
Set "Dst=M:\gsdBackups"
:: Get date and time in a user settinhs/locale independent format
For /f %%Y in ('
powershell -NoP -C "(get-date).AddDays(0).ToString('yyyyMMdd_HHmm')"
') Do Set _DT=%%Y
:: get source and use for variable modifiers to get name extension separated
For %%F in ("%Src%") Do echo copy "%%~F" "%Dst%\%%~nF_%_DT%%%~xF"
Sample output:
> SO_50256566.cmd
copy "M:\Production Schedule.xlsm" "M:\gsdBackups\Production Schedule_20180509_1743.xlsm"
If it looks OK to you remove the echo in front of copy.
Here is my dilemma. I cannot get my code to work. I have a batch file which executeS an SQL script. The batch file needs to be able to execute the sql script using the proper variable. Here is my batch file
#echo off
set list= TRA-1000 TRA-1002 TRA-1003 TRA-1004
for %%a in (%list%) do (
#sqlplus gpms/gpms#prod112 #get_tra1098_data.sql outputfilename = %%a
)
#pause
So essentially I am trying to pass the selected item from 'list' into my sql script. Here are the relevant lines of code from my sql file:
spool $(outputfilename).csv
...
where inst.instruction_doc_no in ($(outputfilename))
...
It is not working. How do I get my batch file to properly call my sql file in a loop, each time calling the sql file using the proper item in 'list'?
ps. this is what the sql file would look like if I wasn't trying to automate it:
spool TRA-1000.csv
...
where inst.instruction_doc_no in ('TRA-1000')
...
Im having some issues understanding what does the following type of query do:
insert overwrite local directory $directorey_name$
select $some_query$
What does this mean, and what are the side effects of this?
Export the query results into a file on the local file system
insert overwrite local directory '/tmp/hello'
row format delimited
fields terminated by '|'
select 1,2,3,'Hello','world'
;
! ls /tmp/hello;
000000_0
! cat /tmp/hello/000000_0;
1|2|3|Hello|world
How to export monetdb query result (e.g. to csv file)?
Manual says:
Copy into File
The COPY INTO command with a file name argument allows for fast
dumping of a result set into an ASCII file. The file must be
accessible by the server and a full path name may be required. The
file STDOUT can be used to direct the result to the primary output
channel.
The delimiters and NULL AS arguments provide control over the layout
required.
COPY subquery INTO file_name [ [USING] DELIMITERS
field_separator [',' record_separator [ ',' string_quote ]]] [ NULL AS
null_string ]
https://www.monetdb.org/Documentation/Manuals/SQLreference/CopyInto
I'm trying with various syntax but with no result.
example query:
select * from test;
example failures:
copy select * from test into test.csv;
copy "select * from test" into test.csv;
OK. Missing apostrophe and full path. Also delimiters useful
copy select * from test into '/home/user/test.csv' using delimiters ',';
I am trying to insert the data from this link to my SQL server
https://www.ian.com/affiliatecenter/include/V2/CityCoordinatesList.zip
I created the table
CREATE TABLE [dbo].[tblCityCoordinatesList](
[RegionID] [int] NOT NULL,
[RegionName] [nvarchar](255) NULL,
[Coordinates] [nvarchar](4000) NULL
) ON [PRIMARY]
And I am running the following script to do the bulk insert
BULK INSERT tblCityCoordinatesList
FROM 'C:\data\CityCoordinatesList.txt'
WITH
(
FIRSTROW = 2,
MAXERRORS = 0,
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n'
)
But the bulk insert fails with following error
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".
When I google, I found several articles which says the issue may be with RowTerminator, but I tried everything like \n\r, \n etc, but nothing is working.
Could anyone please help me to insert this data into my database?
Try ROWTERMINATOR = '0x0a'.
it should work.
This can also happen if the number of columns mismatch between the table and the imported file
I got the same error message, and as you had mention, it was related to unexpected line ending.
In my case the line ending was specified in a fmt file as a Windows Line ending (CRLF), written as \r\n, and the data file to process has a Mac classic one (CR).
I solved it with an editor that can show the current line ending and change it. I used EditPad Lite wich shows the opened file line ending in the bottom bar and pressing it allow to replace with the expected one.
I had this on SQL2019 when the FORMAT='CSV' option was used, and there was a comma on the end of each line in the source file. So the table your BULK inserting into needed to have an extra dummy field to cater for the fact each record has essentially a blank field in the source file.
!
I get the same error, probably from the file encoding problem. I fixed it by opening the problem CSV file using Notepad++, select everything and copy to clipboard. Next, create a new text file (making sure it has the CSV file extension), open it using Notepad++, then paste the text to the new file. Save and close all files. You should be able to successfully load the new CSV file into the SQL server.
you need run BULK INSERT - command from windows login (not from SQL). Now I don't have any examples