Error String or binary data would be truncated - sql

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.

Related

Msg 207, Level 16, State 1 "Invalid column name 'Name'"

I'm trying to drop and recreate a table with a new schema in SQL Server, and then insert some test data into it to validate that it's working correctly. I have the following DDL:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
and the following insert statement:
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Whenever I attempt to insert the aforementioned row, I get an error:
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Name'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'Description'.
I know the table is there since I can SELECT * FROM Product and get an empty result set with the correct columns and no errors... I just can't insert into it for some reason. Any help would be greatly appreciated!
As per Martin's answer, I did some more research and he is correct. The schema manipulation needs to be done in separate batches (I simply added a GO after dropping my old table and after creating my new one with a different schema). A more detailed answer can be found here: "Invalid column name" error when calling insert after table created
Maybe try refreshing the cache. Easy way is to press Ctrl + shift + R
Or
Edit > IntelliSense > Refresh Local Cache
If doesnt work, then use qualified names [dbo].table or username.tablename etc
More here: http://msdn.microsoft.com/en-us/library/ms174205.aspx
For me, your query looks good, and i test it successfully.
Try adding schema name and put all column name inside brackets:
CREATE TABLE [dbo].[Product](
[ID] int IDENTITY(1,1) PRIMARY KEY,
[Name] VARCHAR(100) NOT NULL,
[Description] VARCHAR(200) NULL,
[Modified] DATETIME NOT NULL DEFAULT(GETUTCDATE()),
[ModifiedBy] VARCHAR(32) NOT NULL
);
INSERT INTO [dbo].[Product]([Name], [Description], [Modified], [ModifiedBy])
VALUES('Test', 'Description', GETUTCDATE(), 'Me');
Don't do anything just try to insert (' ')single quotes instead of (") quotes.. or if you insert data with ('')single quotes try for (") double quotes it's really helping you.

SQL Server 2012 - Queries working executed one on one, but not in a group

I have been trying to execute these two queries together:
ALTER TABLE afm.owned_properties_rpt_table
ALTER COLUMN bl_id CHAR(8) NOT NULL;
ALTER TABLE afm.owned_properties_rpt_table
ADD CONSTRAINT owned_properties_rpt_table_PK PRIMARY KEY (bl_id);
But I'm getting this error:
Mens. 8111, Nivel 16, Estado 1, Línea 3
Cannot define PRIMARY KEY constraint on nullable column in table
'owned_properties_rpt_table'.
Mens. 1750, Nivel 16, Estado 0, Línea 3
Could not create constraint. See previous errors.
It seems that somehow, the second line is being executed before the first one finishes.
I have tried changing semicolons by goes, using a begin transaction/commit transaction structure, and creating an auxiliary column where I copied the data in bl_id and then dropped the old column, all of them without success.
The SQL script needs to be executed on a client's server (where I can not intervene), so dividing the code is not an alternative.
I am sorry if I am missing something elementary, I have also searched for the same problem during several hours without success.
Thanks for your help.
Try this
ALTER TABLE afm.owned_properties_rpt_table ALTER COLUMN bl_id CHAR(8) NOT NULL default 'sometest';
GO
ALTER TABLE afm.owned_properties_rpt_table ADD CONSTRAINT owned_properties_rpt_table_PK PRIMARY KEY (bl_id);

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

Inserting Char value into SQL table

I created a SQL table an enforced check constraints on it, but now when I try to insert data I get an error message.
create table BranchTel
(
BrRegNo varchar(10) REFERENCES Branch(BrRegNo),
TelNo char(12)
PRIMARY KEY(BrRegNo)
)
ALTER TABLE BranchTel Add Constraint BranchTelTelNo
Check(TelNo LIKE '[0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
Insert statement
insert into BranchTel values('BG-205','940112571963')
Error message
The INSERT statement conflicted with the CHECK constraint "BranchTelTelNo". The conflict occurred in database "StudentDetails", table "dbo.BranchTel", column 'TelNo'.
The statement has been terminated.
Insert statement
insert into BranchTel values('BG-205','94-011-2571963')
Error message
String or binary data would be truncated.
The statement has been terminated.
Please help me
Your check constraint is 14 characters long (you need to count the - as well), while the field size is 12.
Additionally, 940112571963 does not conform to the pattern xx-xxx-xxxxxxx you have defined in your check constraint.
You need to change the field size to 14 and when inserting make sure the dashes are in the right place:
insert into BranchTel values('BG-205','94-011-2571963')
Insert statement insert into BranchTel values('BG-205','94-011-2571963') Error message String or binary data would be truncated. The statement has been terminated.
Here the value 94-011-2571963 length is greater than 12 which obviously violates the check constraint.

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