1

Help!

I am trying to find the customer that are in VBSTEST that are not in VMFG9. the tables have identical schema. Whenever i run the query i never get a result returning.

`SELECT [ROWID] ,[ID] ,[NAME] ,[ADDR_1] ,[ADDR_2] ,[ADDR_3] ,[CITY] ,[STATE] ,[ZIPCODE] ,[COUNTRY] FROM [VMFG9].[dbo].[CUSTOMER] where NOT EXISTS( select v6.[ID] from [VBSTEST].[dbo].[CUSTOMER] as v6 left outer join [VMFG9].[dbo.CUSTOMER] as v9 on v9.id = v6.[id] where v9.id is null);` 
1
  • Is there any wrong in your question ? If you want to find customers that are in VBSTEST and not in VMFG9, your query cannot Select * from VMFG9 Commented Dec 7, 2017 at 16:43

3 Answers 3

1

The subquery that you want doesn't use a join:

select . . . from [VMFG9].[dbo].[CUSTOMER] c where not exists (select 1 from [VBSTEST].[dbo].[CUSTOMER] v6 where c.id = v6.id ); 

You can use left join/where in the outer query, if you prefer. But a single table reference is sufficient for the not exists.

Sign up to request clarification or add additional context in comments.

Comments

1

The easy way to do this is:

select id from [VBSTEST].[dbo].[CUSTOMER] except select id from [VMFG9].[dbo].[CUSTOMER]; 

You should also find that this kind of query is faster too.

Apart from being much longer, I think that there is an error in your query. Since null never equals anything else, it doesn't even equal null, then your condition:

where v9.id is null 

means that the condition

v9.id = v6.[id] 

can never be true.

Comments

0

With your NOT EXISTS you are currently saying, select all rows only when the subquery returns no results (else return nothing).

The subquery itself should be adequate:

select v6.* from [VBSTEST].[dbo].[CUSTOMER] as v6 left outer join [VMFG9].[dbo.CUSTOMER] as v9 on v9.id = v6.[id] where v9.id is null 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.