Getting NextVal from sequence in Visual Studio SQL Server - sql

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

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.

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.

Understanding SSMS error messages, regarding an INSERT error:

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.

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ',' trying to INSERT INTO

i've succesfully created db and tables, but when i try populate one of a table, like this
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0),
(0,'productName2',0),
(1,'productName3',9),
(1,'productName4',7),
(1,'productName5',3),
(1,'productName6',10),
(0,'productName7',0),
(1,'productName8',6),
(1,'productName9',12),
(1,'productName10',20);
GO
i got an error:
Msg 102, Level 15, State 1,
Line 2 Incorrect syntax near ','.
firstly which ',' is meant, secondly - what is wrong?
PS: i use MS Management Studio v 9.0 if it is needed...
Versions of SQL Server 2005 and below do not support the multiple VALUE clause syntax
SQL Server 2005 is version 9...
See How do I insert multiple rows WITHOUT repeating the "INSERT INTO dbo.Blah" part of the statement? for more
if you are using SQL SERVER 2005 and below, the query wouldn't work because it doesn't support multiple value clause insert statement. You should insert it one by one.
Like this one below,
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName1',0)
GO
INSERT INTO Products(IsProductActive,ProductName,ProductCount)
VALUES(0,'productName2',0)
GO

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