Wednesday, 17 August 2016

Single Stored Procedure for Select Insert Update Delete in SQL Server

Single Stored Procedure for Select Insert Update Delete in SQL Server

I have made use of the following table tbl_course










The following stored procedure will be used to perform Select, Insert, Update and Delete operations on the Customers table of the SQL Server database.


Create PROCEDURE [dbo].[AddCourse]
(
    
    @id VARCHAR(50)=null,
    @Name   VARCHAR(200)=null,
@Duration   VARCHAR(200)=null,
@Fee   VARCHAR(200)=null,
@StatementType nvarchar(20) = ''
)
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
insert into tbl_course values(@Name, @Duration,@Fee)   
END
 
IF @StatementType = 'Select'
BEGIN
select * from tbl_course order by id desc
END 

IF @StatementType = 'SelectId'
BEGIN
select * from tbl_course where id = @id
END  

IF @StatementType = 'Search'
BEGIN
select * from tbl_course where Name like '%'+@Name+'%'
END  

IF @StatementType = 'Update'
BEGIN
UPDATE tbl_course SET
            Name =  @Name
, Duration=@Duration,
Fee = @Fee
      WHERE id = @id
END
IF @StatementType = 'Delete'
BEGIN
DELETE FROM tbl_course WHERE  id = @id
END
end

No comments:

Post a Comment