Search code examples
sqlsybase

SQL How could i add MULTIPLE DATA from an if not exist?


I have a table with more than 1500 records and i want to insert into another table the data which no contains @Cot_NomArr = 'PAGO' and the records i want to insert is the variable @Cot_Credit the table looks like:

ABCOTIZA
-------------------------
Cot_Credit | Cot_NomArr
-------------------------
5459892698 | DEBT
3649949499 | DEBT
6265662645 | PAGO
6265662645 | PAGO
6565626569 | DEBT

So following my query:

create table #TESTINT(
    Cre_Numero  varchar(15) 
)

select * from ABCOTIZA

DROP TABLE #TESTINT

/* VARIABLES*/
declare @Cot_Credit varchar(15)
/* CONSTANTES*/
declare @Cot_NomArr varchar(150)
/* DECLARACIÓN DE CONSTANTE*/
select  @Cot_NomArr = 'PAGO'

select @Cot_Credit = isnull(Cot_Credit,''),
       @Cot_NomArr = isnull(Cot_NomArr,'')
      from ABCOTIZA noholdlock  

if not exists (select distinct(Cot_Credit) from ABCOTIZA where Cot_Credit = @Cot_Credit and Cot_NomArr = @Cot_NomArr) begin
    insert into #TESTINT(Cre_Numero)
        values (@Cot_Credit)
end

select * from #TESTINT

i want to my table #TESTINT at the end looks like:

#TESTINT
----------------
@Cot_Credit
----------------
5459892698
3649949499
6565626569

Because those are the ones with no 'PAGO' in Cot_NomArr

I am using ASE Sybase

But is not working, it's not insert any data... please help.


Solution

  • Is this what you want?

    insert into #TESTINT (Cre_Numero)
        select a.Cre_Numero
        from ABCOTIZA a
        where a.Cot_NomArr <> 'Pago';