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

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.

Related

Why am I getting "Error converting data type varchar to int."

I am creating a mock database for a fictional gym. I have 3 relevant tables: Program, Workouts and Exercises (see below). Each Program contains one or many Workouts and each Workout contains one or many Exercises. I am trying to create a stored procedure to add rows to these three tables. I keep getting the error "Error converting data type varchar to int.". I can't understand why because the variable datatypes all match the datatypes in the tables (as far as I can see).
CREATE TABLE [Program]
(
[ProgramID] INT IDENTITY(1,1),
[MemberNumber] INT,
[TrainerID] INT,
[ProgramStartDate] DATE,
[TrainerReviewDate] DATE,
[Active] CHAR(1),
PRIMARY KEY ([ProgramID]),
FOREIGN KEY ([MemberNumber])
REFERENCES [Members] ([MemberNumber])
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY ([TrainerID])
REFERENCES [Trainers] ([TrainerID])
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE [Workouts]
(
[WorkoutID] INT IDENTITY(1,1),
[Description] VARCHAR(50),
[Day] VARCHAR(10),
[ProgramID] INT,
PRIMARY KEY ([WorkoutID]),
FOREIGN KEY ([ProgramID])
REFERENCES [Program] ([ProgramID])
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE [Exercise]
(
[ExerciseID] INT IDENTITY(1,1),
[ExerciseType] VARCHAR(30),
[Equipment] VARCHAR(30),
[Sets] INT,
[Reps] INT,
[WorkoutID] INT,
PRIMARY KEY ([ExerciseID]),
FOREIGN KEY ([WorkoutID])
REFERENCES [Workouts] ([WorkoutID])
ON DELETE SET NULL ON UPDATE CASCADE
)
/*Description: This stored procedure adds a new program with workout/exercise details. */
CREATE PROCEDURE usp_New_Program_4 (
#MemberNumber int,
#TrainerID int,
#ProgramStartDate date,
#TrainerReviewDate date,
#Active char(1),
#Description varchar(50),
#Day varchar(10),
#ProgramID int = SCOPE_IDENTITY,
#ExerciseType_1 varchar(30),
#Equipment_1 varchar(30),
#Sets_1 int,
#Reps_1 int,
#ExerciseType_2 varchar(30),
#Equipment_2 varchar(30),
#Sets_2 int,
#Reps_2 int,
#ExerciseType_3 varchar(30),
#Equipment_3 varchar(30),
#Sets_3 int,
#Reps_3 int,
#ExerciseType_4 varchar(30),
#Equipment_4 varchar(30),
#Sets_4 int,
#Reps_4 int,
#WorkoutID int = SCOPE_IDENTITY
)
AS
SET xact_abort ON
BEGIN
INSERT INTO Program (MemberNumber, TrainerID, ProgramStartDate, TrainerReviewDate, Active)
VALUES (#MemberNumber, #TrainerID, #ProgramStartDate, #TrainerReviewDate, #Active);
INSERT INTO Workouts (Description, Day, ProgramID)
VALUES (#Description, #Day, SCOPE_IDENTITY());
INSERT INTO Exercise (ExerciseType, Equipment, Sets, Reps, WorkoutID)
VALUES (#ExerciseType_1, #Equipment_1, #Sets_1, #Reps_1, SCOPE_IDENTITY()),
(#ExerciseType_2, #Equipment_2, #Sets_2, #Reps_2, SCOPE_IDENTITY()),
(#ExerciseType_3, #Equipment_3, #Sets_3, #Reps_3, SCOPE_IDENTITY()),
(#ExerciseType_4, #Equipment_4, #Sets_4, #Reps_4, SCOPE_IDENTITY())
END;
GO
/*Test for usp_New_Program_4
Result: Failed - Error converting data type varchar to int*/
Exec usp_New_Program_4 21,3,'2020-06-06','2020-07-07','Y','Chest & Arms',SCOPE_IDENTITY,'Bench
Press','50kg Barbell',3,6,'Press Ups','Floor Mat',3,15,'Bicep Curls','15kg
Dumbells',3,6,'Tricep Extensions','15kg Dumbells',3,6,SCOPE_IDENTITY
Go
It looks like you may have missed a field in your line to execute the stored procedure. I quickly copied the stored proc input values into a spreadsheet followed by the values in your test line that throws the error. Have a look, it looks like you're missing perhaps the Day value? After that is entered it should line everything up correctly. I marked up the first line that after a quick glance looks like it is causing the error (others lower will too though, until the missing value is fixed).
Basic debugging would be to check your parameters... And if we do that...
Exec usp_New_Program_4 21, --#MemberNumber int
3, --#TrainerID int
'2020-06-06', --#ProgramStartDate date
'2020-07-07', --#TrainerReviewDate date
'Y', --#Active char(1)
'Chest & Arms', --#Description varchar(50)
SCOPE_IDENTITY, --#Day varchar(10)
'Bench
Press', --#ProgramID int
'50kg Barbell', --#ExerciseType_1 varchar(30)
3, --#Equipment_1 varchar(30)
6, --#Sets_1 int
'Press Ups', --#Reps_1 int
'Floor Mat', --#ExerciseType_2 varchar(30)
3, --#Equipment_2 varchar(30)
15, --#Sets_2 int
'Bicep Curls', --#Reps_2 int
'15kg
Dumbells', --#ExerciseType_3 varchar(30)
3, --#Equipment_3 varchar(30)
6, --#Sets_3 int
'Tricep Extensions', --#Reps_3 int
'15kg Dumbells', --#ExerciseType_4 varchar(30)
3, --#Equipment_4 varchar(30)
6, --#Sets_4 int
SCOPE_IDENTITY --#Reps_4 int
Pretty clear now. The value 'Bench
Press' is not an int. Neither are 'Press Ups'
, 'Bicep Curls' or 'Tricep Extensions'.
This is why using the format EXEC PROC {Proc Name} #{Parameter Name} = {Parameter Value} is really important for any procedures that have more than 1 or 2 parameters; you don't make simple mistakes like missing a parameter out.
In this case, you clearly missed out a value for #Day (as I assume you wanted to pass SCOPE_IDENTITY for #ProgramID), and thus all your parameters after that are wrong. The SP still ran fine though, as your last parameter (#WorkoutID) has a default value, and so didn't "mind" being omitted.

sql conversion of a varchar data type to a datetime data type out-of-range

I am trying to add to a table a group of values on of them is a date.
When trying to add a date i receive the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
i have tried to run the following query's:
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CAST('27/07/2017 10:24:13' AS DATETIME),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CONVERT(VARCHAR,'27/07/2017 10:24:13',13),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values(CONVERT(VARCHAR,'27-07-2017 10:24:13.000',113),'0','Alpha Day','0','Alpha')
INSERT INTO BoxEntries (Date,Value,Description,Empid,EmpName) Values('27-07-2017 10:24:13.000','0','Alpha Day','0','Alpha')
I have confirmed and 13 or 113 is the time of datatime i want in SQL.
The wired part is that when i try to directly add to the database the values it doesn't give me any errors.
The table:
CREATE TABLE [dbo].[BoxEntries] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Date] DATETIME NOT NULL,
[Value] MONEY NOT NULL,
[Description] VARCHAR (MAX) NOT NULL,
[EmpId] INT NOT NULL,
[EmpName] VARCHAR (MAX) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC) );
mssql format of datetime is 'YYYY-MM-DD HH:MM:SS.mmm'
https://learn.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql
so correct query for your case might be:
INSERT INTO BoxEntries ([Date],Value,Description,Empid,EmpName)
Values('2017-07-27 10:24:13.000', '0', 'Alpha Day', '0', 'Alpha');

SQL Insert Out of Sync

I have a bit of SQL here which is throwing an error:
DROP TABLE HACP_TEMP_PIC_HCV_Imported;
CREATE TABLE HACP_TEMP_PIC_HCV_Imported
(
HeadSSN varchar(255) NOT NULL,
HeadFName varchar(255) NOT NULL,
HeadMName varchar(255),
HeadLName varchar(255) NOT NULL,
ModifiedDate varchar(255) NOT NULL,
ActionType varchar(255) NOT NULL,
EffectiveDate varchar(255) NOT NULL
);
BULK INSERT HACP_TEMP_PIC_HCV_Imported
FROM 'C:\Work\MTWAdhocReport.csv'
WITH
(
FIRSTROW = 11,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
ERRORFILE = 'C:\Work\Import_ErrorRows_HCV.csv',
TABLOCK
);
UPDATE HACP_TEMP_PIC_HCV_Imported
SET HeadSSN = REPLACE(HeadSSN, '"', ''),
HeadFName = REPLACE(HeadFName, '"', ''),
HeadMName = REPLACE(HeadMName, '"', ''),
HeadLName = REPLACE(HeadLName, '"', ''),
ModifiedDate = REPLACE(ModifiedDate, '"', ''),
ActionType = REPLACE(ActionType, '"', ''),
EffectiveDate = REPLACE(REPLACE(EffectiveDate, '"', ''),',','');
DROP TABLE HACP_PIC_HCV_Imported;
CREATE TABLE HACP_PIC_HCV_Imported
(
HeadSSN varchar(255) NOT NULL,
HeadFName varchar(255) NOT NULL,
HeadMName varchar(255),
HeadLName varchar(255) NOT NULL,
ModifiedDate varchar(255) NOT NULL,
ActionType int NOT NULL,
EffectiveDate varchar(255) NOT NULL
);
INSERT INTO HACP_PIC_HCV_Imported(HeadSSN, HeadFName, HeadMName, HeadLName, ModifiedDate, ActionType, EffectiveDate)
SELECT
LTRIM(HeadSSN),
LTRIM(HeadFName),
LTRIM(HeadMName),
LTRIM(HeadLName),
LTRIM(ModifiedDate),
CONVERT(int, LTRIM(ActionType)),
LTRIM(EffectiveDate)
FROM
HACP_TEMP_PIC_HCV_Imported;
Stepping through this, creating the temp table and importing the CSV into it works fine. Updating the table to remove quotes and a trailing comma from the EffectiveDate column works. Creating the new table-proper works.
When trying to copy the data into the second table (and converting ActionType into an INT), I get this error message:
Conversion failed when converting the varchar value '4/07/2016' to data type int.
That data is the second row value in ModifiedDate, so the columns are apparently getting out of sync after importing the first row. I have double-checked that all of the data is in the proper columns after being imported into the temp table initially.
Any thoughts? I feel like I'm missing something obvious.
Your code suggests that you are using "proper" CSV format, which allows fields to be enclosed in double quotes. These delimited fields can contain commas. This is the format produced and read by Excel.
My guess is that you have a comma in such a delimited field and this is throwing off the import.
But, this format is not read properly by bulk insert. Ironically, (at least) one database does import the CSV formatted files with commas in the fields.
In the past when I've had this problem, it has only been on smallish files. I simply loaded the data into Excel and then saved in out using tabs or vertical bars as delimiters. This solved the problem in my case.
I'm not sure if there is a more advanced solution now. But I'm pretty sure your problem is that some fields have embedded commas in the text fields.

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.

Sql Server - Insufficient result space to convert uniqueidentifier value to char

I am getting below error when I run sql query while copying data from one table to another,
Msg 8170, Level 16, State 2, Line 2
Insufficient result space to convert
uniqueidentifier value to char.
My sql query is,
INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
NEWID(),
first_name,
last_name
FROM dbo.tmp_cust_info
My create table scripts are,
CREATE TABLE [dbo].[cust_info](
[uid] [varchar](32) NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
CREATE TABLE [dbo].[tmp_cust_info](
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
I am sure there is some problem with NEWID(), if i take out and replace it with some string it is working.
I appreciate any help. Thanks in advance.
A guid needs 36 characters (because of the dashes). You only provide a 32 character column. Not enough, hence the error.
You need to use one of 3 alternatives
1, A uniqueidentifier column, which stores it internally as 16 bytes. When you select from this column, it automatically renders it for display using the 8-4-4-4-12 format.
CREATE TABLE [dbo].[cust_info](
[uid] uniqueidentifier NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
2, not recommended Change the field to char(36) so that it fits the format, including dashes.
CREATE TABLE [dbo].[cust_info](
[uid] char(36) NOT NULL,
[first_name] [varchar](100) NULL,
[last_name] [varchar](100) NULL)
3, not recommended Store it without the dashes, as just the 32-character components
INSERT INTO dbo.cust_info (
uid,
first_name,
last_name
)
SELECT
replace(NEWID(),'-',''),
first_name,
last_name
FROM dbo.tmp_cust_info
I received this error when I was trying to perform simple string concatenation on the GUID. Apparently a VARCHAR is not big enough.
I had to change:
SET #foo = 'Old GUID: {' + CONVERT(VARCHAR, #guid) + '}';
to:
SET #foo = 'Old GUID: {' + CONVERT(NVARCHAR(36), #guid) + '}';
...and all was good. Huge thanks to the prior answers on this one!
Increase length of your uid column from varchar(32) ->varchar(36)
because guid take 36 characters
Guid.NewGuid().ToString() -> 36 characters
outputs: 12345678-1234-1234-1234-123456789abc
You can try this. This worked for me.
Specify a length for VARCHAR when you cast/convert a value..for uniqueidentifier use VARCHAR(36) as below:
SELECT Convert (varchar(36),NEWID()) AS NEWID
The default length for VARCHAR datatype if we don't specify a length during CAST/CONVERT is 30..
Credit : Krishnakumar S
Reference : https://social.msdn.microsoft.com/Forums/en-US/fb24a153-f468-4e18-afb8-60ce90b55234/insufficient-result-space-to-convert-uniqueidentifier-value-to-char?forum=transactsql