0% found this document useful (0 votes)
12 views3 pages

Ms SQL Server With Store Procedure Insert123

The document provides examples of using stored procedures in Microsoft SQL Server to perform CRUD (create, read, update, delete) operations on database tables. It includes code to create tables, insert sample data, and define two stored procedures - one called MasterInsertUpdateDelete that takes parameters to insert, select, update, or delete records from the employee table; and another called CrudOperations that performs the same CRUD operations on the ProductInfo table based on the value of a @status parameter.

Uploaded by

Bala Sudhakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
12 views3 pages

Ms SQL Server With Store Procedure Insert123

The document provides examples of using stored procedures in Microsoft SQL Server to perform CRUD (create, read, update, delete) operations on database tables. It includes code to create tables, insert sample data, and define two stored procedures - one called MasterInsertUpdateDelete that takes parameters to insert, select, update, or delete records from the employee table; and another called CrudOperations that performs the same CRUD operations on the ProductInfo table based on the value of a @status parameter.

Uploaded by

Bala Sudhakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

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

You might also like