INSERT INTO a Table - sql

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.');

Related

Insert query: Column name or number of supplied values does not match table definition

This is a table that auto increments the ID, takes the time input, and sets the default of the total column as zero and the date column with getdate()
CREATE TABLE OrderPlaced (
OrderID bigint IDENTITY(1,1) PRIMARY KEY,
PlacedAt time NOT NULL,
PlacedOn DATE NOT NULL default getdate(),
Total MONEY default 0
)
So the only value that I have to insert is the time.
insert into OrderPlaced values ('13:40');
However on insertion SQL Server gives me this error:
Column name or number of supplied values does not match table definition.
The error is telling you the problem. Your table, OrderPlaced, has four columns, but you only supply one column in the INSERT and don't tell the instance what column to insert it into.
For your table, if you don't specify the columns then it will expect values for PlacedAt, PlacedOn and Total; it won't expect a value for OrderID as it is an IDENTITY and you haven't turned on IDENTITY_INSERT. As the instance is expecting 3 columns, and you only provide one, you get an error telling you that the number of columns don't match (3 != 1).
If you only want to insert a value into PlacedAt, and allow the rest of the columns use their DEFAULT values then define in your INSERT clause you only want to provide a value for PlacedAt:
INSERT INTO dbo.OrderPlaced(PlacedAt)
VALUES ('13:40');
Or, if you are (oddly) opposed to defining the columns in the INSERT clause, then tell the instance that the other 2 columns need their DEFAULT values.
INSERT INTO dbo.OrderPlaced
VALUES ('13:40',DEFAULT, DEFAULT);
Though I don't recommend this solution, be explicit as then if you change definition of the table, the above statement will fail.
You need to specify the column name as well, like this:
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
So it will be like this:
insert into OrderPlaced values ('13:40');

Trying insert data to database by 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

Trying to insert values into a table where some values will be select statements whereas others will be hardcoded

My select into query would start like this:
insert into camrule (HCHARGECODE, htenant, dtfrom, dtto, IESTIMATETYPE, destimated, imaxpermo)
My hchargecode would be a hardcoded value of 174, my htenant would be based on a select statment (ex. select htenant from tableX), and so on. How can I hardcode the columns and have the other values from the select statments added to my camrule table?
Also, this is for multiple rows, not just a single row to be inserted.
I've tried creating a temp table with the hardcoded values, but am getting an error. I was hoping I could insert the columns from this temp table into my camrule table.
error message
Use the Values keyword to get this done...
insert into camrule (HCHARGECODE, htenant, dtfrom, dtto, IESTIMATETYPE, destimated, imaxpermo)
values(174,(select htenant from tableX), and so on...
This way allows you to select values from different tables, as opposed to just adding hard-coded columns to a Select statement from a single source table.
You can do as simple as:
insert into camrule (
HCHARGECODE, htenant, dtfrom, dtto,
IESTIMATETYPE, destimated, imaxpermo
)
select
174, htenant, dtfrom, dtto,
IESTIMATETYPE, destimated, imaxpermo
from tableX

SQL Server Insert Example

I switch between Oracle and SQL Server occasionally, and often forget how to do some of the most trivial tasks in SQL Server. I want to manually insert a row of data into a SQL Server database table using SQL. What is the easiest way to do that?
For example, if I have a USERS table, with the columns of ID (number), FIRST_NAME, and LAST_NAME, what query do I use to insert a row into that table?
Also, what syntax do I use if I want to insert multiple rows at a time?
To insert a single row of data:
INSERT INTO USERS
VALUES (1, 'Mike', 'Jones');
To do an insert on specific columns (as opposed to all of them) you must specify the columns you want to update.
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
VALUES ('Stephen', 'Jiang');
To insert multiple rows of data in SQL Server 2008 or later:
INSERT INTO USERS VALUES
(2, 'Michael', 'Blythe'),
(3, 'Linda', 'Mitchell'),
(4, 'Jillian', 'Carson'),
(5, 'Garrett', 'Vargas');
To insert multiple rows of data in earlier versions of SQL Server, use "UNION ALL" like so:
INSERT INTO USERS (FIRST_NAME, LAST_NAME)
SELECT 'James', 'Bond' UNION ALL
SELECT 'Miss', 'Moneypenny' UNION ALL
SELECT 'Raoul', 'Silva'
Note, the "INTO" keyword is optional in INSERT queries. Source and more advanced querying can be found here.
Here are 4 ways to insert data into a table.
Simple insertion when the table column sequence is known.
INSERT INTO Table1 VALUES (1,2,...)
Simple insertion into specified columns of the table.
INSERT INTO Table1(col2,col4) VALUES (1,2)
Bulk insertion when...
You wish to insert every column of Table2 into Table1
You know the column sequence of Table2
You are certain that the column sequence of Table2 won't change while this statement is being used (perhaps you the statement will only be used once).
INSERT INTO Table1 {Column sequence} SELECT * FROM Table2
Bulk insertion of selected data into specified columns of Table2.
.
INSERT INTO Table1 (Column1,Column2 ....)
SELECT Column1,Column2...
FROM Table2
I hope this will help you
Create table :
create table users (id int,first_name varchar(10),last_name varchar(10));
Insert values into the table :
insert into users (id,first_name,last_name) values(1,'Abhishek','Anand');
For example, "person" table has "id" IDENTITY column as shown below:
CREATE TABLE person (
id INT IDENTITY, -- Here
name NVARCHAR(50),
age INT,
PRIMARY KEY(id)
)
Then, we don't need to manually put a value to "id" IDENTITY column when inserting a row:
INSERT INTO person VALUES ('John', 27) -- The value for "id" is not needed
And, we can also insert multiple rows without the values for "id" IDENTITY column:
INSERT INTO person VALUES ('John', 27), ('Tom', 18)

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');