Search code examples
sql-serverstored-proceduressql-server-2000

How to check if a record exists in SQL server 2000 using 2 fields


I have a quick question. I've written a stored procedure to interrogate a table, check if there are any records already based on 2 key fields and if not to add a record.

So currently my code looks like this.

select @counter=count(*) from f03 where someid NOT IN (select someid from ReportedEventGPQ)

I would like to know how to convert this counter to check not just on someid in the ReportedEventGPQ table but also on another field called TimepointID

This is so that when the stored procedure runs, it checks if the userid (someid) and the timepointid not in ReportedEventGPQ already. As the user can enter a row in f03 over 10 timepoints which each get a row in the f03 table with a timepointid.

Any help with this would be much appreciated.


Solution

  • Try use WHERE NOT EXISTS:

       select @counter=count(*) from f03 
        where not exists(select * from ReportedEventGPQ where someid =f03.someid and TimepointID=f03.TimepointID)