Type Mismatch Null value SQL - 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.

Related

SQL Server DDL "error Incorrect syntax near ')'."

I have the follow DDL query,
query failed:
CREATE TABLE "auxilio_saude_v2"."Solicitacao"
(
"IdSolicitacao" int NOT NULL IDENTITY(1,1),
"Descricao" varchar(-1) NOT NULL,
"Estado" varchar(255) NOT NULL,
"MotivoRecusa" varchar(255),
"BeneficiarioDboIdVinculo" int NOT NULL,
"CadastroDboIdVinculo" int NOT NULL,
"CadastroData" datetime CONSTRAINT "DF_cd6a13f4e589eeff45ef3b3a8ea" DEFAULT getdate(),
"InicioData" datetime CONSTRAINT "DF_4ae713eccf3d4346f40d25c8fe1" DEFAULT getdate(),
"FinalData" datetime,
CONSTRAINT "PK_348326c6affdf7e7d271ac6d672" PRIMARY KEY("IdSolicitacao")
)
But, it's giving me an error when exectuted:
ErrorMessageToken {
name: 'ERROR',
event: 'errorMessage',
number: 102,
state: 1,
class: 15,
message: "Incorrect syntax near ')'.",
serverName: 'SRVALDESENV',
procName: '',
lineNumber: 1
}
I'm no seeing the problem. Could somebody help me?
This is because of this line of your code:
"Descricao" varchar(-1) NOT NULL,
You can not use a negative value for varchar.
This is the syntax of VARCHAR :
varchar [ ( n | max ) ]
Use n to define the string size in bytes and can be a value from 1 through 8,000 or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).
Learn more

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

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".

Bulk load: An unexpected end of file was encountered in the data file

I am using SQL Server Express 2008
When I'm trying load data from txt file in to this table
create table Clients
(
ClientID int not null IDENTITY (9000,1),
LastName varchar (30)not null,
FirsName varchar (30)not null,
MidInitial varchar (3),
DOB date not null,
Adress varchar (40) not null,
Adress2 varchar (10),
City varchar (40) not null,
Zip int not null,
Phone varchar (30) ,
CategCode varchar (2) not null,
StatusID int not null,
Hispanic BINARY default 0,
EthnCode varchar(3) ,
LangID int,
ClientProxy varchar (200),
Parent varchar (40),
HshldSize int default 1,
AnnualHshldIncome INT,
MonthlyYearly VARCHAR(7) ,
PFDs INT,
WIC BINARY default 0,
Medicaid BINARY default 0,
ATAP BINARY default 0,
FoodStamps BINARY default 0,
AgencyID int not null,
RoutID int ,
DeliveryNotes varchar (200),
RecertificationDate date not null,
Notes text,
Primary Key (ClientID)
);
I use
SET IDENTITY_INSERT Clients2 ON;
BULK INSERT Clients2
FROM 'c:\Sample_Clients.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\r\n'
)
SQL Server Express trows me errors
Msg 545, Level 16, State 1, Line 2
Explicit value must be specified for identity column in table 'Clients' either when IDENTITY_INSERT is set to ON or when a replication user is inserting into a NOT FOR REPLICATION identity column.
File has only one line (for now just sample data) I check it many times its one line
Data looks like this
13144,Vasya,Pupkin,,1944-10-20,P.O. Box 52,,Wrna,99909,(907) 111-1111,SR,4,0,W,1,,,3,1198,month,0,0,1,0,1,45,,,2011-04-27
Any ideas how to fix this problem?
You need the parameter KEEPIDENTITY in your bulk insert statement. This is required to retain identity values in the load.
BULK INSERT Clients2 FROM 'c:\Sample_Clients.txt'
WITH ( KEEPIDENTITY, FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n'
)
I also think you will have a problem because you have no data or placeholder for the Notes column. A comma added to the end of the file should address this.