Bulk insert null into table from csv file in SQL Server - sql

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.

Related

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

Bulk load data conversion error (EMP_TITLE)

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

Type Mismatch Null value SQL

I'm having a problem with Null values in my CREATE TABLE for some reason...It's giving me this error message:
Msg 4864, Level 16, State 1, Line 73
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 4, column 7 (Manager).
Here's my code and the data I'm using:
CREATE TABLE SalesReps
(
EmpNum SMALLINT NOT NULL ,
Name VARCHAR(20) NOT NULL,
Age TINYINT NOT NULL,
RepOffice TINYINT NULL,
Title VARCHAR(20) NULL,
HireDate DATE,
Manager INT NULL,
Quota MONEY NULL,
Sales MONEY DEFAULT 0
)
BULK INSERT SalesReps
FROM 'C:\Users\Steve\Desktop\salesreps.txt'
WITH ( FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n')
Data:
105|Bill Adams|37|13|Sales Rep|02/12/88|104|350000.00|367911.00
109|Mary Jones|31|11|Sales Rep|10/12/89|106|300000.00|392725.00
102|Sue Smith|48|21|Sales Rep|12/10/86|108|350000.00|474050.00
106|Sam Clark|52|11|VP Sales|06/14/88|NULL|275000.00|299912.00
104|Bob Smith|33|12|Sales Mgr|05/19/87|106|200000.00|142594.00
101|Dan Roberts|45|12|Sales Rep|10/20/86|104|300000.00|305673.00
110|Tom Snyder|41|NULL|Sales Rep|01/13/90|101|NULL|75985.00
108|Larry Fitch|62|21|Sales Mgr|10/12/89|106|350000.00|361865.00
103|Paul Cruz|29|12|Sales Rep|03/01/87|104|275000.00|286775.00
107|Nancy Angelli|49|22|Sales Rep|11/14/88|108|300000.00|186042.00
Can anyone please help? I've looked at the other mismatch pages but they aren't helping much. I've been stuck on this for days.
The bulk insert on row 4 includes a value NULL, but I think that SQL Server interprets this as string containing 'NULL'. You can try to change row 4 with that :
106|Sam Clark|52|11|VP Sales|06/14/88||275000.00|299912.00
You will also have the same problem on row 7, your column Quota which expects a MONEY type, but a string containing NULL is provided.

Error when inserting into a simple two column table with SQL Server 2012?

I am running the following:
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ("0918fb0f-79c0-4298-b184-9a754dc5c30e", "527ddbd5-14d3-4fb9-a7ae-374e66f635d4")
Here's my table:
CREATE TABLE [dbo].[AspNetUserRoles] (
[RoleId] NVARCHAR (128) NOT NULL,
[UserId] NVARCHAR (128) NOT NULL
);
However it's giving me an error saying:
Msg 207, Level 16, State 1, Line 2
Invalid column name '0918fb0f-79c0-4298-b184-9a754dc5c30e'.
Is there something wrong with the way I have coded the insert ?
Put the values in '' not "":
insert into [authentication].[dbo].[AspNetUserRoles] ("RoleId","UserId")
values ('0918fb0f-79c0-4298-b184-9a754dc5c30e',
'527ddbd5-14d3-4fb9-a7ae-374e66f635d4');
The problem is that the values with "" around them are treated as identifiers not literals. Like in "RoleId","UserId".

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