I am using to using SQL Server. I'm trying to figure out how to insert multiple rows with one query.
In MySQL the query would be like this:
Code:
INSERT INTO Mytable (Name, Number) VALUES ('Joe', 18), ('Bob', 25), ('Mike', 7);
I tried a query like the one above in SQL Server and it gave me an error that said:
Incorrect syntax near ','.
Is there a way to do this in SQL Server?
That syntax will work in SQL 2008; in SQL 2005, you have to do SELECTs and UNIONs
INSERT INTO Mytable (Name, Number)
SELECT 'Joe', 18
UNION ALL SELECT 'Bob', 25
UNION ALL SELECT 'Mike', 7
INSERT INTO sample (ID, Name)
VALUES (001, 'happy'),
(002, 'sunny'),
(125, 'rajesh')
Related
INSERT INTO MANAGING(SUPID, EMPID)
VALUES (76, 1, 4, 8);
currently this is the code I have, I just simply want to have the 1,4,8 go into the EMPID as three separate values.
Any help would be great, thanks!
Insert them using separate scripts.
Simply try this.
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 1);
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 4);
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 8);
Depending on your db engine, you can do it also using
INSERT INTO MANAGING(SUPID, EMPID) VALUES (76, 1),(76, 4),(76, 8);
if you have emp table
INSERT INTO MANAGINS(SUPID,EMPID) SELECT 76, ID FROM EMP_TABLE
This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 1 year ago.
INSERT into L5_DIRECTORS (director_id, first_name, last_name)
Values
(1010, 'Rob', 'Minkoff'),
(1020, 'Bill', 'Condon'),
(1050, 'Josh', 'Cooley'),
(2010, 'brad', 'bird'),
(3020, 'lake', 'bell');
EDIT: I figured it out, I had to write each row individually and run it, I do not why it would not work all together but thanks for the help anyway.
Oracle does not allow you to insert multiple rows using the VALUES syntax. I recommend just using insert . . select:
INSERT into L5_DIRECTORS (director_id, first_name, last_name)
SELECT 1010, 'Rob', 'Minkoff' FROM DUAL UNION ALL
SELECT 1020, 'Bill', 'Condon' FROM DUAL UNION ALL
SELECT 1050, 'Josh', 'Cooley' FROM DUAL UNION ALL
SELECT 2010, 'brad', 'bird' FROM DUAL UNION ALL
SELECT 3020, 'lake', 'bell' FROM DUAL;
I'm trying to write a simple insert into statement using multiple values. The solutions I've seen say to separate each set of values with a comma, however, I am still being met with an error. Here is my SQL statement.
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
VALUES ('20', 'Quebec'), ('30', 'Ontario');
You can also use this one:
INSERT ALL
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (20, 'Quebec')
INTO DIVISION (DIVISION_ID, DIVISION_NAME) VALUES (30, 'Ontario')
SELECT * FROM dual;
If DIVISION_ID is a numeric data type:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT 20, 'Quebec' FROM DUAL
UNION ALL
SELECT 30, 'Ontario' FROM DUAL;
Otherwise:
INSERT INTO DIVISION (DIVISION_ID, DIVISION_NAME)
SELECT '20', 'Quebec' FROM DUAL
UNION ALL
SELECT '30', 'Ontario' FROM DUAL;
I have 3 tables professionals, services, and feature_professional I need to insert data into when inserting a professional. This is the sql I've tried.
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut'),
INSERT INTO feature_professional(id, professional_id, feature_id, enabled)
VALUES
(1, (select professionals.id from professionals), 1, true)
I've also tried different variations of this wrapping all the insert statements inside parenthesis but still no luck. The error I'm getting is a syntax error.
ERROR: syntax error at or near "INSERT"
LINE 106: INSERT INTO feature_professional
What is the correct syntax to accomplish this? I know that if I take out the third insert statement the query works. For example:
WITH professionals as (INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *)
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
You cant do two insert after a cte, so wrap each one in a different cte
SQL DEMO
WITH professionals as (
INSERT INTO professionals(id, company_id, first_name)
VALUES (1, 1, 'peter') RETURNING *
),
services as (
INSERT INTO services(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'haircut')
)
INSERT INTO features(id, professional_id, name)
VALUES (1, (select professionals.id from professionals), 'autobook');
select * from professionals;
select * from services;
select * from features;
INSERT INTO feature_professional (id, professional_id, feature_id, enabled)
select ?, professionals.id from professionals, ?, ?
Provided that the question marks are values you can insert. If they aren't required you can just omit them.
INSERT INTO feature_professional (professional_id)
select professionals.id from professionals
Here is how to insert from a select instead of static values:
https://www.w3schools.com/sql/sql_insert_into_select.asp
I have been trying to move away from using DECODE to pivot rows in Oracle 11g, where there is a handy PIVOT function. But I may have found a limitation:
I'm trying to return 2 columns for each value in the base table. Something like:
SELECT somethingId, splitId1, splitName1, splitId2, splitName2
FROM (SELECT somethingId, splitId
FROM SOMETHING JOIN SPLIT ON ... )
PIVOT ( MAX(splitId) FOR displayOrder IN (1 AS splitId1, 2 AS splitId2),
MAX(splitName) FOR displayOrder IN (1 AS splitName1, 2 as splitName2)
)
I can do this with DECODE, but I can't wrestle the syntax to let me do it with PIVOT. Is this even possible? Seems like it wouldn't be too hard for the function to handle.
Edit: is StackOverflow maybe not the right Overflow for SQL questions?
Edit: anyone out there?
From oracle-developer.net it would appear that it can be done like this:
SELECT somethingId, splitId1, splitName1, splitId2, splitName2
FROM (SELECT somethingId, splitId
FROM SOMETHING JOIN SPLIT ON ... )
PIVOT ( MAX(splitId) ,
MAX(splitName)
FOR displayOrder IN (1 AS splitName1, 2 as splitName2)
)
I'm not sure from what you provided what the data looks or what exactly you would like. Perhaps if you posted the decode version of the query that returns the data you are looking for and/or the definition for the source data, we could better answer your question. Something like this would be helpful:
create table something (somethingId Number(3), displayOrder Number(3)
, splitID Number(3));
insert into something values (1, 1, 10);
insert into something values (2, 1, 11);
insert into something values (3, 1, 12);
insert into something values (4, 1, 13);
insert into something values (5, 2, 14);
insert into something values (6, 2, 15);
insert into something values (7, 2, 16);
create table split (SplitID Number(3), SplitName Varchar2(30));
insert into split values (10, 'Bob');
insert into split values (11, 'Carrie');
insert into split values (12, 'Alice');
insert into split values (13, 'Timothy');
insert into split values (14, 'Sue');
insert into split values (15, 'Peter');
insert into split values (16, 'Adam');
SELECT *
FROM (
SELECT somethingID, displayOrder, so.SplitID, sp.splitname
FROM SOMETHING so JOIN SPLIT sp ON so.splitID = sp.SplitID
)
PIVOT ( MAX(splitId) id, MAX(splitName) name
FOR (displayOrder, displayOrder) IN ((1, 1) AS split, (2, 2) as splitname)
);