ALTER PROCEDURE [dbo].[sp_SensorsDigitalInUpdateInsert]
		 @SerialNumber		nvarchar(50)
		,@SettingMode		int
		,@ScaleMultiplier	nvarchar(50)
		,@LastModified		datetime
		,@LastModifiedBy	nvarchar(50)
		,@eId				nvarchar(50)
		,@UserValue1		nvarchar(255)
		,@UserValue2		nvarchar(255)
		,@UserValue3		nvarchar(255)
		,@UserTags			varbinary(max)
		,@MeasurementUnit	nvarchar(50)
		,@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 != 1))
		begin
			set @errorNumber = -1
			set @errorMessage = 'The sensor serial mumber - ' + ltrim(rtrim(@SerialNumber)) + ' is not unique';
		end
		else
		begin
			if(exists(select * from v_SensorSerialNumber where SerialNumber = @SerialNumber and SensorType = 1))
			begin
				set @new_id = 0
				exec dbo.sp_SensorsDigitalInUpdate   @SerialNumber		
													,@SettingMode		
													,@ScaleMultiplier	
													,@LastModified		
													,@LastModifiedBy	
													,@eId				
													,@UserValue1		
													,@UserValue2		
													,@UserValue3		
													,@UserTags
													,@MeasurementUnit			
													,@errorNumber output
													,@errorMessage output
			end
			else
			begin
				exec dbo.sp_SensorsDigitalInInsert   @SerialNumber		
													,@SettingMode		
													,@ScaleMultiplier	
													,@LastModified		
													,@LastModifiedBy	
													,@eId				
													,@UserValue1		
													,@UserValue2		
													,@UserValue3		
													,@UserTags
													,@MeasurementUnit	
													,@new_id output		
													,@errorNumber output
													,@errorMessage output
			end
		end
	end
END
