SELECT in a table using not exists SQL (SERVER 2012)

I am trying to make a select tb1 using Where NOT EXISTS (select* tb2) I am trying to select the row in tb1 that does not exist in tb2 to perform an INSERT later.

But when testing in select it is not returning the lines that do not exist in tb2.

SELECT *
FROM TB1
WHERE NOT EXISTS(SELECT * FROM tb2)
Author: José Diz, 2017-06-14

3 answers

tb1 you need to have some coluna that refers to tb2, even if this is not a constraint (A.K. A foreign key).

So let's say tb1 has the column tb2_id that references tb2_id in tb2, you can do so.:

SELECT *
FROM TB1
WHERE NOT EXISTS(SELECT tb2_id FROM tb2 WHERE TB1.tb2_id = tb2.tb2_id)

You can also try.:

SELECT TB1.*
FROM TB1
LEFT JOIN tb2 ON TB1.tb2_id = tb2.tb2_id
WHERE tb2.tb2_id IS NULL

And finally one more alternative.:

SELECT *
FROM TB1
WHERE tb2.tb2_id NOT IN (SELECT tb2_id FROM tb2)

In terms of performance, the LEFT JOIN with IS NULL should be the slowest, the difference between the NOT IN and the NOT EXISTS should be marginal. I would get the NOT EXISTS because it is semantically closer than it aims to do.

And if you do not have a indice for tb2_id in tb1, I advise you to create one, because NOT EXISTS will be greatly affected by the absence of one.

You can read more at.: Should I use NOT IN, OUTER APPLY, LEFT OUTER JOIN, EXCEPT, or NOT EXISTS?

EDIT

Since you are using a linked server, try using the OPENQUERY command to query the linked server data.

DECLARE @QUERY_QDE AS NVARCHAR(MAX)
SET @QUERY_QDE = N'SELECT C.[CHAMADO], , C.[COLUNA_1], C.[COLUNA_2], ..., C.[COLUNA_N] FROM [dbo].[TB_QDE_CHAMADO] C';

SELECT CHAMADO_QDE.* 
FROM OPENQUERY([XXX.XXX.XXX.XXX], @QUERY_QDE) AS CHAMADO_QDE
WHERE NOT EXISTS (
    SELECT CHAMADO_AXA.[CHAMADO] 
    FROM [_dbAXA].[dbo].[TB_QDE_CHAMADO] AS CHAMADO_AXA
    WHERE CHAMADO_QDE.[CHAMADO] = CHAMADO_AXA.[CHAMADO]
)

In this case, you will need to specify all the columns in your query, because OPENQUERY does not understand a wildcard.

Finally, [XXX.XXX.XXX.XXX] is the address of your LINKED SERVER

 4
Author: Tobias Mesquita, 2017-06-16 14:10:40

Yeah, you're trying to select rows in tb1 that don't exist in tb2, but that's not what you wrote. What you wrote was to select rows from tb1 when there is no row in tb2 (NOT EXISTS (SELECT * FROM tb2)).

What you need is to identify in your subquery how do you identify that the line in tb2 is "equal" to the line in tb1. For example:

select *
  from tb1
 where not exists (
        select *
          from tb2
         where tb2.tb2_id = tb1.tb1_id
       )

Or something similar. Now the subquery will bring the rows of tb2 that are " equal "(in the case, via comparison of columns tb1_id and tb2_id) to the row of tb1 that the main select is currently considering. And then she does what you want.

 2
Author: Wtrmute, 2017-06-14 17:04:00

Is missing to relate one row to another row in the other table.

Should be something like and assuming the id is the key of each table:

SELECT 
*
FROM 
  TB1 t1
WHERE 
  NOT EXISTS(SELECT * FROM tb2 t2 where t1.id = t2.id)

In this way select will return rows from t1 that do not exist in t2.

 1
Author: Leandro, 2017-06-14 17:00:53