Skip to content

Instantly share code, notes, and snippets.

@AmirrezaTaleb
Created April 1, 2022 21:47
Show Gist options
  • Save AmirrezaTaleb/841ee51dc55632c0ed750deecb57154f to your computer and use it in GitHub Desktop.
Save AmirrezaTaleb/841ee51dc55632c0ed750deecb57154f to your computer and use it in GitHub Desktop.
Find GUID In All Tables and Columns For SQL Server Databases Place your id as a parmether and Execute it in your database, the result will be like a photo, the script is also placed in a column so that you can easily find the id. If you want to run all the scripts together clear "Union All" from the end of the result
DECLARE @Guid UNIQUEIDENTIFIER ='2F9EC6C6-3127-412E-A3B6-4F78CDC9E689'
DECLARE @SQLQuerys NVARCHAR(MAX)=(
SELECT TOP 250 'select count(*)Counts,'''+t.name+'''TableName, '''+c.name+'''ColumnName ,'''+'select * from '+t.name+' where '+c.name+'=''''''+cast('+c.name+' as nvarchar(100))'+'+ '''''''' '+' from '+t.name+' group by '+c.name+' having '+c.name+'= '''+CAST(@Guid AS NVARCHAR(50))+''' and count( * ) >0 Union All '
FROM sys.columns c
JOIN sys.tables t ON c.object_id=t.object_id
WHERE system_type_id=36 AND t.name NOT LIKE '%temp%'
FOR XML PATH (''))
SET @SQLQuerys =REPLACE(left(@SQLQuerys,LEN(@SQLQuerys)-12),'>','>')
SET @SQLQuerys = REPLACE(@SQLQuerys, '&gt','>0')
EXEC (@SQLQuerys )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment