Error in SELECT with several tables WITH SQL SERVER

0

Good I am doing a query query only with a select with several tables this is my query in the image that I will attach the following error:

DECLARE @Buscar AS VARCHAR(16)
DECLARE @NCuota AS INTEGER

SET @Buscar=('1010100368233')
SET @NCuota= ('8')

SELECT NumeroCredito NroCredito,Moneda,Numero NCuota, CAST(FechaVencimiento AS datetime)as Fec_Vcto, Tb.IDRubro,
                            Descripcion Rubro, Monto_Pactado, Monto_Pagado, Monto_Condonado, 
                            Monto_Descuento, Saldo, Estado_Cuota,Nombres 
                     FROM 
                        (   SELECT Cre.IdCliente,Cli.Nombres,CodigoMoneda Moneda, NumeroCredito,Numero, FechaVencimiento, RubCuo.IDRubro,
                                   Rub.Descripcion, RubCuo.Valor Monto_Pactado, RubCuo.ValorPagado Monto_Pagado, RubCuo.ValorCondonado Monto_Condonado, 
                                   RubCuo.ValorDescuento Monto_Descuento, RubCuo.Saldo, Estado_Cuota=CASE WHEN Cubierta =0 THEN 'Vigente' ELSE 'Cancelado' END
                            FROM BPPROD.CREDITO.DBO.Credito Cre 
                            JOIN  BPPROD. CLIENTE.DBO.vW_Clientes Cli  ON Cre.IDCliente=Cli.IDCliente
                            JOIN BPPROD. CREDITO.DBO.CuotaCredito Cuo ON Cre.IdCredito = Cuo.IdCredito
                            JOIN  BPPROD. CREDITO.DBO.RubroCuotaCredito RubCuo ON Cuo.IdCuotaCredito = RubCuo.IdCuotaCredito
                            JOIN BPPROD. CREDITO.DBO.Rubro Rub  ON RubCuo.IdRubro = Rub.IdRubro
                            WHERE NumeroCredito=''' + CAST(@Buscar AS VARCHAR(16)) + ''' AND Numero=' + CAST(@Ncuota AS VARCHAR(16)) + ' AND Valor<>0 and 
                                  RubCuo.IDRubro not in (41,42,44,45,1056,1057,1058,48,43) 
                        ) Tb
    
asked by PieroDev 16.02.2017 в 16:57
source

2 answers

0

The problem is with this part of the SQL:

WHERE NumeroCredito=''' + CAST(@Buscar AS VARCHAR(16)) + ''' AND Numero=' + CAST(@Ncuota AS VARCHAR(16)) + ' AND Valor<>0 and

The truth is that I do not understand well why you are using the variables @Buscar and @Ncuota within quotes that way. From what I can see, you can use them directly in this way:

WHERE NumeroCredito= @Buscar AND Numero= @Ncuota AND Valor<>0 and
    
answered by 16.02.2017 / 17:06
source
0

Assuming that the Number column is of type smallint, you should not assign a VARCHAR value to the variable @NCuota. I think the solution to your problem would be the following:

In the statement

SET @NCuota= 8

And in the query:

AND Numero= @NCuota
    
answered by 16.02.2017 в 17:05