Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why OBJECT_ID used while checking if a table exists or not

I need to check if a table in SQL exist or not.

If not it must create one automatically.

Now I researched and found this code:

IF  NOT EXISTS (SELECT * FROM sys.objects  WHERE object_id = OBJECT_ID(N'[dbo].[YourTable]') AND type in (N'U'))  BEGIN CREATE TABLE [dbo].[YourTable]( .... .... .... )   END 

Can anyone explain why it says where object_id = OBJECT_ID and what should I put in its place?

like image 281
user2564223 Avatar asked Jul 09 '13 11:07

user2564223


People also ask

What does Object_id mean in SQL?

Object_ID is a unique id number for an object within the database, this is used internally by SQL Server. It should be noted, not all objects have an object_id. DDL triggers for example do not as they are not schema-scoped.

How do you check if a table exists or not?

To check if table exists in a database you need to use a Select statement on the information schema TABLES or you can use the metadata function OBJECT_ID(). The INFORMATION_SCHEMA. TABLES returns one row for each table in the current database.

How do you test if a table exists in SQL?

To check if a table exists in SQL Server, you can use the INFORMATION_SCHEMA. TABLES table. You can use this table with an IF THEN clause do determine how your query responds whether or not a table exists.

How do I find the object ID of a table?

objects catalog view, obtain the object identification numbers by querying the appropriate catalog view. For example, to return the object identification number of a DDL trigger, use SELECT OBJECT_ID FROM sys. triggers WHERE name = 'DatabaseTriggerLog``' .


2 Answers

I like this syntax:

if(object_id(N'[dbo].[YourTable]', 'U') is not null) ... 

Where object_id takes the 2 char type of object as the second parameter. You can find the list of Object types listed below in the sys.objects documentation:

  • AF = Aggregate function (CLR)
  • C = CHECK constraint
  • D = DEFAULT (constraint or stand-alone)
  • F = FOREIGN KEY constraint
  • FN = SQL scalar function
  • FS = Assembly (CLR) scalar-function
  • FT = Assembly (CLR) table-valued function
  • IF = SQL inline table-valued function
  • IT = Internal table
  • P = SQL Stored Procedure
  • PC = Assembly (CLR) stored-procedure
  • PG = Plan guide
  • PK = PRIMARY KEY constraint
  • R = Rule (old-style, stand-alone)
  • RF = Replication-filter-procedure
  • S = System base table
  • SN = Synonym
  • SO = Sequence object
  • SQ = Service queue
  • TA = Assembly (CLR) DML trigger
  • TF = SQL table-valued-function
  • TR = SQL DML trigger
  • TT = Table type
  • U = Table (user-defined)
  • UQ = UNIQUE constraint
  • V = View
  • X = Extended stored procedure
like image 116
Jason Avatar answered Sep 28 '22 13:09

Jason


The ISO SQL way to check existence of a table level object is the INFORMATION_SCHEMA.TABLES view

There's nothing wrong with looking at sys.objects but.... INFORMATION_SCHEMA.TABLES is a bit more declarative -- and it's cross platform (which often doesn't matter at all but meh still nice.)

I think this is probably more readable for a new coder though:

DECLARE @tableName SYSNAME = 'tbfoo' DECLARE @schemaNAme SYSNAME = 'fooSchema'  IF EXISTS ( SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tableName AND TABLE_SCHEMA = @schemaName ) BEGIN     RAISERROR('%s exists in schema: %s', 0, 1, @tableName, @schemaName) END ELSE BEGIN     RAISERROR('%s DOES NOT EXIST in schema: %s', 0, 1, @tableName, @schemaName) END 

Don't worry about the RAISERROR command -- its just a nice way of printing formatted messages.

You can query the INFORMATION_SCHEMA view to get a sense of what's in it.

SELECT TOP 5 * FROM INFORMATION_SCHEMA.TABLES 

As you can see -- you can reference schemas and catalogs by name rather than looking up their ID with OBJECT_ID()

like image 31
Transact Charlie Avatar answered Sep 28 '22 11:09

Transact Charlie