2016-03-02 78 views
0

我对我的脚本有疑问,我的脚本部分很好地计算特定的客户端,但我需要的是为所有客户做,而不询问客户数据需要更新其金额,这将是计算所有客户的适当代码?脚本根据存款和取款计算账户余额

这里我的脚本:

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 

的表格:

databasebank

每个客户端的数据:

enter image description here

+0

P租赁编辑您的问题,以包括相关的表ddl语句,一些示例数据dml语句和所需的结果。 –

+0

也可以请翻译:Recalculo del saldo total en base dedepósitosy retiros – TheGameiswar

+0

@ TheGameiswar总存款和存款取款的计算方法。 –

回答

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

'SELECT cols FROM tbl ON conds' is not valid TSQL。 –