Trying to figure out a SQL script to test the existence of primary keys in cerain tables. If the table has no primary key, then the script should output the table name.
Tables to test:
TableA
TableB
TableC
After running the script (and lets say TableA and TableC have PKs, but not TableB), then the output would be below:
NoKeys
TableB
My version:
SELECT 1
FROM SYS.KEY_CONSTRAINTS
WHERE [TYPE] = 'PK' AND [PARENT_OBJECT_ID] = OBJECT_ID('USER.USER');
As a reference check Microsoft docs here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With