0

I am trying to write a stored procedure for SQL Server 2008. It is for a migration tool that migrates data from one database to another.

I want to get all records from the old database that aren't yet present in the new database. For that end I use the stored procedure below. But I get the error

The multi-part indentifier could not be bound

while executing the stored procedure. What can I do to make this work?

Here is the SP:

SELECT *
FROM Measurement_device
WHERE Measurement_device.Department_Code = '99'
  AND mir.dbo.Gages.Gage_code != Device_Number
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bernhard
  • 1,455
  • 5
  • 19
  • 24

1 Answers1

0

It's because you have the mir.dbo.Gages.Gage_code condition when that table is not referenced in the FROM clause. Try something like this:

SELECT m.*
FROM Measurement_device m
    LEFT JOIN mir.dbo.Gages g ON g.Gage_code = m.Device_Number
WHERE m.Department_Code = '99'
    AND g.Gage_code IS NULL -- where the gage_code/device_number does not already exist in mir database
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200