Total Pageviews

Negative value as an Object Id

Now from SQL Server 2012 onwards, SQL Server can assign negative values as object id to any object.
So if we are using below code to check the existence of object, can be failed from SQL Server 2012 and onwards:-

IF OBJECT_ID(‘Object_Name’)>0
BEGIN
PRINT ‘Object with the name Object_Name exist in Database’
END


So better to use either of below options from SQL Server 2012 and onwards to check the existence of object:-


SELECT TOP 1 NULL
FROM sys.objects
WHERE OBJECT_ID = ‘<mention object id>’
-- Object exist if we get NULL as a result

OR

SELECT OBJECT_ID(‘Object_Name’ IS NOT NULL
BEGIN
PRINT ‘Object with the name Object_Name exist in Database’

No comments:

Post a Comment