Wednesday, January 16

Stored Procedure for Inserting values into a Table


Stored Procedure for Inserting values into Employee table.

Consider that i already created a table called EMPLOYEE, which has Empid, Empname, Salary and Place as columns. Now i want to insert values into these columns using Stored Procedure, So that i don't need to use INSERT statements everytime. See the below Stored Procedure.


CREATE PROCEDURE [dbo].[sp_insertvalues]       

     (
       @Empid int,
     @Empname varchar(50),
@Salary money,
@Place varchar(50)
     )
As
Begin
INSERT into Employee (Empid, Empname, Salary, Placevalues (@Empid, @Empname, @Salary,@Place)
End

Now if you want to add another column or modify the created Stored Procedure, write the  query as shown below.


ALTER PROCEDURE [dbo].[sp_insertvalues]       

     (
       @Empid int,
     @Empname varchar(50),
@Salary money,
@Place varchar(50)
@Gender Varchar(5)
@Address Varchar(100)
     )
As
Begin
INSERT into Employee (Empid, Empname, Salary, Place, Gender, Addressvalues (@Empid, @Empname, @Salary, @Place, @Gender, @Address )
End


To call/Execute this Stored Procedure, see the query below.

EXEC sp_insertvalues 12'John'20000'ZZZ''M''YYY'
EXEC sp_insertvalues 14'Benn'25000'ZZZ''M''KKK'

** These values will be inserted into Employee table


** Related Topics on Stored Procrdures