Delete Records Based on Query Output - sql-server

I have a table, THEKITCHEN with a query:
SELECT FOOD, COUNT(FOOD)
FROM THEKITCHEN
GROUP BY FOOD
This returns an output of:
FOIEGRAS 1
APPLEPIE 1
SORBET 1
FILETMIGNON 1
BRANZINO 5
TRUFFLES 2
ESCARGO 1
POULET 2
And what I do next is delete items using the following:
DELETE FROM THEKITCHEN
WHERE FOOD IN ('FOIEGRAS')
Now this works but sometimes I need to delete all items/records in that column. And running that DELETE statement 15-40 times becomes annoying.
Is it possible to get the output of the initial query into a variable and then use the DELETE statement on that variable to delete all the items? I've tried
UPDATE THEKITCHEN
SET FOOD = NULL;
But that errored in that my table doesn't allow Null.
Thanks!
Update: THEKITCHEN has other columns which I do not want to delete records from. So TRUNCATE TABLE THEKITCHEN would lose a lot of important stuff.
Update: Yes TRUNCATE TABLE THEKITCHEN is the way to go!

If you want to delete all the food items, you don't need a condition at all, just use delete from thekitchen. Moreover, you can use a DDL statement truncate table thekitchen which would be much faster.

Related

Is there a way to delete the duplicate from a table in SQL Server?

I have a table like this:
As you can see, the rows 2 and 3 are similar, and row 3 is a useless duplicate. My question is how can we delete row 3 only but keep row2 and row4 at the same time.
Like this:
Thanks for your help!
You don't have duplicates. If you had a heap table with identical records, then every value in one or more records would be the same. One means of dealing with this would be to add an identity column. Then the identity column can be used to remove some but not all of the duplicates.
In your case, you want to delete records if another record exists that is similar and perhaps has "better" data. You can use an EXISTS clause to do this. The logic below is not what you want, but it should give you the idea of how to handle this.
DELETE t
FROM MyTable t
WHERE t.BCT IS NULL -- delete only records with no values?
AND t.BCS IS NULL
AND EXISTS( -- another record with a value exists, so this one might not be needed?
SELECT *
FROM MyTable x
WHERE (x.BCT IS NOT NULL OR t.BCS IS NOT NULL)
AND x.portCode = t.portCode
AND x.effDate = t.effDate
AND LEFT(x.issueName, 26) = LEFT(t.issueName, 26)
)

problem in instead of delete views in oracle

I have a view as:
select * from ot.vw_rebellionrid;
which is showing me:
full_name is column of trainer table
subject_name is column of subject table
The view has the query:
SELECT full_name, subject_name
FROM ot.trainer, ot.subject;
So,I am learning the instead of delete tigger,so the code for instead of trigger is:
CREATE OR REPLACE TRIGGER ot.io_delete
INSTEAD OF DELETE ON ot.vw_RebellionRid
FOR EACH ROW
BEGIN
DELETE FROM ot.trainer WHERE FULL_NAME = :old.FULL_NAME;
DELETE FROM ot.subject WHERE SUBJECT_NAME= :old.SUBJECT_NAME;
END;
/
So,to execute this trigger,I execute the following statement:
delete from ot.vw_RebellionRid where subject_name='java';
when it is executed,then :
2 rows deleted.
According to my understanding,Since the two rows has subject_name as java the remaining output,I think will be after the deletion:
--------------------------
full_name | subject_ name |
---------------------------
Manish Sharma Oracle |
ashwin Oracle |
---------------------------
But when I executed
select * from ot.vw_RebellionRid ;
then,I got the output as EMPTY TABLE;
Since,there were 4rows,2rows were deleted,but when i again select the view then how I am getting empty table?I should have got remaining two rows.
I think your trigger is working perfectly.
DELETE FROM ot.trainer WHERE FULL_NAME = :old.FULL_NAME;
It will delete 2 rows having name manish and ashwin as both are tagged with java.
The same way,
DELETE FROM ot.subject WHERE SUBJECT_NAME= :old.SUBJECT_NAME;
It will delete subject java from table subject so now table subject will have 1 record remaining i.e.Oracle
Now, your view query will join between 2 tables (trainer table with 0 rows and subject table with 1 rows) so it is producing no result.
You need to understand that,
delete from ot.vw_RebellionRid where subject_name='java';
is deleting total 3 records.
Cheers!!
The problem is that when debugging your problem you didn't follow through. Your INSTEAD OF DELETE trigger certainly deleted SOMETHING, but to determine what was deleted you need to look at the tables underlying the view. If you perform your DELETE statement and then examine the contents of TRAINER and SUBJECT you'll find that the only thing left is the Oracle row in SUBJECT. And if you look at your trigger and think about what you told it to do you'll realize it did exactly what you told it to do, even if that might not have been what you wanted.
dbfiddle here

How to safely use current identity as value in insert query

I have a table where one of the columns is a path to an image and I need to create a directory for the record being inserted.
Example:
Id | PicPath |<br>
1 | /Pics/1/0.jpg|<br>
2 | /Pics/2/0.jpg|
This way I can be sure that the folder name is always valid and it is unique (no clash between two records).
Question is: how can I safely refer to the current id of the record being insert? Keep in mind that this is a highly concurrent environment, and I would like to avoid multiple trips to the DB if possible.
I have tried the following:
insert into Dummy values(CONCAT('a', (select IDENT_CURRENT('Dummy'))))
and
insert into Dummy values(CONCAT('a', (select SCOPE_IDENTITY() + 1)))
The first query is not safe, for when running 1000 concurrent inserts I got 58 'duplicate key' exceptions.
The second query didn't work because SCOPE_IDENTITY() returned the same value for all queries as I suspected.
What are my alternatives here?
Try a temporary table to track your inserted ids using OUTPUT clause
INSERT #temp_ids(someval) OUTPUT inserted.identity_column
This will get all the inserted ids from your queries. 'inserted' is context safe.

Access: Removing a duplicate if two fields are the same

I have an Access database table which sometimes contains duplicate ProfileIDs. I would like to create a query that excludes one (or more, if necessary) of the duplicate records.
The condition for a duplicate record to be excluded is: if the PriceBefore and PriceAfter fields are NOT equal, they are considered duplicate. If they are equal, the duplicate field remains.
In the example table above, the records with ID 7 and 8 have the same ProfileIDs. For ID 8, PriceBefore and PriceAfter are not equal, so this record should be excluded from the query. For ID 7, the two are equal, so it remains. Also note that PriceBefore and PriceAfter for ID 4 are the same, but as the ProfileID is not a duplicate, the record must remain.
What is the best way to do this? I am happy to use multiple queries if necessary.
Create a pointer query. Call it pQuery:
SELECT ProfileID, Sum(1) as X
FROM MyTableName
HAVING Sum(1) > 1
This will give you the ProfileID of every record that's part of a dupe.
Next, find the records where prices don't match. Call this pNoMatchQuery:
SELECT MyTableName.*
FROM MyTableName
INNER JOIN pQuery
ON pQuery.ProfileID = MyTableName.ProfileID
WHERE PriceBefore <> PriceAfter
You now have a query of every record that should be excluded from your dataset. If you want to permanently delete all of these records, run a DELETE query where you inner join your source table to pNoMatchQuery:
Delete MyTableName.*
From MyTableName
Where Exists( Select 1 From pNoMatchQuery Where pNoMatchQuery.ID = MyTableName.ID ) = True
First, make absolutely sure that pQuery and pNoMatchQuery are returning what you expect before you delete anything from your source table, because once it's gone it's gone for good (unless you make a backup first, which I would highly suggest before you run that delete the first time).

delete duplicate rows

anyone know how can i delete duplicate rows by writing new way from script below to improve performance.
DELETE lt1 FROM #listingsTemp lt1, #listingsTemp lt2
WHERE lt1.code = lt2.code and lt1.classification_id > lt2.classification_id and (lt1.fap < lt2.fap or lt1.fap = lt2.fap)
Delete Duplicate Rows in a SQL Table :
delete table_a
where rowid not in
(select min(rowid) from table_a
group by column1, column2);
1 - Create an Identity Column (ID) for your table (t1)
2 - Do a Group by on your table with your conditions and get IDs of duplicated records.
3 - Now, simply Delete records from t1 where IDs IN duplicated IDs set.
Look into BINARY_CHECKSUM .... you could possibly use it when creating your temp tables to more quickly determine if the data is the same.... for example create a new field in both temp tables storing the binary_checksum value... then just delete where those fields equal
The odiseh answer seems to be valid (+1), but if for some reason you can't alter the structure of the table (because you have not the code of the applications that are using it or something) you could write a job that run every night and delete the duplicates (using the Moayad Mardini code).

Resources