SQL Switch assigning value to a variable

1

I need to go through a whole table and depending on its content insert that data somewhere else that I imagine I could use a switch , but I have problems to reference it to the variable I want. I need to recognize the value of [Charge / Credit] and depending on whether it is "-" or "+" save it [Amount] in variable @abono or @cargo .

IF OBJECT_ID('tempdb..#stats_ddl') is not null DROP TABLE stats_ddl

GO
CREATE TABLE #stats_ddl (
[Cuenta] varchar(max),
[Fecha] varchar(50), --
[Hora] varchar(50),
[Suc] varchar(50),
[Descripcio] varchar(max),
[Cargo/Abono] varchar(50),
[Importe] varchar(50),
[Saldo] varchar(50),
[Referencia] varchar(50),
[Concepto / Referencia Interbancaria] varchar(max) --
);
Go 

declare @cargo varchar(max) = '',
@count int = 0,
@abono varchar(max) = ''

bulk insert erpsamfinanzas.dbo.#stats_ddl
from 'C:\SAM\SamFinanzas\Facturas_20180822144927_20180822144927.csv'
with (          firstrow = 2,                
                FIELDTERMINATOR = ',',
                ROWTERMINATOR = '\n')

--select * from   stats_ddl

declare @count int = 0,
  @countb int = 1
set @count = (select count( * ) from #stats_ddl)
while @countb <= @count
begin
    DECLARE @fechaMov VARCHAR(50) = (SELECT TOP(1) [Cargo/Abono] FROM     #stats_ddl);
    PRINT 'fecha' + @fechaMov + ' ' + CONVERT(VARCHAR, @count);

-- por quiero meter el switch y que me tome cargo/abono y dependiendo de lo que sea me almacene en cargo u abono la columa de importe
    DELETE TOP (1) FROM #stats_ddl
    SELECT @count = COUNT(*) FROM #stats_ddl;
end

    
asked by E.Rawrdríguez.Ophanim 22.08.2018 в 19:46
source

1 answer

2

I think you need it for your query is to add this, from a case I'm not sure you can set values to multiple variables the way you want to do it:

DECLARE @Importe VARCHAR(50), 
        @CargoAbono VARCHAR(50);
SELECT TOP(1) @CargoAbono = [Cargo/Abono], @Importe = [Importe] FROM #stats_ddl
IF @CargoAbono LIKE '%+%'
BEGIN
    SET @abono = @Importe
END
IF @CargoAbono LIKE '%-%'
BEGIN
    SET @cargo = @Importe
END
    
answered by 23.08.2018 / 00:23
source