Home > front end >  If not exist clause SQL statement
If not exist clause SQL statement

Time:08-24

so I found this sql query in a project I am succeeding. This is the first time I encountering this clause/statement. I understand that this is to look if the table exist before creating one and that Object_ID is the table name that is to be created. My questions are: Does sysobject mean the database? What is the Object property?

I know that it is not the columns inside the table to be created. The columns are : dtb_color_id and description.

can someone explain this to me. please?

IF NOT EXISTS(SELECT * FROM SYSOBJECTS WHERE ID = OBJECT_ID('DTB_COLOR') AND OBJECTPROPERTY(ID,'ISUserTable') = 1)

BEGIN
.......some query I understand
END

CodePudding user response:

sysobjects, OBJECTPROPERTY and OBJECT_ID are used in Microsoft SQL Server. They are part of the SQL Server DMVs and system functions/procedures used to query and manipulate the metadata.

sys.sysobjects is simply the list of all objects (tables, views, SPs, functions, etc) on the server in the active database. Please note, that sys.sysobjects is deprecated and is only available for backward compatibility. Use sys.objects instead

https://docs.microsoft.com/en-us/sql/relational-databases/system-dynamic-management-views/system-dynamic-management-views?view=sql-server-ver16

It has (as far as I know) no meaning in MySQL, unless somebody specifically created them.

You can also use INFORMATION_SCHEMA which is available in MySQL too (however slightly different in different RDBMS).

MSSQL INFORMATION_SCHEMA: https://docs.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/system-information-schema-views-transact-sql?view=sql-server-ver16

MySQL INFORMATION_SCHEMA: https://dev.mysql.com/doc/refman/8.0/en/information-schema.html


SQL Server has no CREATE TABLE IF NOT EXISTS construct, a variation of the mentioned condition is commonly used to imitate that.

This is a way in SQL Server to check if a table exists in the active database and to perform actions according to the result, like creating the table.

OBJECTPROPERTY simply checks (in this case) if the table is a user created one.

https://docs.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-ver16

I would remove the OBJECTPROPERTY condition in case the part you understand is a CREATE TABLE statement. You don't want to create a table which has a similar name to any system table/view, also you don't want to execute the CREATE TABLE if there is a VIEW with the same name (table creation will fail)

CodePudding user response:

Yes sysobject means database.

The OBJECTPROPERTY() function returns information about schema- scoped objects in the current database. Use this to check if an object is a table, view, stored procedure, etc. You can also use it to check if a table has a primary key, foreign key, foreign key reference, etc.

For more details : https://docs.microsoft.com/en-us/sql/t-sql/functions/objectpropertyex-transact-sql?view=sql-server-ver16

In this scenario it is used to check whether it is user table or not. The result of the ISUserTable property is 1 when it is user table otherwise returns 0.

Here the following steps are followed:

  • First, it executes the select statement inside the IF Exists

  • If the select statement returns a value that condition is TRUE for IF Exists

  • It starts the code inside a begin statement

DTB_COLOR - May be a stored procedure

  • Related