Use test
go
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, --//Mandatory parameters
@Firstname varchar(50)=Null, --//optional parameters//--
@Lastname varchar(50)=Null,
@Salary Decimal(10, 2)=Null,
@City varchar(20)=Null,
@Statementtype nvarchar(200) = ''
)
as
Begin
if @Statementtype = 'Insert'
begin
insert into employee(id, first_name, last_name, salary, city) values(@ID, @Firstname, @Lastname, @Salary, @City)
end
if @Statementtype = 'Select'
begin
select *
from employee
end
if @Statementtype = 'Update'
begin
update employee set first_name = @Firstname, last_name=@Lastname, salary=@Salary, city=@City
where id=@ID
end
if @Statementtype = 'Delete'
begin
delete from employee where id=@ID
end
end
--For insert the records
exec MasterInsertUpdateDelete 7, 'Kallesh', 'Yadav', 30000, 'Bangalore', 'Insert'
--For update the records
exec MasterInsertUpdateDelete 4,'Sneha', 'Kumari', 23000, 'Mumbai', 'Update'
select *
from employee
--For delete the records
exec MasterInsertUpdateDelete @ID = 2, @Statementtype = 'Delete' -->We should specify the parameters name while deleting the specific records
希望对您有所帮助。 :)