Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.91 KB | None | 0 0
  1. CREATE PROC usp_TransferMoney(@SenderId INT, @ReceiverId INT, @Amount DECIMAL(15, 4))
  2. AS
  3. BEGIN TRANSACTION
  4.         DECLARE @senderAccount INT =
  5.         (
  6.             SELECT Id
  7.               FROM Accounts
  8.              WHERE Id = @SenderId
  9.         );
  10.  
  11.         DECLARE @receiverAccount INT =
  12.         (
  13.             SELECT Id
  14.               FROM Accounts
  15.              WHERE Id = @ReceiverId
  16.         );
  17.  
  18.         IF (@senderAccount IS NULL)
  19.         BEGIN
  20.             ROLLBACK;
  21.             RAISERROR('Invalid sender account.', 16, 1);
  22.             RETURN;
  23.         END
  24.  
  25.         IF (@receiverAccount IS NULL)
  26.         BEGIN
  27.             ROLLBACK;
  28.             RAISERROR('Invalid receiver account.', 16, 2);
  29.             RETURN;
  30.         END;
  31.  
  32.         IF (@Amount < 0)
  33.         BEGIN
  34.             ROLLBACK;
  35.             RAISERROR('Amount should be bigeer than zero.', 16, 3);
  36.             RETURN;
  37.         END;
  38.  
  39.         EXEC dbo.usp_WithdrawMoney @SenderId, @Amount;
  40.         EXEC dbo.usp_DepositMoney @ReceiverId, @Amount;
  41. COMMIT;
  42.  
  43. EXEC usp_TransferMoney 5, 1, 5000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement