(I have already seen this answer, but this does not answer my specific question).
I have a table - in MSSQLServer - named Files, with many rows. There are FK relationships to Files from many other tables. I would like to delete all orphaned rows from Files (i.e. any row where there is no FK relationship to it from any other table).
If I attempt to delete a row manually from within the Data view (in SQL Server view of VS) then this will succeed if the row is an orphan, but fail if there is an FK reference from any table - so (as I would expect) the database is checking this. But how can I either delete all such orphaned rows, or, preferably, retrieve them in a SQL query to inspect them.
To address something you said in the comments:
This is not possible without specifying the tables to check against, but while you may not know what all the FKs are off the top of you head, as long as they are defined the system does and you can query that to build your query dynamically to check all child tables:
So with Adventureworks if you use
HumanResources.Employee
as the input table, you end up with SQL something like:Examples on db<>fiddle