I did a mistake insert in such way
insert into tab1 ( col1 , col2 , col3) select col1 , col2,col3 from tab2
i got 7600 row effects
i want to delete those 7600 rows , if I made below delete would it be safe ?
delete from tab1 where exists select col1 , col2,col3 from tab2
I am asking this to know if i will lost data in tab1 . would this query delete more then 7600 rows ?. if there was common data i would got unique constraint in isert right ? ( yes both have the same PK)
Your DELETE statement isn't even valid syntax, so I don't think that it will do what you want. Even if you fixed the syntax though, you have nothing relating tab1 to tab2.
Further, you haven't provided your table structures, so anything from here on is mostly a guess on my part. Please read: https://stackoverflow.com/help/how-to-ask
Try running:
SELECT COUNT(*)
FROM
tab1
INNER JOIN tab2 ON tab2.col1 = tab1.col1 -- Is this the primary key? If not then you'll need to join on the whole primary key
If you get the same number as you got when you did the INSERT then you can do:
DELETE T1
FROM
Tab1 T1
INNER JOIN Tab2 T2 ON T2.col1 = T1.col1
I would put the DELETE into a transaction and roll it back once just to verify the count, then if it matches, run it and commit.
Related
I have a small app that inserts data into the database.
I have a something like this
dbo.system.table1(col1, col2, col3) and table2(col1) (table2.col1 is just one single row).
What I want to do is insert the table2.col1 into table1.col3 when a new order is made.
Also the table2.col1 updates twice a day, so every time a new order is made with table1 I need to keep the old table1.col3 changes.
I've tried
CREATE TRIGGER dbo.TR_CHANGES
ON dbo.SYSTEM
AFTER INSERT
AS
UPDATE table1
SET table1.col3 = (SELECT col1
FROM table2
WHERE table1.col3 = table2.col1)
But it ends updating col3 for all rows.
You need to include the Inserted pseudo table into your statement, to find the rows that were actually updated - and I would recommend using proper JOIN syntax instead of those nested subqueries - seems a lot easier to read and understand for me.
So try this:
CREATE TRIGGER dbo.TR_CHANGES
ON dbo.SYSTEM
AFTER INSERT
AS
UPDATE t1
SET col3 = t2.col1
FROM table1 t1
INNER JOIN table2 t2 ON t1.col3 = t2.col1
INNER JOIN inserted i ON t1.primarykeycol = i.primarykeycol
You need to replace the .primarykeycol for the Inserted and t1 tables with the actual primary key column for your table - this is needed to link the Inserted rows with the actual data table
Say I've already created table3, and try to insert data into it using the following code
WITH table1
AS
(SELECT 1 AS key, 'One' AS value),
table2
AS
(SELECT 1 AS key, 'I' AS value)
INSERT TABLE table3
SELECT t1.key, t1.value, t2.value
FROM table1 t1
JOIN table2 t2
ON (t1.key = t2.key)
However, I got an error as cannot recognize input 'insert' in select clause. If I simply delete the insert sentence, then the query runs just fine.
Is this a syntax problem? Or I cannot use with clause to insert?
Use INTO or OVERWRITE depending on what you need:
INSERT INTO TABLE table3 --this will append data, keeping the existing data intact
or
INSERT OVERWRITE TABLE table3 --will overwrite any existing data
Read manual: Inserting data into Hive Tables from queries
I have a table than over time can get bigger and I want to insert some of its rows in another table but I also want to make sure I am not duplicating the rows that I had inserted before.
So here is the type of condition for my insert:
INSERT INTO SecondTable(Col1,Col2)
SELECT Col5,Col6
FROM
FirstTable ft
WHERE ft.RecType = 'ABC'
So if I keep running this it will keep inserting the same rows again and again. How can I tell it only insert if it is not already there?
You can use not exists:
INSERT INTO SecondTable(Col1,Col2)
SELECT Col5,Col6
FROM FirstTable ft
WHERE ft.RecType = 'ABC' AND
NOT EXISTS (SELECT 1 FROM SecondTable t2 WHERE t2.col1 = ft.col5 AND t2.col2 = ft.colt6);
Generate unique constraint on table with proper columns which identifies unicity. This will also help you to preserve integrity of your table. when you try to insert records into the RDBMS will give you an error.
ALTER TABLE SecondTable
ADD UNIQUE (col1, col2, col3);
INSERT INTO SecondTable(Col1,Col2)
SELECT Col5,Col6
FROM FirstTable ft
LEFT JOIN SecondTable st ON st.Col1 = ft.Col1
WHERE st.Col1 IS NULL AND ft.RecType = 'ABC'
I am trying to delete duplicate records using SQL statement. I am using MS Access 2010. Here is my statement:
DELETE * FROM table1 where not exists (SELECT min(col1) FROM table1 GROUP BY col2, col3 );
Even though the nested Select statement returns a subset of the records of table1, when I execute the delete statement, it deletes no (0) records.
You need a correlation clause. I'm not sure what the data looks like, but this should work:
DELETE *
FROM table1
WHERE col1 <> (SELECT MIN(t1.col1)
FROM table1 as t1
WHERE t1.col2 = table1.col2 AND t1.col3 = table1.col3
);
I suspect that you are trying for:
DELETE *
FROM table1
WHERE col1 NOT IN (SELECT MIN(col1) FROM table1 GROUP BY col2, col3);
This will also work, assuming that col1 is unique across all rows (the first version will work as long as col1 is unique for any pair of col2/col3 values).
I need to copy a lot of data from one table to another. If the data already exists, I need to update it, otherwise I need to insert it. The data to be copied is selecting using a WHERE condition. The data has a primary key (a string of up to 12 characters).
If I was just inserting the data, I would do
INSERT INTO T2 SELECT COL1, COL2 FROM T1 WHERE T1.ID ='I'
but I cannot figure out how to do the INSERT / UPDATE. I keep seeing references to upserts and MERGE, but MERGE appears to have issues,and I cannot figure ut how to do the upsert for multiple records.
What is the best solution for this?
If you want to avoid merges (though you should not be afraid of it) you can do something like
update t2
set col1 = t1.col1
,col2 = t1.col2
from t2
join t1
on t2.[joinkey] = t1.[joinkey]
where [where clause]
And after for the ones that you do not have
insert into t2(col1,col2)
select col1,col2 from t1
where not exists (select * from t2 where t1.[joinkey] = t2.[joinkey])
in such way you first update the ones that match and then insert the ones that do not. Also if you want it in one go you can wrap it in a transaction.
It is commonly known as UPSERT operation. Yes you are correct in saying merge has some issues with it so stay away from it.
A simple approach assuming there is a Primary Key column in Both tables called PK_Col would be something like this...
BEGIN TRANSACTION;
-- Update already existing records
UPDATE T2
SET T2.Col1 = T1.Col1
,T2.Col2 = T1.Col2
FROM T2 INNER JOIN T1 ON T2.PK_COl = T1.PK_Col
-- Insert missing records
INSERT INTO T2 (COL1, COL2 )
SELECT COL1, COL2
FROM T1
WHERE T1.ID ='I'
AND NOT EXISTS (SELECT 1
FROM T2
WHERE T2.PK_COl = T1.PK_Col )
COMMIT TRANSACTION;
Wrap the whole UPSERT operation in one transaction.
You can use IF EXISTS something like:
if exists (select * from table with (updlock,serializable) where key = #key)
begin
update table set ...
where key = #key
end
else
begin
insert table (key, ...)
values (#key, ...)
end
Another solution is to check ##ROWCOUNT
UPDATE MyTable SET FieldA=#FieldA WHERE Key=#Key
IF ##ROWCOUNT = 0
INSERT INTO MyTable (FieldA) VALUES (#FieldA)