text: CREATE TABLE Clients(
  	Id INT PRIMARY KEY IDENTITY,
  	FirstName NVARCHAR(50) NOT NULL,
  	LastName NVARCHAR(50) NOT NULL	
  )
  
  CREATE TABLE AccountTypes(
  	Id INT PRIMARY KEY IDENTITY,
  	NAME NVARCHAR(50) NOT NULL
  )
  
  CREATE TABLE Accounts(
  	Id INT PRIMARY KEY IDENTITY,
  	AccountTypeId INT FOREIGN KEY REFERENCES AccountTypes(Id),
  	Balance DECIMAL(15,2) NOT NULL DEFAULT(0),
  	ClientId INT FOREIGN KEY REFERENCES Clients(Id)
  )
  
  INSERT INTO Clients(FirstName, LastName) VALUES
  ('Gosho', 'Ivanov'),
  ('Pesho', 'Petrov'),
  ('Ivan', 'Iliev'),
  ('Merry', 'Ivanova')
  
  INSERT INTO AccountTypes(NAME) VALUES
  ('Checking'),
  ('Savings')
  
  INSERT INTO Accounts(ClientId, AccountTypeId, Balance) VALUES
  (1, 1, 175),
  (2, 1, 275.56),
  (3, 1, 138.01),
  (4, 1, 40.30),
  (4, 2, 375.50)
  
  GO
  
  CREATE FUNCTION f_CalculateBalanceById(@ClientId INT)
  RETURNS DECIMAL(15, 2)
  BEGIN
  	DECLARE @result AS DECIMAL(15, 2) = (
  	SELECT SUM(Balance) 
  	FROM Accounts WHERE ClientId = @ClientId
  	)
  	RETURN @result
  END
  
  GO
  
  SELECT dbo.f_CalculateBalanceById(3) AS Balance
  
  GO
  
  CREATE PROCEDURE p_AddAccount @ClientId INT, @AccountTypeId INT AS
  INSERT INTO Accounts(ClientId, AccountTypeId) VALUES
  (@ClientId, @AccountTypeId)
  
  p_AddAccount 2, 2
  
  GO
  
  CREATE PROCEDURE p_Deposit @AccountId INT, @Amount DECIMAL(15, 2) AS
  UPDATE Accounts 
  SET Balance += @Amount
  WHERE Id = @AccountId
  
  
  GO
  
  CREATE PROC p_Withdraw @AccountId INT, @Amount DECIMAL(15, 2) AS
  BEGIN
  	DECLARE @OldBalance DECIMAL(15, 2)
  	SELECT @OldBalance = Balance FROM Accounts WHERE Id = @AccountId
  	IF (@OldBalance - @Amount >= 0)
  	BEGIN
  		UPDATE Accounts
  		SET Balance -= @Amount
  		WHERE Id = @AccountId
  	END
  	ELSE
  	BEGIN
  		RAISERROR('Insufficient funds', 10, 1)
  	END
  END
  