I am Trying to insert this data into SQL - 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');

Related

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

Invalid Column Name when Inserting Data

I have a local installation of SQLExpress, and accessing it using SQL Server Management Studio.
I created a new database BirdSite in SSMS, with a single table TMasterCountry, with these columns:
Id (int)
CountryName (varchar(50))
where Id is the primary, auto-incrementing key.
When I use the Script table as... option in the object explorer, I am able to view the empty table with this bit of SQL:
USE [BirdSite]
GO
SELECT [Id]
,[CountryName]
FROM [dbo].[TMasterCountry]
GO
without any issues.
However, using the same option to try inserting some data, brings up this SQL:
USE [BirdSite]
GO
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
(<CountryName, varchar(50),>)
GO
So I changed the line under VALUES to
(CountryName, 'test')
But when I try running the code, I get these errors:
Msg 207, Level 16, State 1, Line 4
Invalid column name 'CountryName'.
Msg 110, Level 15, State 1, Line 4
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.
Does anyone know what might be going on here? The column clearly does exist, as I can SELECT the data just fine.
Following some similar questions on SO, I have tried restarting SSMS and also tried refreshing the local Intellisense cache, but with no luck. I also tried surrounding CountryName in square brackets but with the same result.
Change it to:
USE [BirdSite]
GO
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')
GO
<CountryName, varchar(50),> refers to name of column and it's datatype.
Syntax error; Should be
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')
You need to replace the entire content between < and > with the value:
INSERT INTO [dbo].[TMasterCountry]
([CountryName])
VALUES
('test')

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.