Convert Access delete statement to SQL Server - sql-server

I am converting Access to SQL and I got stuck at this Delete Statement because I am not sure what it does exactly.
DELETE TableA.cID, TableB.*
FROM TableA RIGHT JOIN TableB ON TableA.cID = TableB.CID2
WHERE (((TableA.cID) Is Null));
I am guessing it is something like this:
Delete
from TableA right join TableB ON TableA.cID = TableB.CID2
Where TableA.cID is null
I am not really sure if it should delete from 1 table or 2 or..

You want:
Delete TableB
from TableA right join TableB ON TableA.cID = TableB.CID2
Where TableA.cID is null
Personally, I always avoid right joins because the same thing can be stated by switching the order of the tables. I would re-write the statement like this:
DELETE B
FROM TableB AS B LEFT JOIN TableA AS A ON B.CID2 = A.cID
WHERE A.cID IS NULL
See T-SQL: Selecting rows to delete via joins for further information.
As for what that query is actually doing, here is a brief description:
Delete all rows in TableB where no matching record exists in TableA (TableA.cID IS NULL) for the condition TableB.CID2 = TableA.cID

Related

Using special character to JOIN

I have a table A which loaded from a external stage, one column col1 has special char for example 'Español'. I need to join tableA with tableB
select * from tableA
join tableB
on tableA.col1 = tableB.col1
I know tableB.col1 has exactly same value 'Español', but this join couldn't catch it. Anyone knows why and how to get it joined?
Thanks.
If the join is failing it is because the values in col1 are not equivalent even though they might look the same when displayed. I wonder if using hex_encode(col1) on each table might surface the difference?

T-SQL column that indicates one or more records exist in a separate table

I want a query that selects all records from tableA and no other records. However, I want my query to include a column that indicates that 1 or more records exist in tableB.
LEFT OUTER JOIN tableA to tableB doesn't work because if there are 2 records in tableB that relate to a record in tableA I get 2 records in the result set. I only want 1.
RIGHT OUTER JOIN doesn't work because my query returns all of the records in tableB that do not match to any records in tableA. I do not want to get records from tableB that do not match at least 1 record in tableA.
INNER JOIN also fails because I do not get all of the records in tableA; only those that contain a matching record in tableB.
It's as if I need a query like this:
SELECT tableA.ID, IF EXISTS row in tableB THEN 1 ELSE 0
FROM tableA <some sort of join> tableB on tableA.ID = tableB.FKtoTableA
Due to the fact that the goal is to merely test for existence then we highly suggest using the EXISTS clause in-line:
SELECT A.*
, CASE
WHEN EXISTS (
SELECT 1
FROM TableB B
WHERE B.Id = A.Id
) THEN 1
ELSE 0
END
FROM TableA A
Not only is this typically going to be faster than a solution that employs a LEFT JOIN + IS NOT NULL or a COUNT and has the added benefit of having semantics that agree with your problem statement.
You could use a subquery:
select
tableA.*,
(select count(*) from tableB where tableA.ID=tableB.ID) as 'Count in TableB'
from tableA
You could wrap a conditional or case statement around the subquery to give you a more Boolean value if you wanted.
You could use left join and only pull in aggregate data from b:
select a.id, cast(count(b.id) as bit) from a left join b on a.id = b.id group by a.id;
example
Converting to bit promotes any nonzero value to 1.

How to restore SQL data from one table to another

I have a backup table with 15,000 rows. The data was directly copied from a main table. I need to perform an UPDATE statement on the main table, using the data from the backup table. Both tables are identical in structure and use a unique primary key called ID.
How do I loop through the backup table, find the matching record for each row in the main table, and then replace each record in main table with the matching record in the backup table?
Thanks
EDIT:
Proposed Solution based on Sean Lange's comment:
UPDATE a
SET a.col1 = b.col1
, a.col2 = b.col2
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id
You can use a JOIN update to accomplish this. Assuming they have matching primary key ID's, it should be quick and painless.
UPDATE table1
SET a.col1 = b.col2
, a.col2 = b.col2
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id

most efficient way to quickly delete records using a join?

What is the fastest way to delete records from a SQL Server database for a large year-end purge, when the query has to join to another table? I understand cursors are slow. Do I do
DELETE FROM table1 WHERE table1_id in (SELECT table1_id FROM table2 WHERE whatever)
Seems like it might be faster to join to the table inside the query using a different technique, something like
DELETE FROM table1 WHERE table1_id = table2
A join would be the best way to go.
I assume you have a foreign key association from table2 to table1, so you're going to want to delete from the second table before the primary table.
Example:
DELETE T2
FROM table1 T1
JOIN table2 T2 ON -- JOIN CRITERIA HERE
WHERE -- FILTER CRITERIA HERE
DELETE T1
FROM table1 T1
JOIN table2 T2 ON -- JOIN CRITERIA HERE
WHERE -- FILTER CRITERIA HERE
If you attempt to delete from the primary table first, you will likely encounter a foreign key constraint violation.
Could you add a cascade delete relationship on the foreign key then get SQL server to cascade the delete for you?
Maybe something like this would be faster.
delete from (select * FROM table1 WHERE table1_id in (SELECT table1_id FROM table2 WHERE whatever))
You can try this (should be faster than IN statement, because query stops than find first value in subquery and search next value):
DELETE t1
FROM table1 t1
WHERE EXISTS (SELECT 0
FROM table2 t2
WHERE t1.table1_id = t2.table1_id
AND whatever)
Or you can do JOIN:
DELETE t1
FROM table1 t1
JOIN table2 t2 ON t1.table1_id = t2.table1_id
WHERE whatever

Delete Data from One Table using multiple tables in SQL Server

I am trying to delete some data from one table using multiple table. The problem i am having with this query is that it is deleting data that is not suppose to delete. I want to delete only the Data where ID's in both tables are DIFFERENT. In other words, i want to keep when the ID's in both tables are the same. Here is my query:
delete Tabel1
from Table1 r join
Table2 w on r.ID <> w.ID
and w.Date_Assigned is not null
You probably want to do:
delete Tabel1 where Id not in (select Id from Table2)
Your statement will probably delete all records from Tabel1, because for every record it will find at least one in Tabel2 with a different ID.
To test this, just run
select r.ID
from Table1 r join
Table2 w on r.ID <> w.ID
and w.Date_Assigned is not null
and you'll understand what the problem is.

Resources