Bulk load data conversion error (EMP_TITLE) - sql

I am trying to work with amusement park database here. I have created all the required tables. Now i need to Bulk insert records in all the tables. In the employee table i am trying to insert the records but keep getting this error
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 24, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 45, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
Bulk load data conversion error (truncation) for row 648, column 3 (EMP_TITLE).
Msg 4863, Level 16, State 1, Line 77
I am unable to figure out what is the error here.
My EMPLOYEE table structure is this
CREATE TABLE EMPLOYEE (
EMP_NUM NUMERIC(4) PRIMARY KEY,
EMP_NAME VARCHAR(30) NOT NULL,
EMP_TITLE VARCHAR(35),
EMP_HIRE_DATE DATE,
EMP_DOB DATE NOT NULL,
EMP_PHONE VARCHAR(15) NOT NULL,
PARK_CODE VARCHAR(10),
CONSTRAINT FK_EMP_PARK FOREIGN KEY(PARK_CODE) REFERENCES THEMEPARK(PARK_CODE)
);
and i am using this command to bulk insert records into database
BULK INSERT EMPLOYEE
FROM 'C:\Users\TechProBox\Desktop\Amusement Park Data\Employee.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0X0a',
TABLOCK
)
This is how my data looks like in csv file

Related

Bulk insert null into table from csv file in SQL Server

I want to bulk insert from a csv file to a table.
This is my code for creating the table
create table customers
(
customerNumber int not null,
customerName varchar(50) not null,
salesRepNumber int null,
primary key(customerNumber)
);
This is the example of the csv file:
1001,John,1121
1002,James,1122
1003,Jonas,1123
1004,Jane,1124,
1005,Tom,1125,
1006,Bob,1126
1007,Thomas,NULL
This is the code for inserting data into the table:
BULK INSERT customers
FROM 'D:\Customers.csv'
WITH (fieldterminator=',', rowterminator='\n')
And I get these errors:
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 7, column 12 (salesRepNumber).
Msg 4864, Level 16, State 1, Line 4
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 22, column 12 (salesRepNumber).
I am not so sure about how to bulk insert 'NULL' into a table and I am only supposed to use int datatype for the salesRep.

Generating random primary key during bulk insert

I am trying to read some data from .csv and add the to a table with a randomly generated primary key.
So I created a database, added a table and if I insert data 1 by 1 the everything is fine but I want to add the data in bulk and I'm not sure if the primary key would be automatically generated or not.
I created a view and the added everything to the view except the Id column and then called the bulk insert on the view.
CREATE TABLE Person
(
PersonID UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
StreetAddress VARCHAR(50) NOT NULL,
City VARCHAR(50) NOT NULL,
State_prov VARCHAR(2) NOT NULL,
Zip VARCHAR(5) NOT NULL,
phoneNumber VARCHAR(15) NOT NULL,
SSN VARCHAR(15) NOT NULL
)
CREATE VIEW viewPerson
AS
SELECT everything other than personID
FROM Person
BULK INSERT viewPerson
FROM 'Data1.csv'
WITH (FIRSTROW = 2,FIELDTERMINATOR = ',' , ROWTERMINATOR = '\n');
Sample data:
Olivia,Abbas,7823 West Charles Lan,Bothell,WA,98012,956) 692-3392,770-78-9562
Isla,Abbey,9534 East Bank Ave,Bothell,WA,98012,(413) 296-8853,770-78-9563
Amara,Abbott,9535 East Bank Ave,Bothell,WA,98012,(788) 281-2027,770-78-9564
This is the error that I am getting:
Msg 4863, Level 16, State 1, Line 62
Bulk load data conversion error (truncation) for row 11, column 7 (SSN).
Msg 4863, Level 16, State 1, Line 62
Bulk load data conversion error (truncation) for row 12, column 7 (SSN).
Msg 4865, Level 16, State 1, Line 62
Cannot bulk load because the maximum number of errors (10) was exceeded.
Msg 7399, Level 16, State 1, Line 62
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7330, Level 16, State 2, Line 62
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
And there is nothing in the table.
enter image description here

How to fix this message error that i cant create table using script?

I just want to make a new table in database, when I create table just got some messages like
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'tbl_invoice'".
So my question is where do I start to make a new table?
use master
use db_penjualan
create table tbl_invoice (
no_invoice varchar primary key (10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null )
;
Messages
Msg 102, Level 15, State 1, Line 5 Incorrect syntax near
'tbl_invoice'.
The problem lies with primary key declaration syntax. Write it as:
use db_penjualan
Go
create table tbl_invoice (
no_invoice varchar(10),
jenis_pembayaran varchar(25),
user_generate varchar (10),
tgl_terbit date not null ,
primary key(no_invoice)--define primary key col here
)
;

Error String or binary data would be truncated

I've been trying to create tables and add data into but I've run into this error
Msg 8152, Level 16, State 14, Line 35
String or binary data would be truncated.
Code:
CREATE TABLE Speakers_photos
(
SpeakerID CHAR(10) NOT NULL,
Image VARBINARY(MAX) NOT NULL,
PRIMARY KEY(SpeakerID),
FOREIGN KEY (SpeakerID) REFERENCES Speakers(SpeakerID)
ON UPDATE CASCADE ON DELETE NO ACTION
)
INSERT INTO Speakers_photos VALUES('S001210001', 0)
Where did it go wrong?
The truncation error will not occur with the code provided. Either the script is incomplete or the error is in an INSERT trigger on the Speakers_photos table. In the case of a trigger, look at line 35 as indicated in the error message.

Trying to insert data correctly

After receiving very good correction from fuzzy lollipop, I amended my code to create an insert statement for every row of data. This is the code that I entered:
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0001','Think Tank',NULL,'L0001')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0002','Creators',NULL,'L0002')
GO
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0003','Marketers',NULL,'L0003')
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0002','John','Doe',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0003','Sue','Happy',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0004','Tina','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0005','Ike','Turner',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0006','Big','Bird',NULL,NULL,2010/06/25,NULL,NULL)
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0007','Speedy','Gonzales',NULL,NULL,2010/06/25,NULL,NULL)
GO
However, these were the error messages that I received:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 2
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__DEPARTMEN__Locat__09DE7BCC". The conflict occurred in database "Final_Project", table "dbo.LOCATIONS", column 'Location_ID'.
The statement has been terminated.
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
Msg 206, Level 16, State 2, Line 2
Operand type clash: int is incompatible with date
I won’t make the same mistake by failing to respond immediately to solutions. I did not know the checking the green checkmark meant that the answer was satisfactory.
Thank you for any help
You have two different types of errors.
The first is that you are violating a foreign key constraint. There are three ways to solve this:
Find out what the correct key should be (for example by querying the LOCATIONS table) and change your foreign key to the correct value.
Insert the missing row in the LOCATIONS table before inserting into DEPARTMENTS.
Remove the constraint (this is probably a bad idea).
The second error is simpler - you have incorrectly formatted your date. It should be a string.
'2010-06-25'
The complete query:
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001','Joe','Blow',NULL,NULL,'2010-06-25',NULL,NULL)
1) there are no records with the given LocationIDs in the Location table
2) you need to quote the date values
Is there a FOREIGN KEY constraint between Department and Location. If so, then inserting a deparment requires an existing Location. Location is linked to Department with Location_ID
Timestamps syntax should be as a string '12/12/2010' not without the '-signs