tags:

views:

36

answers:

2

Hello, I'm building a stored procedure. This stored procedure needs to insert a record if a record with a specific value does not exist. If the value does exist, I need to update the record. The problem I'm having is determining if a record with the given value exists or not. I am using the following code:

DECLARE @record1ID as char(36)
SET @record1ID = (SELECT TOP 1 ID FROM Person WHERE [Role]='Manager')

DECLARE @record2ID as char(36)
SET @record2ID = (SELECT TOP 1 d.ID FROM Department d WHERE d.[ManagerID]=@record1ID)

-- If @record2ID is set update record, otherwise add record 
-- how do I setup this if/else statement?

Thank you!

+1  A: 

If this were a SQL Server as it looks like, you could do a count like this:

declare @rec_counter as int

set @rec_counter = 0

select @rec_counter = count(*) FROM Department d WHERE d.[ManagerID]=@record1

if (@rec_counter > 0)

begin

-- do whatever here

end

Luis Lobo Borobia
A: 
IF (EXISTS YOUR_SELECT)
BEGIN ...

or

IF (@record2ID IS NULL) 
BEGIN ...

or use select count(*) instead of selecting a value

DVK