Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Is there a better way to write this SQL? - I wish to use something other than NOT IN.

$
0
0

Is there a better way to write this SQL?

SET @IMP_COUNT =
  (SELECT COUNT(*)
   FROM LinkedServerName.DatabaseName.dbo.TableName 
   WHERE 'P-' + CASE
       WHEN CaseNoExt is null
       THEN CaseNumber
       ELSE CaseNumber + rtrim(ltrim(Convert(char(25),CaseNoExt)))
       END NOT IN (SELECT PNUM FROM SchemaName.TableName))


This query is going through a linked server (LinkedServerName.DatabaseName.dbo.TableName) and doing a compare with a database on the source.  It is going across the network and taking about 10 minutes.  It is also utilizing a lot of CPU time.

It is using a NOT IN selecting a database from the source database.

I was wondering if the NOT IN could be written better.


lcerni


Viewing all articles
Browse latest Browse all 23857

Trending Articles