Monday 29 July 2013

Update data in one table and insert data in another Table


-----Update data in one table and insert data in another Table---------

Create proc SP_InsertUpdate
(
---
---

as
BEGIN


begin try
Begin Transaction

Update table
set phone=921393
where ID=@ID

Insert into Table2
values(3,2,5)

Commit Trancation
End Try


Begin Catch

Rollback Transaction

End Catch


END



--------------------------------------------------------------------------------------------





WORKING with Stored Proc with Insert /Update:

use Working
go
Create proc InsertValues
(
@Studentid int,
@Firstname nvarchar(50),
@Lastname nvarchar(50),
@Email nvarchar(50)
)
as
begin

if @studentid =0   -- does not allow duplicates values
begin
UPDATE tbl_Students
   SET Firstname = @Firstname,
       Lastname = @Lastname,
       Email = @Email
    Where Studentid=@Studentid

end
else
begin


insert into tbl_Students(Studentid,Firstname,lastname,Email) values (@Studentid,@Firstname,@lastname,@Email)
    set @Studentid = @@IDENTITY;
    end
    end
    go
  
  
>execute InsertValues 5,'XHema','Xlatha','XHemalatha@hotmail.com'



 >SELECT * FROM [Working].[dbo].[tbl_Students]
 >Select @@rowcount
 

No comments:

Post a Comment