Why is my SQL failing? I think it s formatted correctly, but it throws an error at me.
INSERT INTO NBOT_USERS
(ID,LAST_NAME,FIRST_NAME)
VALUES
(1002, 'Smith', 'John')
WHERE 1002 NOT IN (SELECT IT_ID FROM NBOT_USERS);
Insert queries do not have where clauses, unless you're doing INSERT ... SELECT FROM, in which case there can be a where clause in the SELECT portion.
Inserting Values with a Subquery: Example
INSERT INTO bonuses
SELECT employee_id, salary*1.1
FROM employees
WHERE commission_pct > 0.25 * salary;
With your schema:
INSERT INTO NBOT_USERS (ID,LAST_NAME,FIRST_NAME)
Select 1002, 'Smith', 'John'
From dual
WHERE 1002 NOT IN (SELECT FROM NBOT_USERS);
Related
i`m trying to do something.... I have to insert some data in to a table but..... So here is where I end up...
INSERT into HR.my_employees
(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT
3 AS ID,
'Biri' AS LAST_NAME,
'Ben' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
1100 AS salary
FROM dual
UNION
SELECT
4 AS ID,
'Newman' AS LAST_NAME,
'Chad' AS FIRST_NAME,
substr(FIRST_NAME,1,1)||''||substr(LAST_NAME,1,7) AS userid,
750 AS salary
FROM dual;
any suggestion...
You cannot refer to an alias in the SELECT or WHERE clauses of a sub-query where it is defined. Generate the data in a sub-query (or a sub-query factoring clause) and then refer to it in an outer query:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
WITH data (id, last_name, first_name, salary) AS (
SELECT 3, 'Biri', 'Ben', 1100 FROM DUAL UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM data;
or:
INSERT into HR.my_employees(ID,LAST_NAME,FIRST_NAME,userid,SALARY)
SELECT id,
last_name,
first_name,
SUBSTR(first_name,1,1) || SUBSTR(last_name,1,7),
salary
FROM (
SELECT 3 AS id, 'Biri' AS last_name, 'Ben' AS first_name, 1100 AS salary FROM DUAL
UNION ALL
SELECT 4, 'Newman', 'Chad', 750 FROM DUAL
);
fiddle
Why do you want to use select statements? - Based on the values you provide it seems like you want to insert hardcoded values. Or does the dual table contain data you want to insert into HR.my_employees?
If you want to just insert values into the table, but not from an existing one, you can use the following structure:
INSERT INTO table_name (column1, column2, column3, ..., columnXX)
VALUES (value1, value2, value3, ..., valueXX);
I have a table that stores employees (id, name, and gender). I need to randomly get two men and two women.
CREATE TABLE employees
(
id INT,
name VARCHAR (10),
gender VARCHAR (1),
);
INSERT INTO employees VALUES (1, 'Mary', 'F');
INSERT INTO employees VALUES (2, 'Jake', 'M');
INSERT INTO employees VALUES (3, 'Ryan', 'M');
INSERT INTO employees VALUES (4, 'Lola', 'F');
INSERT INTO employees VALUES (5, 'Dina', 'F');
INSERT INTO employees VALUES (6, 'Paul', 'M');
INSERT INTO employees VALUES (7, 'Tina', 'F');
INSERT INTO employees VALUES (8, 'John', 'M');
My attempt is the following:
SELECT TOP 2 *
FROM employees
WHERE gender = 'F'
ORDER BY NEWID()
UNION
SELECT TOP 2 *
FROM employees
WHERE gender = 'M'
ORDER BY NEWID()
But it doesn't work since I can't put two order by in the same query.
Why not just use row_number()? One method without a subquery is:
SELECT TOP (4) WITH TIES e.*
FROM employees
WHERE gender IN ('M', 'F')
ORDER BY ROW_NUMBER() OVER (PARTITION BY gender ORDER BY newid());
This is slightly less performant than using ROW_NUMBER() in a subquery.
Or, a fun method would use APPLY:
select e.*
from (values ('M'), ('F')) v(gender) cross apply
(select top (2) e.*
from employees e
where e.gender = v.gender
order by newid()
) e;
You cannot put an ORDER BY in the combinable query (the first one) of the UNION. However, you can use ORDER BY if you convert each one into a table expression.
For example:
select *
from (
SELECT TOP 2 *
FROM employees
WHERE gender = 'F'
ORDER BY newid()
) x
UNION ALL
select *
from (
SELECT TOP 2 *
FROM employees
WHERE gender = 'M'
ORDER BY newid()
) y
Result:
id name gender
--- ----- ------
5 Dina F
4 Lola F
2 Jake M
3 Ryan M
See running example at SQL Fiddle.
I want to insert (user_id) value from a select statement like below. Can this query will work on both sqlserver and oracle? Kindly advise.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
select max(user_id) from b_user ,
david brown,
david#david.com,
chicago,
il,
usa,
60007)
No, that query won't work in either Oracle or SQL Server. You can do an insert based on a select statement, however. Note that you also have to treat strings as strings and enclose them in quotes.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
select max(user_id) user_id,
'david brown',
'david#david.com',
'chicago',
'il',
'usa',
'60007'
from b_user
If we insert bracket () around the SELECT statement, it will work. I tried for SQL Server and it's working for me. Maybe you can try the following query for the Oracle as well.
insert into b_user (user_id,
user_name,
user_email,
user_address,
user_city,
user_state,
user_country,
user_zip)
values (
(select max(user_id) from b_user),
'david brown',
'david#david.com',
'123 davids street',
'chicago',
'il',
'usa',
60007
)
Tried to fill in a table with insert into command, but it just works for one value not for more.
I work with MS Access and the mistake is the comma placement, but I didn't find the mistake. Tried it with just one value and that works, but I have to insert it all.
INSERT INTO Abteilung (ID, Abteilung, Mitarbeiteranzahl)
VALUES (('1', 'Einkauf', '5'), ('2', 'HR', '5'), ('3', 'Controlling', '5'),
('4', 'Produktion', '20'), ('5', 'Vertrieb', '20'),
('6', 'Qualitätsmanagement', '3'), ('7', 'Industrial Engineering', '8')
)
You can't do this as easily in access as other major db but it can be made possible. First off, create yourself a table with one row:
CREATE TABLE dual([Dummy] Integer)
INSERT INTO dual VALUES(1)
Now you can select a bunch of hard coded values "from" this table:
INSERT INTO Abteilung
(
ID,
Abteilung,
Mitarbeiteranzahl
)
SELECT '1' as e1,'Einkauf' as e2, '5' as e3 FROM dual
UNION
SELECT '2','HR','5' FROM dual
UNION
SELECT '3','Controlling','5' FROM dual
UNION
SELECT '4','Produktion','20' FROM dual
UNION
SELECT '5','Vertrieb','20' FROM dual
UNION
SELECT '6','Qulaitätsmanagement','3' FROM dual
UNION
SELECT '7','Industrial Engineering','8' FROM dual
You might need to wrap all those selects in another SELECT * FROM, I can't quite remember
By the time you're done writing all those out you might well get to thinking it would be as easy to just write N number of insert statements..
There is a way how you can insert rows without building SQL strings.
check this Answer
You can't insert more than 1 row with an INSERT statement in Access.
Even multiple INSERT statements separated with ; are not allowed.
You can use a trick though like this:
INSERT INTO Abteilung ( ID, Abteilung, Mitarbeiteranzahl)
SELECT * FROM (
SELECT '1' AS ID, 'Einkauf' AS Abteilung, '5' AS Mitarbeiteranzahl FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '2','HR','5' FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '3','Controlling','5' FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '4','Produktion','20' FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '5','Vertrieb','20' FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '6','Qulaitätsmanagement','3' FROM (SELECT COUNT(*) FROM Abteilung)
UNION ALL
SELECT '7','Industrial Engineering','8' FROM (SELECT COUNT(*) FROM Abteilung)
)
Unfortunately the FROM clause is needed for each of the INSERTs and it must return only 1 row, this is why I used SELECT COUNT(*) which for a large table may be not that efficient.
Can you try the following query:
INSERT INTO Abteilung
(
ID,
Abteilung,
Mitarbeiteranzahl
)
SELECT '1','Einkauf','5'
UNION ALL SELECT '2','HR','5'
UNION ALL SELECT '3','Controlling','5'
UNION ALL SELECT '4','Produktion','20'
UNION ALL SELECT '5','Vertrieb','20'
UNION ALL SELECT '6','Qulaitätsmanagement','3'
UNION ALL SELECT '7','Industrial Engineering','8'
Hope this should work.
In DB2 SQL, I want to write something like
insert into employees
(id, name) values (1, "emp1")
where (select count(*) from employee_registry) <= 10
Can't figure out the correct syntax though.
post edit.
i wanted to insert records only if some condition is met.
You need to have a SELECT statement somewhere in order to use the WHERE clause, so something like this might work:
insert into employees (id, name)
select 1, 'emp1' from sysibm.sysdummy1
where (select count(*) from employee_registry) <= 10
sysibm.sysdummy1 is a special system table that always has only one row.
Other method, group by Nothing + having :
insert into employees (id, name)
select '1', 'emp1' from employee_registry
group by 1 having count(*)<=10