Trying insert data to database by SQL - sql

I want to insert data on table dbo.batch but but I have an error message when i tried to insert the data
error message
Msg 110, Level 15, State 1, Line 3
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

You have 10 columns in your statement but 11 values to enter. Therefore the error message

You have 10 columns and you are inserting 11 values check for the valid columns values you need and remove other.
I guess the last 2nd column value is the one you should remove

Related

Insert from select

Based on this topic I've encountered a problem with insertions.
My Tests table contains:
TestID Name
1 test_insert_film
2 test_insert_writer
3 test_insert_location
4 test_delete_film
5 test_delete_writer
6 test_delete_location
I want to insert into my TestTables the id's of the tests with the following sequence:
INSERT INTO TestTables(TestID)
SELECT TestID
FROM Tests
But I get:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'TableID', table 'FilmS.dbo.TestTables'; column does not allow nulls. INSERT fails. The statement has been terminated.
TestTables contains 4 columns, one of them being TestID. Why isn't this working?
The column TableID (!) in your table TestTables is not allowed NULL values! This column is not in the list of columns to be filled upon the INSERT, so the default value assumed is NULL. This is why you get the error.
You may need something like:
INSERT INTO TestTables(TestID, TableID)
SELECT TestID, '' FROM Tests
To fill the TableID column with a default value. Maybe also other columns in the TestTables table are affected and need to be treated similarly.
PS: You could also modify the the TestTables definition to provide a default value for the respective columns. If you do so you can leave the above statement as it is.

Find the exact line number of error in sql script

I am trying to insert nearly 1000 records for each insert statement in to a table called ZonePlaces. This table is having an Index.
When i try to run the script for insert on this table it throws error
Msg 2601, Level 14, State 1, Line 56
Cannot insert duplicate key row in object 'dbo.ZonePlaces' with unique index 'IX_ZonePlaces'.
Line 56 is start of Insert statement.
I know i am inserting something duplicate value but is there a way to find which exact line number has the error?
Hope i am clear.

INSERT INTO a Table

I'm trying to simply adding some data in a table but I receive an error:
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Here what I'm using
INSERT INTO dbo.ReModalities
(ModalityId, Name, Description)
VALUES
(
1,'A','A.',
2,'B','B.'
);
This should give you an idea of the Table Column
INSERT INTO [XXX].[dbo].[ReModalities]
([ModalityId]
,[Name]
,[Description])
VALUES
(<ModalityId, int,>
,<Name, nvarchar(64),>
,<Description, nvarchar(256),>)
GO
Also I would like to know if there is way I can avoid typing the IDs (the table has PK so they should be created automatically) many thanks!
Each row of the values statement should be enclosed in parenthesis. Try:
VALUES
(1,'A','A.'),
(2,'B','B.');
If the ID has a default value or is an identity, you can omit it:
insert dbo.ReModalities
(Name, Description)
values ('A','A.'),
('B','B.');

I want to insert multiple rows in a table using sql server 2005

I am wanting to insert rows into a four column table. I have inserted rows in the first two columns. However, when I try adding to the third and fourth column I receive the following error,
"Msg 8152, Level 16, State 4, Line 1 String or binary data would be
truncated. The statement has been terminated"
I am using the following query:
USE DB
GO
INSERT INTO dbo.Table (Column1, Column2, Column3,Column4)
VALUES ('data','data','data','data')
Check if one of your strings exceeds the length of the datafield.
For example, if you are inserting a string of length 50 into a varchar(20), you would recieve this error.
You are receiving that error because your columns are not capable of storing a value that large. You need to either shorten the length of your strings being inserted or increased the length on the column structure.

I am Trying to insert this data into SQL

I am trying to create the correct values for the tables I created. This is the code:
INSERT DEPARTMENTS
(Department_Id,Department_Name,Manager_Id,Location_Id)
VALUES
('D0001,D0002,D0003','Think Tank,Creators,Marketers',NULL,'L0001,L0002,L0003')
GO
INSERT EMPLOYEES
(Employee_Id,First_Name,Last_Name,Email,PhoneNumber,Hire_Date,Manager_ID,Department_Id)
VALUES
('E0001,E0002,E0003,E0004,E0005,E0006,E0007','Joe,John,Sue,Tina,Ike,Big,Speedy','Blow,Doe,Happy,Turner,Turner,Bird,Gonzales',NULL,NULL,2010/06/25,2010/06/25,2010/06/25,2010/06/25,2010/06/25,2010/06/25,2010/06/25,NULL,NULL)
GO
INSERT LOCATIONS
(Location_ID,Postal_Code,City,State_Province,Country)
VALUES
('L0001,L0002','19121,08618','Philadelphia,Trenton','PA,NJ','USA,USA')
This is the error message:
Msg 8152, Level 16, State 14, Line 2
String or binary data would be truncated.
The statement has been terminated.
Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.
I would like to know what I am doing wrong and the correct code. Can anyone help me out? Thanks
INSERT EMPLOYEES has 8 columns and WAY more than 8 values in the VALUES part of the statement.
I don't think you understand how SQL works.
As an hint, this:
INSERT LOCATIONS
(Location_ID,Postal_Code,City,State_Province,Country)
VALUES
('L0001,L0002','19121,08618','Philadelphia,Trenton','PA,NJ','USA,USA')
should look like this:
INSERT LOCATIONS
(Location_ID,Postal_Code,City,State_Province,Country)
VALUES
('L0001','19121','Philadelphia','PA','USA');
INSERT LOCATIONS
(Location_ID,Postal_Code,City,State_Province,Country)
VALUES
('L0002','08618','Trenton','NJ','USA');
You need one insert statement for every row you are inserting.
The DEPARTMENTS and EMPLOYEES is wrong in this same way as well.
You can do multiple inserts like this though,
INSERT INTO LOCATIONS
(Location_ID,Postal_Code,City,State_Province,Country)
VALUES
('L0001','19121','Philadelphia','PA','USA'),
('L0002','08618','Trenton','NJ','USA');