MS SQL SERVER WITH STORE PROCEDURE
INSERT,DELETE,UPDATE
CREATE TABLE employee(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR(10),
last_name VARCHAR(10),
salary DECIMAL(10,2),
city VARCHAR(20),
INSERT INTO employee VALUES (2, 'Monu', 'Rathor',4789,'Agra');
GO
INSERT INTO employee VALUES (4, 'Rahul' , 'Saxena', 5567,'London');
GO
INSERT INTO employee VALUES (5, 'prabhat', 'kumar', 4467,'Bombay');
go
INSERT INTO employee VALUES (6, 'ramu', 'kksingh', 3456, 'jk');
go
select * from employee
create PROCEDURE MasterInsertUpdateDelete
(
@id INTEGER,
@first_name VARCHAR(10),
@last_name VARCHAR(10),
@salary DECIMAL(10,2),
@city VARCHAR(20),
@StatementType nvarchar(20) = ''
)
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
insert into employee (id,first_name,last_name,salary,city) values( @id,
@first_name, @last_name, @salary, @city)
END
IF @StatementType = 'Select'
BEGIN
1
select * from employee
END
IF @StatementType = 'Update'
BEGIN
UPDATE employee SET
First_name = @first_name, last_name = @last_name, salary = @salary,
city = @city
WHERE id = @id
END
else IF @StatementType = 'Delete'
BEGIN
DELETE FROM employee WHERE id = @id
END
end
SECOND STOREPROCEDURE EXAMPLES
CREATE TABLE ProductInfo
(
ProductId INT IDENTITY,
Productname VARCHAR(50),
Price INT
)
CREATE PROCEDURE CrudOperations
@productid int = 0,
@productname varchar(50)=null,
@price int=0,
@status varchar(50)
AS
BEGIN
SET NOCOUNT ON;
--- Insert New Records
IF @status='INSERT'
BEGIN
INSERT INTO productinfo(productname,price) VALUES(@productname,@price)
END
--- Select Records in Table
IF @status='SELECT'
BEGIN
SELECT productid,productname,price FROM productinfo
END
--- Update Records in Table
IF @status='UPDATE'
BEGIN
UPDATE productinfo SET productname=@productname,price=@price WHERE
productid=@productid
END
--- Delete Records from Table
IF @status='DELETE'
BEGIN
DELETE FROM productinfo where productid=@productid
2
END
SET NOCOUNT OFF
END