Introduction
In this article am going to explain how to create and execute parameterized stored procedures From another stored procedure, how to prevent SQL Injection attacks, and how to insert data in the table using stored procedure in SQL Server for example. And also show you how you can use the procedure in SQL Server for example.
SQL Server Stored Procedure |
When You working with any data-driven application you need the database and also need some data manipulation operations in DBMS(Database Management System) such as selection, insertion, updating, deletion and etc.
Requirement
1) Insert data in the table using a stored procedure.
Implementation
Before, start the actual implementation of our example we need a database for demonstration, So first we will create a database.
Create Database
CREATE DATABASE db_Codingvila
Syntax
CREATE PROCEDURE Your_Procedure_Name -- list of parameters i.g: @Id INT = 0, @EmpName VARCHAR(50)='' AS BEGIN -- Sql statements END
Create First Stored Procedure
--**************************************** -- CREATED ON 20/11/2018 BY NIKUNJ SATAIYA --**************************************** -- Create First Stored Procedure CREATE PROCEDURE Employee_Insert @EmpId INT, @EmpName VARCHAR(50), @EmpDesignation VARCHAR(50) AS BEGIN -- Declare temporary table DECLARE @Temp1 TABLE (EmployeeId INT, EmployeeName VARCHAR(50), EmployeeDesignation VARCHAR(50)) -- insert records in temporary table INSERT INTO @temp1 (EmployeeId, EmployeeName, EmployeeDesignation) VALUES(1,'Nikunj Satasiya','Software Engineer'), (2,'Hiren Dobariya','Web Developer'), (3,'Vivek Ghadiya','Business Development Executive'), (4,'Pratik Pansuriya','Business Development Executive'), (5,'Milan Lathiya','Software Engineer') -- Select all records from temporary table SELECT EmployeeId, EmployeeName, EmployeeDesignation FROM @Temp1 -- Select specific records from temporary table SELECT EmployeeId, EmployeeName, EmployeeDesignation FROM @Temp1 WHERE EmployeeId = @EmpId AND EmployeeName = @EmpName AND EmployeeDesignation = @EmpDesignation END
Create Second Stored Procedure
Now it's time to call/Execute created the stored procedure and insert data in the table and also display the result in tabular format, for that here we will create another stored procedure and will call/Execute the developed stored procedure.
--**************************************** -- CREATED ON 20/11/2018 BY NIKUNJ SATAIYA --**************************************** -- Create Second Stored Procedure CREATE PROCEDURE Employee_GetData @Id INT, @Name VARCHAR(50), @Designation VARCHAR(50) AS BEGIN -- Execute First Procedure EXECUTE Employee_Insert @EmpId = @Id , @EmpName = @Name, @EmpDesignation = @Designation END
--Execute Second Procedure By Passing Paramters EXEC Employee_GetData @id=1, @name='Nikunj Satasiya', @Designation ='Software Engineer'
Output
SQL Server Stored Procedure Example |
Summary
This article explains how to create a stored procedure in an SQL server, how you can do data manipulation operations ad also how you can protect your database from the authorized activity and prevent data-driven attacks like SQL Injection. I hope this article helps you, if you have any queries then you can leave your comments in the comment box thank you.