Learn how to create and drop procedures in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server, a procedure is a stored program that you can pass parameters into. It does not return a value like a function does. However, it can return a success/failure status to the procedure that called it.
You can create your own stored procedures in SQL Server (Transact-SQL). Let's take a closer look.
The syntax to create a stored procedure in SQL Server (Transact-SQL) is:
The name of the schema that owns the stored procedure.
The name to assign to this procedure in SQL Server.
One or more parameters passed into the procedure.
The schema that owns the data type, if applicable.
The data type for @parameter.
It is specified for cursor parameters when the result set is an output parameter.
The default value to assign to @parameter.
It means that @parameter is an output parameter.
It means that @parameter is an output parameter.
It means that @parameter can not be overwritten by the stored procedure.
It means that the source for the stored procedure will not be stored as plain text in the system views in SQL Server.
It means that a query plan will not be cached for this stored procedure.
It sets the security context to execute the stored procedure.
It means that the stored procedure is executed only during replication.
Let's look at an example of how to create a stored procedure in SQL Server (Transact-SQL).
The following is a simple example of a procedure:
This procedure is called FindSite. It has one parameter called @site_name which is an output parameter that gets updated based on the variable @site_id.
You could then reference the new stored procedure called FindSite as follows:
Once you have created your procedure in SQL Server (Transact-SQL), you might find that you need to remove it from the database.
The syntax to a drop a stored procedure in SQL Server (Transact-SQL) is:
The name of the stored procedure that you wish to drop.
Let's look at an example of how to drop a stored procedure in SQL Server.
For example:
This DROP PROCEDURE example would drop the stored procedure called FindSite.