I am writing a very basic insert statement. I noticed that the error message "Invalid column name" appears if the VALUE is a character besides a number.
The data type is VARCHAR(50) on all three columns.
Database: MS SQL 2012
Query:
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,john
,main)
GO
ERROR:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'john'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'main'.
Change your query to this
INSERT INTO [test-db].[dbo].[Computer]
([id]
,[name]
,[office])
VALUES
(123
,'john'
,'main')
GO
The varchar needs quotes around it to work.
Related
I am trying to run the following T-SQL code which contains 8160 individual numbered accounts and receive the the following errors, with Line 10 being the first instance of INSERT and line 8162 being the last instance of INSERT
Code:
SELECT
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
FROM
BIDW_Staging.BPFCentrac.ACCEXTPF
WHERE
AXACNO IN (
INSERT INTO ##AN_INSERT VALUES ('0000000000000000')
INSERT INTO ##AN_INSERT VALUES ('1111111111111111')
INSERT INTO ##AN_INSERT VALUES ('2222222222222222')
I get these errors:
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'INSERT'
Msg 102, Level 15, State 1, Line 8162
Incorrect syntax near ')'
First you need to insert your data into your ##AN_INSERT table. Each INSERT is a separate statement and should be terminated with a ;.
INSERT INTO ##AN_INSERT VALUES ('0000000000000000');
INSERT INTO ##AN_INSERT VALUES ('1111111111111111');
INSERT INTO ##AN_INSERT VALUES ('2222222222222222');
Then you can select using this table:
select
AXACNO AS [AGREEMENT],
AX15C1 AS [BLOCKCODE]
from BIDW_Staging.BPFCentrac.ACCEXTPF
Where AXACNO in
(SELECT <whatevercolumnthisis> FROM ##AN_INSERT);
I am trying to insert into table using next value from sequence. This is causing an error in LocalDB of Visual Studio but works in SQL Developer.
insert into department (id_department, name, abbreviation, dept_head)
values (id_department_seq.nextval, 'School of Communications, Media, Arts and Design', department_abbreviation_seq.nextval, 'Nate Horowitz');
Getting this error
Msg 4104, Level 16, State 1, Line 63
The multi-part identifier "id_department_seq.nextval" could not be bound.
Msg 4104, Level 16, State 1, Line 63
The multi-part identifier "department_abbreviation_seq.nextval" could not be bound
That syntax for getting the next value for your sequence looks weird to me. I'm familiar with the following:
insert into department (id_department, name, abbreviation, dept_head)
values (
NEXT VALUE FOR id_department_seq,
'School of Communications, Media, Arts and Design',
NEXT VALUE FOR department_abbreviation_seq,
'Nate Horowitz'
);
Put this before your insert statement to ensure you have the sequence created.
if not exists(select * from sys.sequences where name ='id_department_seq') then
begin
create sequence id_department_seq start with 1000 increment by 1 minvalue 1000 maxvalue 99999;
end
insert into ....
General newbie SQL question probs...
Why doesn't this work - or, what does this mean?
INSERT INTO test_db.dbo.Customers2
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
I get:
Msg 213, Level 16, State 1, Line 2
Column name or number of supplied values does not match table definition.
You should supply column name for Customers2:
INSERT INTO test_db.dbo.Customers2(**Firstname**)
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
I would recommend to always explicitly specify which columns you want to insert into:
INSERT INTO test_db.dbo.Customers2(FirstName) -- <-- specify the columns you want to insert into!
SELECT FirstName
FROM jas_test_db.dbo.Customers
WHERE (Customers.FirstName = Firstname)
If you don't do this, you have to provide values for all columns, in the exact order in which they are defined in the table
So I'm supposed to take the last two digits from the phone number, and insert it into a new column in the same table.
I'm currently getting this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'Pass_id', table 'lunches.dbo.passenger'; column does not allow nulls. INSERT fails.
The statement has been terminated.
This is the query I'm using to get this error:
INSERT INTO dbo.passenger (age)
SELECT
RIGHT(phone, 2)
FROM dbo.passenger
UPDATE dbo.passenger
SET age = Cast(Right(phone, 2) As tinyint)
WHERE phone IS NOT NULL
AND Right(phone, 2) NOT LIKE '%[^0-9]%'
When I run an SSMS command to insert data, I get this error:
Msg 206, Level 16, State 2, Line 1
Operand type clash: int is incompatible with ntext
What is ment by Level 16, State 2
My insert is a standard insert:
insert into surveys
values (18964, 'CD9E8ACE-0D22-4969-B324-620DAFF06F97', 19959, 486, etc etc
How do I find where my error was ?
Probably you are inserting value without single quote on a string column. String values need to be quoted as they are literal not identifiers.