SQL Server many-to-many query - sql-server

I have two tables, let's call them table1 and table2. They both have a column called ID1 and ID2 which are respective PK for each of two tables.
I have also another table, called table3 which contains ID1 and ID2, establishing a many to many relation between these two tables.
What I need is to get all the records from table2 that are not related with records in table1.
Ex.
Table 1 contains a record with ID = 1
Table 2 contains two records with ID 1, 2
Table 3 contains one record with values 1 - 1
I need a query that will give me as result 2.
Can anyone suggest me a way to proceed?
Thanks

SELECT t2.ID2
FROM table2 t2
WHERE NOT EXISTS(SELECT NULL
FROM table3 t3
WHERE t3.ID2 = t2.ID2);
You could also use a LEFT JOIN:
SELECT t2.ID2
FROM table2 t2
LEFT JOIN table3 t3
ON t2.ID2 = t3.ID2
WHERE t3.ID2 IS NULL;

Related

Cascade delete rows from multiple tables in SQL

I have 3 tables like this:
Table1: Id, Name
Table2: Id, Name
Table3: Id, EntityName (nvarchar), EntityId (int)
There are no foreign keys on Table3. I am inserting rows for Table1 and Table2 to Table3 like this:
Id
EntityName
EntityId
OtherColumn(s)
1
Table1
1
...
2
Table2
1
...
3
Table2
2
...
When I delete a row from Table1 or Table2, how can I cascade it from Table3?
You can create a delete trigger on each table to delete the matching rows from table3, for example:
create trigger Table1Delete on dbo.table1
for delete
as
if ##RowCount = 0 return
set nocount on
delete from t3
from deleted d
join table3 t3 on t3.EntityName='Table1' and d.Id=t3.EntityId
And a similar one for Table2
This is how you could write queries to "mimic" cascade delete:
delete from table2 t2
where exists(select 1 from table3
where t2.id = entityId and EntityName = 'Table2')
delete from table1 t1
where exists(select 1 from table3
where t1.id = entityId and EntityName = 'Table1')
If you have some condition upon which you delete from table3 you should also inlcude it in those queries.
UPDATE
To do it automatically, you need todefine foreign keys with cascade delete action. But only one foreign key can be defined on column, thus you'd need to have two columns - one for referencing table1 and second for referncing table2. Then on both you'd need define cascade delete.
With current design (one column to reference multiple tables) this isn't possible and you would need to work around (as suggested implement delete trigger on table).

Updating a column in SQL table where items are identified in another table that is linked

I have two tables in MSSQL.
Table1
Table2
I want to update the Status column in Table 1 to "YES" if the same Ticket ID, House and Part Number exists in Table 2. After updating, Table 1 should be like,
How can I achieve this?
Thanks
a simple EXISTS() will do the job
UPDATE t1
SET Status = 'Yes'
FROM Table1 t1
WHERE EXISTS
(
SELECT *
FROM Table2 t2
WHERE t1.TicketID = t2.TicketID
AND t1.House = t2.House
AND t1.PartNumber = t2.PartNumber
)
or an INNER JOIN will gives you the query that you want

sql server join for nonmatch

what ever BU is present in table-2 that should be present in table-1 BU so write a query to find out the records available in table-2 whose BU is not available in table-1 BU.
Try to use left join where Table2 is left table so that you will get all the records from Table2 and matching records from Table1. And for those records from Table2 there is no match, result will shoe NULL entries for Table1. That's it-- that's what you are looking for --
SELECT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL
SELECT DISTINCT T2.BU FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T2.BU = T1.BU
WHERE T1.BU IS NULL

With TSQL, how do I create a record in Table2 using GUID from existing record in Table1

Table1 contains Work Orders and Table2 contains Installers. I would like to insert a record into Table2 for Work Order 3906 in Table1 using unique identifier for that Work Order such that Table1GUID = Table2GUID . I'll worry about populating the new record later (all columns in Table2 can be blank for now except GUID which must be identical to corresponding GUID from Table1). Records for Table1 and Table2 are one to one.
This is NOT working: (0 row(s) affected)
INSERT INTO Table2 (Table2GUID)
SELECT Table1GUID FROM Table1
WHERE Table1.WOnumber = '3906'
This doesn't work either:
INSERT INTO Table2 (Table2GUID)
SELECT Table1GUID FROM Table1
INNER JOIN Table2 ON Table2GUID = Table1GUID
WHERE Table1.WOnumber = '3906'

Select data (join?) from only one table using an id from another table

I have two tables, that we'll call t1 and t2. I want to select the data in t1 that has a certain ID that I can only find using a where clause in t2. I don't want to select the data in t2 (many duplicate column names with different data) so how do I do that?
try this
select * from t1 where t1.Id in (select distinct Id from t2)
Another approach is to join the tables
SELECT * FROM t1
JOIN t2 on t1.id = t2.id
You are joining them on a specific ID common between the 2 tables.

Resources