I need to check if a column is candidate key. To make it generic, I created a stored procedure:
ALTER PROCEDURE [dbo].[CheckPK]
(
@tableName VARCHAR(100),
@pk VARCHAR(100)
)
AS
PRINT 'Checking for candidate key ' + @pk + ' for table: ' + @tableName
DECLARE @sql NVARCHAR(4000)
SET @sql = 'select count(distinct ([' + @pk + '])) as tot_pk from ' + @tableName + ' select count (*) as tot_real from ' + @tableName
EXEC sp_executesql @sql
Which works fine. What I have to do is verify that the two selected values are the same.
QUESTION: is it possible to add an if(tot_pk = tot_real)
type of condition?
I am a newbie in SQL, so I apologise if this question seems trivial. Thank you