I'm using SQL Express 2005.
I have two tables, TABLEA and TABLEB. TABLEA has the following:
col1
-----
NAMEAAA
NAMEAAA
NAMEBBB
NAMEBBB
NAMECCC
NAMECCC
NAMECCC
NAMEDDD
I want to insert rows into TABLEB so that:
fld1 fld2 fld3 fld4 fld5
---- ----- ---- ---- ----
NAMEAAA 0 NULL 0.0000 NULL
NAMEBBB 0 NULL 0.0000 NULL
NAMECCC 0 NULL 0.0000 NULL
NAMEDDD 0 NULL 0.0000 NULL
=============== =============== =============== ===========
If I try the following, 8 rows get created in TABLEB:
INSERT INTO TABLEB
([fld1]
,[fld2]
,[fld3]
,[fld4]
,[fld5])
SELECT col1,0,NULL,0,N ULL
FROM TABLEA
WHERE not exists (select * from TABLEB
where TABLEB.fld1 = TABLEA.col1);
If instead I try the following, 8 rows still get created:
INSERT INTO TABLEB
([fld1]
,[fld2]
,[fld3]
,[fld4]
,[fld5])
SELECT col1,0,NULL,0,N ULL
FROM TABLEA
WHERE col1 NOT IN (select fld1 FROM TABLEB);
=============== =============== =============== ======
If I put the word DISTINCT before "col1" in my SELECT statement, I get the error, "The text, ntext, or image data type cannot be selected as DISTINCT". If I remove the "col1,0,NULL,0, NULL" from the SELECT statement, I get the error, "The select list for the INSERT statement contains fewer items than the insert list".
I would like to mention that TABLEB has an identity column as its key.
Also, if I run the above twice, the second time it is run, no additional rows are put in TABLEB. If I then delete the 3 rows starting with NAMECCC from TABLEB, and rerun the above, it inserts the 3 rows NAMECCC, NAMECCC, NAMECCC !
If I remove the WHERE clause, and run the above twice, then it puts 16 rows in TABLEB.
It's almost as if the SQL language is incapable of perceiving duplicates that have been added during the session that is running.
I have two tables, TABLEA and TABLEB. TABLEA has the following:
col1
-----
NAMEAAA
NAMEAAA
NAMEBBB
NAMEBBB
NAMECCC
NAMECCC
NAMECCC
NAMEDDD
I want to insert rows into TABLEB so that:
fld1 fld2 fld3 fld4 fld5
---- ----- ---- ---- ----
NAMEAAA 0 NULL 0.0000 NULL
NAMEBBB 0 NULL 0.0000 NULL
NAMECCC 0 NULL 0.0000 NULL
NAMEDDD 0 NULL 0.0000 NULL
=============== =============== =============== ===========
If I try the following, 8 rows get created in TABLEB:
INSERT INTO TABLEB
([fld1]
,[fld2]
,[fld3]
,[fld4]
,[fld5])
SELECT col1,0,NULL,0,N ULL
FROM TABLEA
WHERE not exists (select * from TABLEB
where TABLEB.fld1 = TABLEA.col1);
If instead I try the following, 8 rows still get created:
INSERT INTO TABLEB
([fld1]
,[fld2]
,[fld3]
,[fld4]
,[fld5])
SELECT col1,0,NULL,0,N ULL
FROM TABLEA
WHERE col1 NOT IN (select fld1 FROM TABLEB);
=============== =============== =============== ======
If I put the word DISTINCT before "col1" in my SELECT statement, I get the error, "The text, ntext, or image data type cannot be selected as DISTINCT". If I remove the "col1,0,NULL,0, NULL" from the SELECT statement, I get the error, "The select list for the INSERT statement contains fewer items than the insert list".
I would like to mention that TABLEB has an identity column as its key.
Also, if I run the above twice, the second time it is run, no additional rows are put in TABLEB. If I then delete the 3 rows starting with NAMECCC from TABLEB, and rerun the above, it inserts the 3 rows NAMECCC, NAMECCC, NAMECCC !
If I remove the WHERE clause, and run the above twice, then it puts 16 rows in TABLEB.
It's almost as if the SQL language is incapable of perceiving duplicates that have been added during the session that is running.
Comment