Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
908 views
in SQL Queries by 21 20 26

In SQL Server, I have an employee table and I created a function that returns data from the table.
I want to bind a function with a table to prevent delete The table because the function depends on it

Here is the function :

Create Function [dbo].[EmployeeGender] (@Gender Nvarchar (10))
Returns Table 
As
Return (Select ID,Name,Gender from Employee where Gender = @Gender )

 


1 Answer

0 like 0 dislike
by 24 26 40

Prevent Delete table in SQL Database

To prevent delete table you can add schemabinding to your function as below

Create Function [dbo].[EmployeeGender] (@Gender Nvarchar (10))
Returns Table 
with schemabinding 
As
Return (Select ID,Name,Gender from Employee where Gender = @Gender )

SCHEMABINDING: Specifies that the function is bound to the database objects that it references. When SCHEMABINDING is specified, the base objects cannot be modified in a way that would affect the function definition. The function definition itself must first be modified or dropped to remove dependencies on the object that is to be modified.

If you don’t ask, the answer is always NO!
...