Insert several values in SQL Server - sql

I'm trying to insert some values into SQL Server but I get an error message:
Line 2: Incorrect syntax near ','
I need to enter several values in 1 table so I was looking for a quicker way to not insert 1 at a time.
For some reason I always have issues with databases :(
My query is:
INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
VALUES (81570, 4, 2001, 2, 'N'),
(81570, 4, 2001, 3, 'S'),
(81570, 4, 2001, 3, 'N');
Thank you in advance, David

For older versions of SQL Server you can use insert from select
INSERT INTO PERSONALRH_NIVEL (SERHGCCAB, PUESTO_ID, COMPANIA_ID, REGION_ID, TIPO_EMPLEADO)
SELECT 81570, 4, 2001, 2, 'N'
UNION ALL
SELECT 81570, 4, 2001, 3, 'S'
UNION ALL
SELECT 81570, 4, 2001, 3, 'N';

It depends on the version of SQL Server you are using. Insert Into () VALUES (),() only works in SQL Server 2008 and newer. If you are using SQL Server 2005 you need to use separate Insert statements.

Related

MariaDb Begin End in a multi-value Insert

Im using MariaDb and Im trying to execute a multi-value insert query using Begin End and I get the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END' at line 1 0.000 sec
BEGIN;
INSERT INTO Client (Id, Name, Total)
VALUES
(1, 'John', 1000),
(2, 'Mary', 3000),
(3, 'Mike', 14000),
(4, 'George', 15000);
END;
Can I use Begin, End with a multi-value insert and if yes what is the correct syntax here?
I have millions of data to insert. the above is just a small sample of my dataset

Insert query with gettime() into SQL Server table using Python

I am having an issue putting the below SQL query into Python :
insert into GERVERSION values (10, 'xxxxxxxx', 7, 4, 2, getdate(),'')
Note that in SQL , I have timestamp for the getdate() part
Can anyone please help me out with the right syntax to put this into work ?
Try using INSERT INTO ... SELECT syntax:
INSERT INTO GERVERSION
SELECT 10, 'xxxxxxxx', 7, 4, 2, GETDATE(), '';

Subquery cannot appear in an Insert Values statement

I have the following query in SQL Server CE which gives me an error during the execution time:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
VALUES('p', 104, (select MAX(expence_id) from c_expence))
The error is this:
Subquery cannot appear in an Insert Values statement.
What is wrong with this query?
Try this one:
INSERT INTO trans_rel
SELECT 'p', '102', MAX(expence_id)
FROM c_expence
This is exactly what your are looking for:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
SELECT 'p' as 'trans', '104' as 'sale_purch_id', MAX(expence_id) AS inc_exp_id
FROM c_expence;

Insert statement in SQL Server 2005

I have strange problem related to SQL Server 2005
When I try to insert into table
insert into IDName
VALUES (101 , 'AA'),
(301 , 'BB')
I receive this error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ','.
There is no problem, if I insert records one by one.
EDIT:
Thanks for reply guys.... but this script works in other installation of sql server 2005... I think it is some setting issue but i do not where... if you could help
This syntax was introduced in SQL Server 2008. So upgrade, or use the more verbose:
INSERT dbo.IDName(column1, column2)
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB';
Some additional changes:
Always use the schema prefix when referencing objects
Always specify the column list for an INSERT
Always use semi-colons to terminate statements
SQL Server 2005 does not support that insert syntax; you would either need
insert into IDName
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB'
or
insert into IDName VALUES (101 , 'AA');
insert into IDName VALUES (301 , 'BB');

SQL Server – inserting multiple rows with single (ANSI style) statement

I am using following method for inserting multiple rows using a single INSERT statement, that is the ANSI style of inserting rows. It is available in SQL Server 2008 and 2012. I am not sure of SQL Server 2005/ 2000.
Create test table:
create table TestInsert (ID INT, Name NVARCHAR(50))
Single INSERT statement to insert 5 rows
INSERT INTO TestInsert
VALUES (1,'a'),
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
Please let me know if there is any other best way to achieve this
Try this instead:
INSERT TestInsert
SELECT 1, 'a'
UNION ALL
SELECT 2, 'b'
UNION ALL
SELECT 3, 'c'
UNION ALL
SELECT 4, 'd'
UNION ALL
SELECT 5, 'e'
SQL Server - inserting multiple rows with single (ANSI style) statement
For SQL Server 2000+
According to SQL The Complete Reference, Third Edition (August 12, 2009):
1) The syntax for multirow INSERTs is
INSERT INTO table-name (columns not mandatory)
query
(page 236, Figure 10-3).
2) The SELECT statement has the FROM clause mandatory (page 87, Figure 6-1).
So, in this case, to insert multiple rows using just one INSERT statement we need an auxiliary table with just one row:
CREATE TABLE dual(value INT PRIMARY KEY CHECK(value = 1))
INSERT dual(value) VALUES(1)
and then
INSERT INTO table-name (columns) -- the columns are not mandatory
SELECT values FROM dual
UNION ALL
SELECT another-values FROM dual
UNION ALL
SELECT another-values FROM dual
Edit 2: For SQL Server 2008+
Starting with SQL Server 2008 we can use row constructors: (values for row 1), (values for row 2), (values for row 3), etc. (page 218).
So,
INSERT INTO TestInsert
VALUES (1,'a'), --The string delimiter is ' not ‘...’
(2,'b'),
(3,'c'),
(4,'d'),
(5,'e')
will work on SQL Server 2008+.