IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_SensorsDigitalOutUpdateInsert]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sp_SensorsDigitalOutUpdateInsert] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_SensorsDigitalOutUpdateInsert]') AND type in (N'P', N'PC')) BEGIN EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sp_SensorsDigitalOutUpdateInsert] AS' END GO ALTER PROCEDURE [dbo].[sp_SensorsDigitalOutUpdateInsert] @SerialNumber nvarchar(50) = null ,@DelayMS float ,@DurationMS smallint ,@DurationMSFloat float ,@OutputMode smallint ,@LimitDuration bit ,@LastModified datetime ,@LastModifiedBy nvarchar(50) ,@Version int ,@UserTags varbinary(max) ,@new_id int output ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorNumber = 0 set @errorMessage = space(0) if(@SerialNumber is null) begin set @errorNumber = 1560 set @errorMessage = 'An invalid parameter or option was specified for procedure'; end else begin if(exists(select SerialNumber from v_SensorSerialNumber where SerialNumber = @SerialNumber and SensorType != 2)) begin set @errorNumber = -1 set @errorMessage = 'The sensor serial mumber - ' + ltrim(rtrim(@SerialNumber)) + ' is not unique'; end else begin if(exists(select SerialNumber from v_SensorSerialNumber where SerialNumber = @SerialNumber and SensorType = 2)) begin exec dbo.sp_SensorsDigitalOutUpdate @SerialNumber ,@DelayMS ,@DurationMS ,@DurationMSFloat ,@OutputMode ,@LimitDuration ,@LastModified ,@LastModifiedBy ,@Version ,@UserTags ,@errorNumber output ,@errorMessage output end else begin exec dbo.sp_SensorsDigitalOutInsert @SerialNumber ,@DelayMS ,@DurationMS ,@DurationMSFloat ,@OutputMode ,@LimitDuration ,@LastModified ,@LastModifiedBy ,@Version ,@UserTags ,@new_id output ,@errorNumber output ,@errorMessage output end end end END GO