Translate

Tuesday, 13 June 2017

sql : create procedure using transaction and output parameter


CREATE TABLE Customers(
CustomerId INT  PRIMARY KEY IDENTITY(1,1) NOT NULL,
Name    VARCHAR(100) NULL,
Country VARCHAR(100) NULL,
City    VARCHAR(100) NULL
);
INSERT INTO Customers(Name,Country,City) VALUES('PABITRA','INDIA','PUNE');
INSERT INTO Customers(Name,Country,City) VALUES('PRAKASH','INDIA','GOA');

CREATE PROC USP_Sample
(
@CustomerId INT=NULL,
@Name    VARCHAR(50)=NULL
@MSG                VARCHAR(100)=NULL OUTPUT
)
AS BEGIN
  DECLARE @errorCount INT,@STATUS VARCHAR(50);
  SET @errorCount=0;
  SET NOCOUNT ON;
  SET XACT_ABORT ON;

BEGIN TRAN _SampleTransation
  SELECT CustomerId,Name,Country,City FROM Customers WHERE CustomerId=@CustomerId AND Name=@Name;

IF(@@ERROR<>0)
  BEGIN
     SET @MSG='An Error Occurs Please try again .';
     SET @errorCount=1;
  END
IF(@errorCount<>0)
    ROLLBACK TRAN _SampleTransation
  ELSE
    COMMIT TRAN _SampleTransation

END

No comments:

Post a Comment