bulk insert isn't working reading .txt file into SQL Server - sql-server

I'm using bulk insert to read a text file into a SQL Server table. I keep getting the same error when I try using bulk insert, even though if I take the first line of the text file, and copy the values into a plain old insert into statement, everything works. My database, stop_times, is setup as follows:
train_num int,
arrival_time time,
dept_time time,
station_name varchar(50),
seq_num, int
My .txt file has the following format
101,'04:30:00','04:30:00','San Jose',1
101,'04:35:00','04:35:00','Santa Clara',2
101,'04:40:00','04:40:00','Lawrence',3
Running
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=','
)
Gives me
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (train_num).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 2 (arrival_time).
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 3, column 2 (arrival_time).
etc
However, copying and pasting the first row into
insert into stop_times(train_num, arrival_time, dept_time, station_name, seq_num)
values (101, '04:30:00', '04:30:00', 'San Jose', 1);
works. I feel like I'm missing something totally obvious in the bulk insert function, but am clueless.

try this
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n'
)
IF you have column names in 1st row u need to do something like this
bulk insert dbo.stop_times
from 'C:\Users\amanda\Desktop\stop_times1.txt'
with(
FIELDTERMINATOR=',',
ROWTERMINATOR = '\n',
FIRSTROW = 2
)

Related

Error creating a computed column in SQL Server

I have this question regarding computed columns. This is the first time that I work with them, and I do not understand why I'm getting an error.
Let's assume I have a table already created:
create table testComputed
(
a int null,
b int null
)
Now, I want to update it with another couple of columns including a computed one:
alter table testcomputed add c int null;
alter table testcomputed add d int null;
alter table testcomputed add e as c + d;
However, I'm getting this error message:
Msg 207, Level 16, State 1, Line 10
Invalid column name 'c'
Msg 207, Level 16, State 1, Line 10
Invalid column name 'd'
What is missing in the code, why the computed column is not created?
As I mention in the comment, the problem here is that the parsing/compilation is failing. Prior to the batch actually running it is parsed, and only some objects/properties benefit from the parser knowing that the object was created previous in the batch, and thus not failing; columns are not one of these. As such when the batch is parsed, the columns c and d do not exist at that point, and so the the parser generates an error.
The simple solution would be to separate the latter statement into a separate batch. In an application like SSMS that would just mean adding a batch separator (traditionally GO):
ALTER TABLE dbo.testcomputed ADD c int NULL;
ALTER TABLE dbo.testcomputed ADD d int NULL;
GO
ALTER TABLE dbo.testcomputed ADD e AS c + d;
GO
If you need to do it in the same batch, you could defer the parsing by using sys.sp_executsql:
ALTER TABLE dbo.testcomputed ADD c int NULL;
ALTER TABLE dbo.testcomputed ADD d int NULL;
EXEC sys.sp_executesql N'ALTER TABLE dbo.testcomputed ADD e AS c + d;';

PL/SQL to DECLARE a CURSOR with an EXCEPTION [ERROR]

I am using Oracle Apex and I tried various forms of this code but for some reason, I keep getting the same error message and I'm frankly getting frustrated! Can someone please help me and explain why I am getting this wrong? (It's my first advanced dbms course so I hope the question isn't too stupid.)
This is my error:
ORA-06550: line 25, column 16: PLS-00201: identifier 'MARK_ASSG' must be declared
ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_210200", line 673
ORA-06550: line 25, column 1: PL/SQL: Statement ignored
ORA-06550: line 26, column 16: PLS-00201: identifier 'MARK_EXAM' must be declared
ORA-06550: line 26, column 1: PL/SQL: Statement ignored
ORA-06550: line 27, column 11: PLS-00201: identifier 'COEF_ASSG' must be declared
ORA-06550: line 27, column 1: PL/SQL: Statement ignored
ORA-06550: line 28, column 10: PLS-00306: wrong number or types of arguments in call to '+'
ORA-06550: line 28, column 1: PL/SQL: Statement ignored
ORA-06512: at "SYS.DBMS_SYS_SQL", line 1658 ORA-06512: at "SYS.WWV_DBMS_SQL_APEX_210200", line 659 ORA-06512: at "APEX_210200.WWV_FLOW_DYNAMIC_EXEC", line 1829
DECLARE
mark NUMBER(4,1);
nbe INT;
no_student EXCEPTION;
I have the following tables: student, notation, result, course.
Creating table STUDENT and inserting data:
create table student
( nums number(2) primary key, name varchar(20), block number(2) );
insert into student values (10,'A',1);
insert into student values (20,'B',1);
insert into student values (30,'C',1);
insert into student values (40,'D',2);
insert into student values (50,'E',2);
insert into student values (60,'F',3);
Creating table COURSE and inserting data:
create table course
( code_course number(2) primary key, coef_assg number(4, 2), coef_exam number(4, 2) );
insert into course values(1, 0.5, 0.5);
insert into course values(2, 0.2, 0.8);
insert into course values(3, 0.3, 0.7);
insert into course values(4, 0.4, 0.6);
Creating table NOTATION and inserting data:
create table notation
( nums number(2), code_course number(2), mark_assg number(4,1), mark_exam number(4,1),
foreign key (nums) references student(nums),
foreign key (code_course) references course(code_course)
);
insert into notation values(10, 1, 15, 12);
insert into notation values(10, 2, 16, 8);
insert into notation values(10, 3, 12, 13);
insert into notation values(10, 4, 16, 17);
insert into notation values(20, 1, 12, 8);
insert into notation values(20, 2, NULL, 13);
insert into notation values(20, 3, 7, 9);
insert into notation values(20, 4, 17.5, NULL);
insert into notation values(40, 1, 7, 5);
insert into notation values(40, 2, 8, 9);
insert into notation values(40, 3, NULL, 12);
insert into notation values(40, 4, NULL, NULL);
insert into notation values(50, 1, 10, 11.5);
insert into notation values(50, 2, 5, 13);
insert into notation values(50, 3, NULL, 16);
insert into notation values(50, 4, 9, 15);
insert into notation values(60, 1, 15, 12);
insert into notation values(60, 2, 16, 8);
insert into notation values(60, 3, 12 ,13);
insert into notation values(60, 4, 16, 17);
QUESTIONS:
Define a PL/SQL program that enables inserting data in the table RESULT. The calculation of the student final mark must take into account the assignment coefficient (example: 20% = 0.2) and the exam coefficient (example: 80% = 0.8). The student can have a Zero as a mark of an assignment or exam. Thus, use the NVL function to compute the student's final mark in each course.
Define a cursor “student_mark” from tables STUDENT, NOTATION, and COURSE useful to compute the student's final mark in each course.
Define a cursor “student_No_mark” from the table STUDENT to consider the case of the student without marks. Consider the possibility of having no tuple in the STUDENT table. Raise this case as EXCEPTION and insert the values (0, ‘No_student’, NULL, NULL) into the RESULT table.
MY CODE:
DECLARE
mark NUMBER(4,1);
nbe INT;
no_student EXCEPTION;
CURSOR student_mark IS
SELECT student.nums, student.name, course.code_course, course.coef_assg, course.coef_exam, notation.mark_assg, notation.mark_exam
FROM student,course, notation
WHERE (student.nums = notation.nums) AND (notation.code_course=course.code_course);
CURSOR student_no_mark IS
SELECT nums,name
FROM student
WHERE nums NOT IN (SELECT DISTINCT nums FROM notation);
s student_mark%ROWTYPE;
sn student_no_mark%ROWTYPE;
n_mark_assg notation%ROWTYPE;
n_mark_exam notation%ROWTYPE;
n_coef course%ROWTYPE;
BEGIN
DELETE FROM result;
SELECT COUNT(*) INTO nbe FROM student;
IF (nbe=0) THEN RAISE no_student;
END IF;
OPEN student_mark;
FOR s IN student_mark LOOP
n_mark_assg := mark_assg*coef_assg;
n_mark_exam := mark_exam*coef_exam;
n_coef := coef_assg+coef_exam;
mark := (n_mark_assg+n_mark_exam)/(n_coef);
INSERT INTO result VALUES (s.nums, s.name, NULL, 0);
END LOOP;
CLOSE student_mark;
OPEN student_no_mark;
FOR sn IN student_no_mark LOOP
INSERT INTO result VALUES(sn.nums, sn.name, NULL, 0);
END LOOP;
CLOSE student_no_mark;
EXCEPTION
WHEN no_student THEN INSERT INTO result VALUES(0,'No Student', NULL, NULL);
END;
There are a lot of errors in that code. If you solve the errors you are getting now you'll run into other errors. Since this is a learning task, I'm going to point out some issues that are causing the errors and not fix your code. Note that this list might not be exhaustive.
A tip for writing any code: start with the simplest working block, then add a bit of code. If it fails, fix it first, then add a bit more code until you're done. If you write a complete block from the beginning you'll get into an endless loop of fixing errors which could result in code that is a lot worse.
DECLARE
-- tip: prefix local variables with a fixed prefix, eg l_mark instead of mark. It will make your code a lot more readable.
mark NUMBER(4,1);
nbe INT;
no_student EXCEPTION;
-- Tip: use ANSI join syntax
CURSOR student_mark IS
SELECT student.nums, student.name, course.code_course, course.coef_assg, course.coef_exam, notation.mark_assg, notation.mark_exam
FROM student,course, notation
WHERE (student.nums = notation.nums) AND (notation.code_course=course.code_course);
CURSOR student_no_mark IS
SELECT nums,name
FROM student
WHERE nums NOT IN (SELECT DISTINCT nums FROM notation);
-- all the variables below are defined as rowtype. That implies that you have to assign an entire row to them or define the column when assigning.
s student_mark%ROWTYPE;
sn student_no_mark%ROWTYPE;
n_mark_assg notation%ROWTYPE;
n_mark_exam notation%ROWTYPE;
n_coef course%ROWTYPE;
BEGIN
-- there is no ddl for a result table in the question
DELETE FROM result;
SELECT COUNT(*) INTO nbe FROM student;
IF (nbe=0) THEN RAISE no_student;
END IF;
OPEN student_mark;
FOR s IN student_mark LOOP
-- 2 things wrong in row below
-- 1. n_mark_assg is of type ROWTYPE, so you cannot assign a scalar value. Either change datatype of n_mark_assg or assign to a specific column.
-- 2. "mark_assg" and "coeff_assgn" are cursor attributes, reference them with the cursor name (eg s.mark_assg)
n_mark_assg := mark_assg*coef_assg;
n_mark_exam := mark_exam*coef_exam;
n_coef := coef_assg+coef_exam;
mark := (n_mark_assg+n_mark_exam)/(n_coef);
INSERT INTO result VALUES (s.nums, s.name, NULL, 0);
END LOOP;
CLOSE student_mark;
OPEN student_no_mark;
FOR sn IN student_no_mark LOOP
INSERT INTO result VALUES(sn.nums, sn.name, NULL, 0);
END LOOP;
CLOSE student_no_mark;
EXCEPTION
WHEN no_student THEN INSERT INTO result VALUES(0,'No Student', NULL, NULL);
END;
/

Conversion failed on value

Who can help me on this?
select Battery_Volts
from ems
where Battery_Volts between 25 and 27
I am lost on this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '25.19' to data type int.
If my guess is correct, and you're using a varchar to store a numerical value, you could also try:
where Battery_Volts between 25.00 and 27.00
If you get a further conversion error, I imagine you have non numerical values in your data, which opens a further can of worms.

TSQL BULK INSERT with auto incremented key from .txt file

error im getting
This is to insert into an already created table:
CREATE TABLE SERIES(
SERIES_NAME VARCHAR(225) NOT NULL UNIQUE, --MADE VARCHAR(225) & UNIQUE FOR FK REFERENCE
ONGOING_SERIES BIT, --BOOL FOR T/F IF SERIES IS COMPLETED OR NOT
RUN_START DATE,
RUN_END DATE,
MAIN_CHARACTER VARCHAR(20),
PUBLISHER VARCHAR(12),
S_ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
CONSTRAINT chk_DATES CHECK (RUN_START < RUN_END)
)
and the text file is organized as:
GREEN LANTERN,0,2005-07-01,2011-09-01,HAL JORDAN,DC
SPIDERMAN,0,2005-07-01,2011-09-01,PETER PARKER,MARVEL
I have already tried adding commas to the end of each line in .txt file
I have also tried adding ,' ' to the end of each line.
Any suggestions?
Indeed, the KEEPIDENTITY prevents the bulk insert from taken place. Removing the statement however won't resolve the problem.
Msg 4864, Level 16, State 1, Line 13
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 7 (S_ID).
The bulk insert expects to update all the columns. Another way of solving this issue is adding a format file for the text file, see MS Docs - Use a Format File to Bulk Import Data
You can create a format file for your text file with the following command.
bcp yourdatabase.dbo.series format nul -c -f D:\test.fmt -t, -T
Remove the last row, update the number of columns, and replace the last comma with the row terminator. The result will look like as shown below.
13.0
6
1 SQLCHAR 0 255 "," 1 SERIES_NAME SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 1 "," 2 ONGOING_SERIES ""
3 SQLCHAR 0 11 "," 3 RUN_START ""
4 SQLCHAR 0 11 "," 4 RUN_END ""
5 SQLCHAR 0 510 "," 5 MAIN_CHARACTER SQL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 510 "\r\n" 6 PUBLISHER SQL_Latin1_General_CP1_CI_AS
Remove KEEPIDENTIY from your BULK INSERT, since that specifies that you want to use the values in the source text file as your IDENTITY.
If this still fails, try adding a VIEW on the table that excludes the IDENTITY field, and INSERT into that instead, e.g.:
CREATE VIEW SeriesBulkInsertTarget
AS
SELECT Series_Name,
Ongoing_Series,
Run_Start,
Run_End,
Main_Character,
Publisher
FROM SERIES

Bulk insert in SQL Server with a regional separator

I want to make bulk insert of data where decimal separator is a comma as in regional settings.
The data are the following:
RegionName Value_1 Value_2 Value_3
Region 1 27,48 66,41 32,82
Region 2 38,93 45,80 61,83
Region 3 38,17 58,02 35,11
Region 4 34,35 16,03 29,01
Region 5 67,94 58,02 17,56
I make the bulk insert using this script:
create table RegVaues (
RegionName varchar(30)
,Value_1 float
,Value_2 float
,Value_3 float
)
go
bulk insert RegVaues
from N'A:\TestValues.txt'
with
(
DATAFILETYPE = 'widechar'
,fieldterminator = '\t'
,rowterminator = '\n'
,firstrow = 2
,keepnulls
)
go
After fulfilling a scrip I receive an error:
sg 4864, Level 16, State 1, Line 2 Bulk load data conversion error
(type mismatch or invalid character for the specified codepage) for
row 2, column 2 (Value_1).
When I try the same with a dot as a separator - all works. I have tried to insert the data with different types (float, decimal, numeric). In my SSMS in Tools->Options->International Settings the Language is set to "Same as in Microsoft Windows". The database collation is Ukrainian_CI_AS. But still the data with a comma separator couldn't be inserted. What I'm doing wrong?
Well the error is self explanatory, values with commas in them are not float values and when you are to insert values like 27,48 , 66,41 into a float column, it tries to convert them values to float and it fails hence the error message.
A simple solution would be to insert the data into a holding/staging table first with column data type character (VarChar) , then replace the commas with a decimal point and then use them values to insert into your final destination table.
Also mind you float is an approximate data type and should only be used for approximate values like (mass of earth and distance between planets etc) for exact values use DECIMAL or NUMERIC data types.

Resources