Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GO
- CREATE PROCEDURE SPMoveMoney (
- @user_id INT,
- @from_acc INT,
- @to_acc INT,
- @amount MONEY,
- @STATUS INT OUTPUT
- ) AS
- BEGIN
- BEGIN TRANSACTION
- DECLARE @owner_of_acc_from AS INT
- DECLARE @to_exist AS INT
- DECLARE @has_money AS MONEY
- SET @owner_of_acc_from = (
- SELECT COUNT(*) AS 'rows'
- FROM Holder AS H
- INNER JOIN HolderAccount AS HA
- ON H.user_id = HA.user_id
- INNER JOIN Account AS A
- ON HA.acc_nr = A.acc_nr
- WHERE H.user_id = @user_id AND A.acc_nr = @from_acc
- )
- -- no need to make it larger... or w/e
- SET @has_money = (SELECT amount FROM Account WHERE acc_nr = @from_acc)
- SET @to_exist = (SELECT COUNT(*) FROM Account WHERE acc_nr = @to_acc)
- -- User has one account with "from" accountnr
- IF @owner_of_acc_from > 0 BEGIN
- IF @to_exist > 0 BEGIN
- IF @has_money >= @amount BEGIN
- UPDATE Account
- SET amount = amount + @amount
- WHERE acc_nr = @to_acc
- INSERT INTO Log (user_id, from_acc, to_acc, amount, DATE)
- VALUES (@user_id, @from_acc, @to_acc, @amount, GETDATE());
- -- 0: Everything went through
- SET @STATUS = 0
- COMMIT TRANSACTION
- END ELSE BEGIN
- -- 2: From account didn't have enough money
- SET @STATUS = 2
- ROLLBACK TRANSACTION
- END
- END
- END ELSE BEGIN
- -- 1: User id didn't own account or the account didn't exist
- SET @STATUS = 1
- ROLLBACK TRANSACTION
- END
- END
- GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement