SQL Server Create And Execute Parameterized Stored Procedure From Another Stored Procedure

Introduction

 
In this article I am going to explain how to create and execute parameterized stored procedure from another stored procedure, how to prevent SQL Injection attacks, and how to insert data in the table using stored procedure in SQL Server with an example. And also I'll show you how you can use the procedure in SQL Server with an example.
 
When working with any data-driven application you need the database and you also need some data manipulation operations in DBMS (Database Management System) such as selection, insertion, updating, deletion and etc.
 
Your database may contain some useful and confidential information so it is necessary to protect it from any unauthorized activity and implement some security credentials with your DBMS (Database Management System) to secure your data from unauthorized activity.
 
If you're working with an SQL server then a stored procedure is one of the best options for data manipulation operations because a stored procedure accepts parameters as an argument, and parameterized statements prevent code injection techniques such as "SQL Injection" that might destroy your database.
 
So, here I will show you how you can use stored procedures in your SQL server database.
 
Requirements
  1. Insert data in the table using a stored procedure.
  2. Create a Stored Procedure in SQL Server.
  3. Call or Execute Stored Procedure in SQL Server.
  4. Call or Execute Stored Procedure From Another Stored Procedure in SQL Server.
  5. Select and Display Inserted Data in Tabular Format in SQL Server Using Stored Procedure.

Implementation

 
Before starting the actual implementation of our example we need a database for demonstration, so first we will create a database.
 
Create Database
  1. CREATE DATABASE db_Codingvila  
Before creating a stored procedure I will show you the syntax of the stored procedure in SQL Server.
 
Syntax
  1. CREATE PROCEDURE Your_Procedure_Name  
  2. -- list of parameters i.g: @Id INT = 0, @EmpName VARCHAR(50)=''  
  3. AS  
  4. BEGIN  
  5. -- SQL statements  
  6. END  
Now, we will start to write our stored procedure something like,
 
Create First Stored Procedure
  1. CREATE PROCEDURE Employee_Insert  
  2. @EmpId INT,   
  3. @EmpName VARCHAR(50),  
  4. @EmpDesignation  VARCHAR(50)  
  5. AS  
  6. BEGIN  
  7. -- Declare temporary table  
  8.    
  9. DECLARE @Temp1 TABLE (EmployeeId INT, EmployeeName VARCHAR(50), EmployeeDesignation VARCHAR(50))  
  10.    
  11. -- insert records in the temporary table  
  12.    
  13. INSERT INTO @temp1 (EmployeeId, EmployeeName, EmployeeDesignation)  
  14.      VALUES(1,'Nikunj Satasiya','Software Engineer'),  
  15.      (2,'Hiren Dobariya','Web Developer'),  
  16.      (3,'Vivek Ghadiya','Business Development Executive'),  
  17.      (4,'Pratik Pansuriya','Business Development Executive'),  
  18.      (5,'Milan Lathiya','Software Engineer')  
  19.        
  20. -- Select all records from the temporary table      
  21. SELECT EmployeeId, EmployeeName, EmployeeDesignation FROM @Temp1  
  22. -- Select specific records from temporary table  
  23. SELECT EmployeeId, EmployeeName, EmployeeDesignation FROM @Temp1 WHERE EmployeeId = @EmpId AND EmployeeName = @EmpName AND EmployeeDesignation = @EmpDesignation  
  24.    
  25. END  
If you analyzed the above procedure then you can see @EmpId, @EmpName, and @EmpDesignation where @ can be described as a parameter.
 
Now, I will show you how to create a second stored procedure and how you can insert data in the table using the first created stored procedure, and for that, I used a temporary table.
 

Create Second Stored Procedure

 
Now it's time to call/execute/create the stored procedure and insert data in table and also display the result in tabular format, for that here we will create another stored procedure and will call/execute the created stored procedure.
  1. CREATE PROCEDURE Employee_GetData  
  2. @Id INT,   
  3. @Name VARCHAR(50),  
  4. @Designation  VARCHAR(50)  
  5. AS  
  6. BEGIN  
  7. -- Execute First Procedure  
  8. EXECUTE Employee_Insert @EmpId = @Id , @EmpName = @Name, @EmpDesignation = @Designation  
  9. END  
Again if you analyze the above procedure then you can see our first created stored procedure is executed with "EXEC." You  can also use "EXECUTE", where @Id, @Name, and @Designation send parameters to the first stored procedure.
  1. --Execute Second Procedure By Passing Parameters  
  2. EXEC Employee_GetData @id=1, @name='Nikunj Satasiya', @Designation ='Software Engineer'  
Now we will call/execute our second created procedure that will call/execute our first created procedure with the actual result as per our requirement.
 
After the successful execution of this procedure, you can see results as shown in the output window.
 
Output
 
 

Summary

 
This article explains how to create a stored procedure in a SQL server, how you can do data manipulation operations ad also how you can protect your database from unauthorized activity and prevent data-driven attacks like SQL Injection. I hope this article helps you, if you have any query then you can leave your comments in the comment box. Thank you.
 


Similar Articles
Codingvila
Codingvila is an educational website, developed to help tech specialists/beginners.