Condition before insert SQL - sql

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

Related

INSERT with SELECT returning multiple values throws error "subquery returned more than 1 value"

Trying to insert multiple values in a table (as many as ID's from the SELECT result), but the select Id query returns more than 1 value (expected), hence the error.
INSERT INTO table1 (UserId, Date, ...)
VALUES
((SELECT Id from table2 WHERE ClientId = 26), GETDATE(), ...)
what would be the best approach for this?
I'm not sure how your columns line up. It looks like GETDATE() is inserting into Name. But if that's what you need, what about this? You could just do an INSERT SELECT without the VALUES.
INSERT INTO table1 (UserId, Name, ...)
SELECT Id, GETDATE(), ...
from table2
WHERE ClientId = 26

Oracle - Conditional Insert with Sequence

I know the sequence can not be used in these places.
For a SELECT statement:
In a WHERE clause
In a GROUP BY or ORDER BY clause
In a DISTINCT clause
Along with a UNION or INTERSECT or MINUS
In a sub-query
Please help to achieve the below requirement with conditional insert.
Objective is - Need to insert if the name is not exist in the table as its protected by Sequence as primary key.
INSERT
WHEN EXISTS (SELECT 0 FROM TABLE1 WHERE NAME = 'DUPLICATE_NAME_TEST')
THEN
INTO TABLE1 (KEY, NAME, GROUP)
SELECT TESTSEQ.NEXTVAL, 'DUPLICATE_NAME_TEST', 30 FROM DUAL;
SQL has no INSERT WHEN construct. It does have WHERE. So you intend something like this:
INSERT INTO TABLE1 (NAME, GROUP)
SELECT 'DUPLICATE_NAME_TEST', 30
FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM TABLE1 T1 WHERE T1.NAME = 'DUPLICATE_NAME_TEST');

How to insert multiple insert sql statement

There is a table Person(id, name). I am inserting more than 1000 records into person table. Both id and name should be unique. I wrote something like this
INSERT ALL
INTO PERSON (1, 'MAYUR')
INTO PERSON (2, 'SALUNKE')
.....(1000 records)
SELECT * FROM DUAL;
I am getting unique constraint for name in this query. How do I know which record in particular is failing. All I see in logs is this
Error starting at line : 3 in command - ORA-00001: unique constraint
(UN_PERSON_NAME) violated.
This does not tell the exact record which is duplicate.
You are missing values keyword. Try this!
INSERT ALL
INTO PERSON values(1, 'MAYUR')
INTO PERSON values(2, 'SALUNKE')
.....(1000 records)
SELECT * FROM DUAL;
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
Unfortunately, Oracle doesn't support multiple inserts using a single VALUES() statement. I usually approach this as:
INSERT PERSON (id, name)
SELECT 1, 'MAYUR' FROM DUAL UNION ALL
SELECT 2, 'SALUNKE' FROM DUAL UNION ALL
.....;
One advantage of this approach is you can use a subquery and assign the id:
INSERT PERSON (id, name)
SELECT rownum, x.name
FROM (SELECT 'MAYUR' FROM DUAL UNION ALL
SELECT 'SALUNKE' FROM DUAL UNION ALL
.....
) x

Combining INSERT SELECT with constants

I'm trying to insert records into a table where one of the columns comes from another table.
The other two columns are the same for each record.
All three columns are keys.
I'm trying this embedded INSERT SELECT which I see is not allowed?
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C)
VALUES (1,(SELECT COLUMN_NAME FROM TABLE) ,2)
Your syntax is off - this is the correct syntax for this:
Insert Into Table
(Column_A, Column_B, Column_C)
Select 1, Column_Name, 2
From OtherTable
I would post a comment but I don't have enough 'status' yet.
The insert below works in PostgreSQL. What database are you using?
insert into people_experiences (qty, created_at, updated_at, person_id, experience_id) values (1, now(), now(), (select id from people where first_name='Joseph'), (select id from experiences where experience='MS'));
INSERT 0 1
We can even use this
Insert Into Table
(Select "value-1" as column, "value-2" as column, ot.* from otherTable ot)
for two column i want constant values and rest should be use as it is.
Note: Insert select will not allow virtual column.

SQL query for the latest record for each day

I've got an Oracle 10g database with a table with a structure and content very similar to the following:
CREATE TABLE MyTable
(
id INTEGER PRIMARY KEY,
otherData VARCHAR2(100),
submitted DATE
);
INSERT INTO MyTable VALUES (1, 'a', TO_DATE('28/04/2010 05:13', ''DD/MM/YYYY HH24:MI));
INSERT INTO MyTable VALUES (2, 'b', TO_DATE('28/04/2010 03:48', ''DD/MM/YYYY HH24:MI));
INSERT INTO MyTable VALUES (3, 'c', TO_DATE('29/04/2010 05:13', ''DD/MM/YYYY HH24:MI));
INSERT INTO MyTable VALUES (4, 'd', TO_DATE('29/04/2010 17:16', ''DD/MM/YYYY HH24:MI));
INSERT INTO MyTable VALUES (5, 'e', TO_DATE('29/04/2010 08:49', ''DD/MM/YYYY HH24:MI));
What I need to do is query the database for the latest record submitted on each given day. For example, with the above data I would expect the records with ID numbers 1 and 4 to be returned, as these are the latest each for 28 April and 29 April respectively.
Unfortunately, I have little expertise as far as SQL is concerned. Could anybody possibly provide some insight as to how to achieve this?
Thanks in advance!
I would use a common table expression (aka CTE) like so:
With RankedItems As
(
Select Id, otherdata, submitted
, ROW_NUMBER() OVER( PARTITION BY TO_CHAR(submitted, 'YYYY-MM-DD') ORDER BY submitted DESC ) ItemRank
From MyTable
)
Select
From RankedItems
Where ItemRank = 1
I think it's as simple as:
SELECT * from MyTable ORDER BY submitted DESC LIMIT 1
Though it might be worth exploring whether there are some column/where parameters that could make the query run more quickly, particularly if you've got a query plan analyzer handy.