簡體   English   中英

用於根據存款和取款計算賬戶余額的腳本

[英]script to calculate the account balance based on deposits and withdrawals

我的腳本有一個問題,我的腳本在某種程度上可以很好地計算出特定的客戶,但是我需要為所有客戶做的事而無需詢問需要更新其金額的客戶數據,因為這將是計算所有客戶的適當代碼顧客?

這是我的腳本:

    DECLARE @RFC VARCHAR(100)
DECLARE @Nombre VARCHAR(100)
DECLARE @Apellidos VARCHAR(100)
DECLARE @NoCuenta VARCHAR(50)

SET @RFC = ''
SET @Nombre = ''
SET @Apellidos = ''

--Suma de los depósitos
SELECT SUM(Monto) FROM [dbo].[Depositos] a
INNER JOIN [dbo].[Clientes] b
ON a.CuentaId = b.ClienteId
WHERE  b.Nombre = @Nombre and b.RFC = @RFC and b.Apellidos = @Apellidos and 

--sum of retires

SELECT SUM(Monto) FROM [dbo].[Retiros] a
INNER JOIN [dbo].[Clientes] b
ON a.CuentaId = b.ClienteId
WHERE b.Nombre = @Nombre and b.RFC = @RFC and b.Apellidos = @Apellidos

--Calculation of the total balance on the basis of deposits and withdrawals
DECLARE @Deposito DECIMAL
DECLARE @Retiro   DECIMAL
DECLARE @Total    DECIMAL
DECLARE @NoCuenta VARCHAR(50)

SET @Deposito = (SELECT SUM(Monto) FROM [dbo].[Depositos] a
INNER JOIN [dbo].[Clientes] b
ON a.CuentaId = b.ClienteId
WHERE b.Nombre = 'Marco' and b.RFC = 'sadfasfasfadsf')

SET @Retiro = (
SELECT SUM(Monto) FROM [dbo].[Retiros] a
INNER JOIN [dbo].[Clientes] b
ON a.CuentaId = b.ClienteId
WHERE b.Nombre = 'Marco' and b.RFC = 'sadfasfasfadsf')

SET @Total = (@Deposito - @Retiro)
SELECT @Total

SET @NoCuenta = '123456'

UPDATE A SET 
Saldo = @Total FROM [dbo].[CuentasBancarias] A 
WHERE NoCuenta = @NoCuenta

SELECT * FROM [dbo].[CuentasBancarias] WHERE NoCuenta = @NoCuenta

表格:

數據庫銀行

每個客戶的數據:

在此處輸入圖片說明

UPDATE CuentasBancarias SET
    Saldo = 
        ISNULL((SELECT SUM(Monto) FROM [dbo].[Depositos] a INNER JOIN [dbo].[CuentasBancarias] ON a.CuentaId = CuentasBancarias.ClienteId), 0) 
        - 
        ISNULL((SELECT SUM(Monto) FROM [dbo].[Retiros] a INNER JOIN [dbo].[CuentasBancarias] ON a.CuentaId = CuentasBancarias.ClienteId), 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM