PROCEDURE (Procedure) in SQL Server

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:

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

For example

 CREATE PROCEDURE spNhanvien 
@nhanvien_name VARCHAR (50) OUT

AS

BEGIN

DECLARE @nhanvien_id INT;

SET @nhanvien_id = 8;

IF @nhanvien_id <10
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.

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 example

 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

4 ★ | 1 Vote

May be interested

  • Deploying Network Access Quarantine Control, Part 2Deploying Network Access Quarantine Control, Part 2
    i stepped qua cách làm việc truy cập của quarantine mạng (naqc) works and đã cung cấp chi tiết điều khiển instructions. in the second and final installment, i'll continue the procedure by finishing the deployment, then discuss how isa server 2004's entrance to the marketplace changes the field of naqc and how to quarantining is implemented within the isa server itself.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • What is VPS? VPS used to do? What is VPS different from Server?What is VPS? VPS used to do? What is VPS different from Server?
    what is vps? vps used to do? what is vps different from server ?. when you intend to learn about network data or open the website, you will definitely be introduced to many different server and server services. but server hosting has a lot of tricks
  • What is GRPC (Google Remote Procedure Call)?What is GRPC (Google Remote Procedure Call)?
    google remote procedure call (grpc) is an http/2-based rpc protocol that allows clients and servers to communicate via methods defined in protobuffers.
  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • Instructions for setting up and managing FTP Server on Windows 10Instructions for setting up and managing FTP Server on Windows 10
    if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
  • Create VPN Server on Windows 8Create VPN Server on Windows 8
    no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
  • What is the future of server virtualization?What is the future of server virtualization?
    server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.
  • Learn about the architecture of MS SQL ServerLearn about the architecture of MS SQL Server
    in the previous articles, you already know briefly about sql server, how to install sql server on the computer. in this section we will learn about the architecture of sql server.