Using SQL Server 2014, this query doesn't seem to work (ie, doesn't drop the foreign key)
IF (OBJECT_ID('FK', 'F') IS NOT NULL)
BEGIN
ALTER TABLE my_table
DROP CONSTRAINT [FK]
END
but this one does
IF EXISTS(
SELECT *
FROM sys.foreign_keys
WHERE name = 'FK')
BEGIN
ALTER TABLE my_table
DROP CONSTRAINT [FK]
END
I'm trying to understand why. Especially because the first query did use to work.
Running this query does return the row with the foreign key (with type F
)
select * from dbo.sysobjects o where o.type = 'F' and name = 'FK'
But running this doesn't print the Found foreign key statement
IF (OBJECT_ID('FK', 'F') IS NOT NULL)
BEGIN
PRINT 'Found foreign key'
END
Results from suggested queries in comments
select LEN(name) as 'Len', CAST(name as varbinary(MAX)) as AsBinary, name from dbo.sysobjects o
where o.type = 'F' and name = 'my_fk_name'
gives me
108 | 0x46004B005F0049........006F006E0049006400 | my_fk_name
and
select id from dbo.sysobjects o
where o.type = 'F' and name = 'my_fk_name'
select OBJECT_NAME(TheObjectId)
gives me
my_fk_name