I have about 50 tables with created-by and changed-by referencing the user table. When I want to delete the user, I would like to create a list of what tables this user has data in, so one can go delete/edit everything belonging to said user. Is there any way of doing this in SQL?
Have you tried this one http://sqlserver2000.databases.aspfaq.com/schema-how-do-i-find-all-the-foreign-keys-in-a-database.html
Not exactly sure of what you're going for here..
If you want a list of foreign keys referencing a table try this question http://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server-2005/483266
A couple of methods for finding the tables that reference your User table through foreign key relationships have been posted and here's another: http://www.sqlservercurry.com/2009/03/simple-stored-procedure-to-find-foreign.html
Once you get the list of tables, you're going to have to write the SQL to determine which tables have records for a specific user. There's no way around that. Since the queries will all look the same, you could paste the table list into Excel, use a formula to build the queries, then paste the queries back into SSMS.
this will list all tables where your ID exists:
DECLARE @Query varchar(1000)
DECLARE @MaxRow int
DECLARE @CurrentRow int
DECLARE @CurrentTable varchar(500)
DECLARE @UserID int
SET @UserID=???
CREATE TABLE #Rows
(
RowID int not null primary key identity(1,1)
,TableWithForeignKey varchar(500)
)
CREATE TABLE #Temp
(
RowValue int
)
INSERT INTO #Rows
(TableWithForeignKey )
select
t.name as TableWithForeignKey --, fk.constraint_column_id as FK_PartNo , c.name as ForeignKeyColumn
from sys.foreign_key_columns as fk
inner join sys.tables as t on fk.parent_object_id = t.object_id
inner join sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where fk.referenced_object_id = (select object_id from sys.tables where name = '????????')
--order by TableWithForeignKey, FK_PartNo
SELECT @MaxRow=@@ROWCOUNT
SET @CurrentRow=0
SELECT * FROM #Rows
WHILE @CurrentRow<@MaxRow
BEGIN
SET @CurrentRow=@CurrentRow+1
SELECT @CurrentTable=TableWithForeignKey FROM #Rows WHERE RowID=@CurrentRow
--SET @Query='DELETE FROM '+@CurrentTable+' WHERE UserID='+CONVERT(varchar(20),@UserID)
SET @Query='SELECT COUNT(*) FROM '+@CurrentTable+' WHERE YourIDhere='+CONVERT(varchar(20),@UserID)
PRINT @Query
INSERT INTO #Temp
EXECUTE (@Query)
IF NOT EXISTS (SELECT RowValue FROM #Temp WHERE RowValue>0)
BEGIN
PRINT 'no matches found'
DELETE #Rows WHERE RowID=@CurrentRow
END
ELSE
BEGIN
PRINT 'matches found!!'
END
DELETE #Temp
END
--list all tables where the ID exists
SELECT * FROM #Rows
SQL 2005+ provides a number of system views like sys.tables and sys.foreign_key_columns which may help you.
SELECT
pt.name AS ParentTable,
pc.name AS ParentColumn,
rt.name AS ReferencedTable,
rc.name AS ReferencedColumn
FROM sys.foreign_key_columns fkc
INNER JOIN sys.tables pt ON pt.object_id = fkc.parent_object_id
INNER JOIN sys.columns pc ON pc.column_id = fkc.parent_column_id AND
pc.object_id = fkc.parent_object_id
INNER JOIN sys.tables rt ON rt.object_id = fkc.referenced_object_id
INNER JOIN sys.columns rc ON rc.column_id = fkc.referenced_column_id AND
rc.object_id = fkc.referenced_object_id