Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

SQL Server Procedure: PROCEDURE (Procedure) in SQL Server

Understand SQL Server Procedure: PROCEDURE (Procedure) in SQL Server with clear explanations, practical examples, and useful tips. This updated guide covers...

Table of Contents

SQL Server Procedure: PROCEDURE (Procedure) in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

Procedure is a program in a database of multiple statements that you save for later use. In SQL Server, you can pass parameters to the procedure, although it does not return a specific value as a function but indicates the successful or failed execution.

The article will give you the syntax and examples of how to create and delete procedures in SQL Server.

CREATE PROCEDURE

Syntax

To create a procedure in SQL Server, we use the following syntax:

 CREATE {PROCEDURE | PROC } [schema_name.]procedure_name PROC} [schema_name.] Procedure_name 
 [@parameter [type_schema_name.] datatype 
 [VARYING] [= default] [OUT | OUTPUT | READONLY] 
 , @parameter [type_schema_name.] datatype 
 [VARYING] [= default] [OUT | OUTPUT | READONLY]] 

 [WITH {ENCRYPTION | RECOMPILE | RECOMPILE | EXECUTE AS Clause } ] EXECUTE AS Clause}] 
 [FOR REPLICATION] 

 AS 

 BEGIN 
 [declaration_section] 

 executable_section 

 END; 

Parameters:

  • schema_name: Schema name (schema) owns the procedure.
  • procedure_name: The assigned name for the procedure
  • @parameter: One or more parameters are passed into the function.
  • type_schema_name: Data type of schema (if any).
  • Datatype: Data type for @parameter.
  • Default: The default value assigned to @parameter.
  • OUT / OUTPUT: @parameter is an output parameter  
  • READONLY: @parameter cannot be overridden by the procedure.
  • ENCRYPTION: The source code of the procedure will not be stored as text in the system.
  • RECOMPILE: The query will not be cached (cache) for this procedure.
  • EXECUTE AS clause: Specifies the security context to execute the procedure.
  • FOR REPLICATION: The saved procedure will only be executed during the replication process.

For instance,

CREATE PROCEDURE spNhanvien 
 @nhanvien_name VARCHAR (50) OUT 

 AS 

 BEGIN 

 DECLARE @nhanvien_id INT; 

 SET @nhanvien_id = 8; 

 IF @nhanvien_id

The above procedure is named spNhanvien, there is a parameter of @nhanvien_name, the output of the parameter will be based on @nhanvien_id.

After that, you can perform the spNhanvien reference as follows:

 USE [test] 
 GO 

 DECLARE @site_name varchar (50); 

 EXEC FindSite @site_name OUT; 

 PRINT @site_name; 

 GO 

Drop Procedure

Once you've created the procedure successfully, there are also cases where you want to remove the procedure from the database for a few reasons.

Syntax

To remove a procedure, we have the following syntax:

DROP PROCEDURE procedure_name ;

Parameters:

procedure_name: The name of the procedure you want to delete.

For instance,

 DROP PROCEDURE spNhanvien; 

By executing this command, you have just deleted the spNhan procedure from the database.

Previous post: FUNCTION (Function) in SQL Server

Next lesson: IF. ELSE command in SQL Server

FAQ

What should you know about cREATE PROCEDURE Syntax?

To create a procedure in SQL Server, we use the following syntax:

What should you know about for instance, CREATE PROCEDURE spNhanvien @nhanvien_name VARCHAR (50) OUT AS BEGIN DECLARE @nhanvien_id INT; SET @nhanvien_id = 8; IF @nhanvien_id SET @nhanvien_name = 'Smith'; ELSE SET @nhanvien_name = 'Lawrence'; END; The above procedure is named spNhanvien, there is a parameter of @nhanvien_name, the output of the parameter will be based on @nhanvien_id. After that, you can perform the spNhanvien reference as follows: USE [test] GO DECLARE @site_name varchar (50); EXEC FindSite @site_name OUT; PRINT @site_name; GO Drop Procedure?

Once you've created the procedure successfully, there are also cases where you want to remove the procedure from the database for a few reasons.

What should you know about syntax?

To remove a procedure, we have the following syntax:

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.