Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Pls some one tell that how to do insert,update,delete and select operation in transaction using stored procedure.Pls some one tell if possible with trigger also. Pls help me i am new to this.
Posted

Hi Chander_rani,

In SQL Stroed Proc u can do lots of stuff. The below link might be helpful for u.

http://www.c-sharpcorner.com/UploadFile/rohatash/select-insert-update-delete-using-stored-procedure-in-sql/[^]

Regards,
Babu.K
 
Share this answer
 
Comments
[no name] 27-Feb-13 7:45am    
i need transaction with stored procedure, given example is simple insert, update,delete.i want to do with transaction commit,rollback,but how pls tell me.
Babu Kumar 27-Feb-13 7:59am    
You Can do like that,

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
BEGIN TRAN

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
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
IF @@ERROR <> 0
ROLLBACK TRAN
ELSE COMMIT TRAN
END

Regards,
Babu.K
[no name] 27-Feb-13 8:02am    
thanks to you,it is very useful.
Babu Kumar 27-Feb-13 8:03am    
you are welcome. Rate my answer if you like it. :)

Regards,
Babu.K
[no name] 27-Feb-13 8:03am    
one more thing i want to know that how to handle these operation with triggers,pls help me in this.
BEGIN TRAN Atran

your sql


if @@ERROR <> 0
ROLLBACK Atran
else commit Atran
 
Share this answer
 
You Can do like that,

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
BEGIN TRAN

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
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
IF @@ERROR <> 0
ROLLBACK TRAN
ELSE COMMIT TRAN

END

Regards,
Babu.K
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900