So far I've been using the format below for creating/updating tables
IF EXISTS (SELECT 1 FROM sysobjects WHERE name = 'table_name' AND type = 'U')
DROP TABLE [dbo].[table_name]
GO
CREATE TABLE [dbo].[table_name]()
GO
But recently I came across a case where two schemas have a table with the same name. How can I check if the table exists in a specific schema? Its only the partSELECT 1 FROM sysobjects WHERE name = 'table_name' AND type = 'U'
that needs fixing, I've changed the rest to:
IF EXISTS (SELECT 1 FROM sysobjects WHERE name = 'table_name' AND type = 'U')
DROP TABLE [schema_name].[table_name]
GO
CREATE TABLE [schema_name].[table_name]()
GO
My current server version is 2008R2
so I would prefer answers that also work for that version. I have many other checks is done this way so I don't really want to completely change this pattern.
OBJECT_ID
function:IF OBJECT_ID(N'[schema_name].[table_name]') IS NOT NULL...
.