Understanding SSMS error messages, regarding an INSERT error: - sql

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.

Related

How do I do select into from another table

Basically I want to run a SQL statement like this, but I can't due to errors.
Not sure why?
INSERT INTO allevent (eventname)
VALUES (select username from registered)
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
No need of parentheses around the SELECT statement in your query.
INSERT INTO allevent (eventname)
SELECT username FROM registered
Parentheses with VALUES required, where some places like below:
INSERT INTO tablename (fieldname)
VALUES ('field 01'), ('field 02');
Please find the syntax for the INSERT statement.

Getting NextVal from sequence in Visual Studio SQL Server

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

SQL Insert invalid column name non-numeric values only

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.

"Arithmetic overflow error converting expression to data type int"

I'm trying to execute my simple SQL code to create a table.
BEGIN TRANSACTION;
CREATE TABLE vlan (vlanId int, vlanValue varchar(250), vlanName varchar(250), portId int, portName varchar(250), nodeId int, nodeName varchar(250));
INSERT INTO vlan VALUES(1111,998,'VOICE',7272,'1/97',456439345,'10.2.2.2');
.
.
.
.
INSERT INTO vlan VALUES(923482342,240,'INFODESK',2348328434,'4/46',2349234234,'10.1.2.3');
COMMIT;
it gives the error:
Msg 8115, Level 16, State 2, Line 16164
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
Msg 8115, Level 16, State 2, Line 16165
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
Msg 8115, Level 16, State 2, Line 16166
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
The table has approx. 16500 elements. Is it because the file is too large?
Any help will be useful.
Your vlanId column is to big for the datatype int (-2147483647 to 2147483647).
Use bigInt.
Link to SQL Server Doc.
From MSDN - int, bigint, smallint, and tinyint (Transact-SQL):
int
Integer (whole number) data from -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647). Storage size is 4 bytes. The SQL-92 synonym for int is integer.
The value you are trying to insert (2348328434) is outside of these. Use bigint instead.

Error when using openrowset for particular record

INSERT INTO OPENROWSET('MSDASQL', 'Driver=PostgreSQL Unicode;uid=postgres;Server=localhost;port=5432;database=data;pwd=xxx',
'select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from tesing WHERE SanctionId = ''-1'' ')
select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from testing where SanctionId=1103
While executing the above query, I am getting following error:
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.
The statement has been terminated.
Can anyone help me to resolve this?
You will have to check the source data against the target column definitions.
This happens when you try to insert, say, 100 characters into a varchar(50) column