Issue
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
Solution
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
Answered By - Sivaranjani C Answer Checked By - Clifford M. (sql.easybugfix Volunteer)
Post a Comment
Post a Comment