1
tou with a problem in SQL server 2008
I have to enter an occurrence of the credit card data however the query is not taking place the way I needed.
The tables are
CREATE TABLE TEMP_DADOS_CARTAO
(
TOKENID_PARCELA VARCHAR(255)
, CARDNUMBER VARCHAR(255)
, HOLDER VARCHAR(255)
, EXPIRATIONDATE VARCHAR(255)
, ARQUIVO_LOG VARCHAR(100)
)
;
CREATE TABLE DADOS_CARTAO (
ID int IDENTITY(0,1) NOT NULL,
TOKENID_PARCELA varchar(255) NOT NULL,
TOKENID varchar(255) NOT NULL,
CARDNUMBER varchar(255) NULL,
HOLDER varchar(250) NULL,
EXPIRATIONDATE varchar(7) NULL,
DATA_PROCESSAMENTO datetime2(7) NULL,
ARQUIVO_LOG varchar(100) NULL,
CONSTRAINT DADOS_CARTAO_PK PRIMARY KEY (ID)
);
both the Insert into not exist http://sqlfiddle.com/#! 18/d0b83/1
INSERT INTO DADOS_CARTAO
(TOKENID_PARCELA
, TOKENID
, CARDNUMBER
, HOLDER
, EXPIRATIONDATE
, DATA_PROCESSAMENTO
, ARQUIVO_LOG
)
SELECT
TEMP.TOKENID_PARCELA
, SUBSTRING(TEMP.TOKENID_PARCELA, 1, 25)
, TEMP.CARDNUMBER
, TEMP.HOLDER
, TEMP.EXPIRATIONDATE
, GETDATE()
, TEMP.ARQUIVO_LOG
FROM
TEMP_DADOS_CARTAO TEMP
WHERE
NOT EXISTS
(
SELECT
1
FROM
DADOS_CARTAO EC
WHERE
EC.TOKENID =SUBSTRING(TEMP.TOKENID_PARCELA, 1, 25)
)
how much merge http://sqlfiddle.com/#! 18/5b90b/1
MERGE DADOS_CARTAO as DC
USING (SELECT
T.TOKENID_PARCELA , SUBSTRING(T.TOKENID_PARCELA, 1, 25) TOKENID, T.CARDNUMBER , T.HOLDER , T.EXPIRATIONDATE ,T.ARQUIVO_LOG
from TEMP_DADOS_CARTAO T ) as TEMP
on DC.TOKENID =TEMP.TOKENID
WHEN NOT MATCHED THEN
INSERT (TOKENID_PARCELA
, TOKENID
, CARDNUMBER
, HOLDER
, EXPIRATIONDATE
, DATA_PROCESSAMENTO
, ARQUIVO_LOG)
VALUES (TEMP.TOKENID_PARCELA
, TEMP.TOKENID
, TEMP.CARDNUMBER
, TEMP.HOLDER
, TEMP.EXPIRATIONDATE
, GETDATE()
, TEMP.ARQUIVO_LOG);;
are duplicating the records
I don’t know what I’m doing wrong
Hummm then makes the whole select before the Insert, I thought for each Insert it would check the "WHERE NOT EXISTS"
– HagaHood