Skip to content

Instantly share code, notes, and snippets.

@kmoormann
Created July 26, 2013 16:33
Show Gist options
  • Save kmoormann/6090291 to your computer and use it in GitHub Desktop.
Save kmoormann/6090291 to your computer and use it in GitHub Desktop.
--USE <REPLACE WITH DATABASE NAME>
CREATE TABLE #tablesOfInterest (TableName VARCHAR(2000))
INSERT INTO #tablesOfInterest
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME
IN ('table1'
,'table2'
)
SELECT tableName, keyColumn.constraint_name as PrimaryKey
FROM #tablesOfInterest toi
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE keyColumn
ON toi.tableName = keyColumn.TABLE_NAME
AND OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
WHERE keyColumn.Table_Name IS NULL
DROP TABLE #tablesOfInterest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment