Hello folks if you want to learn about Stored Procedures in SQL and how it can help you to create a layer of abstraction around functionality then you have come to the right place. With the help of many examples, we will learn how to construct stored procedures in SQL Server.
A SQL Server stored procedure is a collection of statements saved in the database as a logical entity. The stored procedure takes the parameters and runs the T-SQL statements, returning the result set if there is one. We now know what is a stored procedure in SQL. But, why use them? What are their benefits? Let's see and search for the answer to these questions.
Benefits of using Stored procedures
Creating database
CREATE TABLE Product (ProductID INT, ProductName VARCHAR(100) ); CREATE TABLE ProductDescription (ProductID INT, ProductDescription VARCHAR(400) ); INSERT INTO Product VALUES (680,'HL Road Frame - Black, 58') ,(706,'HL Road Frame - Red, 58') ,(707,'Sport-100 Helmet, Red'); INSERT INTO ProductDescription VALUES (680,'Replacement mountain wheel for entry-level rider.') ,(706,'Sturdy alloy features a quick-release hub.') ,(707,'Aerodynamic rims for smooth riding.');
Creating a stored procedure
CREATE PROCEDURE GetProductDesc AS BEGIN SET NOCOUNT ON SELECT P.ProductID,P.ProductName,PD.ProductDescription FROM Product P INNER JOIN ProductDescription PD ON P.ProductID=PD.ProductID END
Creating a stored procedure with parameters
CREATE PROCEDURE GetProductDesc_withparameters (@PID INT) AS BEGIN SET NOCOUNT ON SELECT P.ProductID,P.ProductName,PD.ProductDescription FROM Product P INNER JOIN ProductDescription PD ON P.ProductID=PD.ProductID WHERE P.ProductID=@PID END
EXEC GetProductDesc_withparameters 706
Creating a stored procedure with a default value
CREATE PROCEDURE GetProductDesc_withDefaultparameters (@PID INT =706) AS BEGIN SET NOCOUNT ON SELECT P.ProductID,P.ProductName,PD.ProductDescription FROM Product P INNER JOIN ProductDescription PD ON P.ProductID=PD.ProductID WHERE P.ProductID=@PID END
Creating an encrypted stored procedure
CREATE PROCEDURE GetEmployees WITH ENCRYPTION AS BEGIN SET NOCOUNT ON SELECT EmpID,EmpName from Employee END
Conclusion
In this article, we explored SQL Server stored procedures with different examples. we have seen many kinds of stored procedures including parameters, default values, and encrypted too. We also have gone through the benefits of stored procedures. This will help you guys use and utilize a stored procedure. Hope you guys enjoyed it. see you in the next article!- 4 Examples of CASE expression in SQL Server
- How to use Stored Procedure in SQL?
- How to use WHERE and HAVING clause in SQL
- Difference between Self and Equi join in SQL
- Difference between UNION and UNION ALL in SQL
- What are T-SQL Window Functions?
- MySQL vs NoSQL comparison
- 10 Examples of ALTER command in SQL
- How to find top 10 records in a table?
Thanks for reading this article so far. IF you like it then please share and if you have any questions, feel free to ask.
No comments:
Post a Comment