﻿CREATE PROCEDURE [dbo].[sp_AnalogDiagnosticsGet]
	@Id					bigint				= null,
	@DiagnosticRunId    bigint              = null,
	@SensorId           int                 = null,
	@SensorSerialNumber nvarchar            = null
AS
BEGIN
	SET NOCOUNT ON;
	if( @Id IS NULL)
	BEGIN
		IF (@DiagnosticRunId is NULL)
		BEGIN
			IF(@SensorId IS NULL)
			BEGIN
				IF NULLIF(@SensorSerialNumber, '') IS NULL
				BEGIN
					SELECT [Id], [DiagnosticRunId], [Excitation], [ExcitationStatus], [Offset], [OffsetStatus], [ActualRange], [ActualRangeStatus], [Noise], [NoiseStatus], [Shunt], [ShuntStatus], [SensorId], [SensorSerialNumber], [DASId], [DASSerialNumber], [DASChannelIdx], [UserCode], [UserChannelName], [IsoCode], [IsoChannelName], [ScaleFactor], [CalibrationRecordId], [CalibrationRecordXML], [Timestamp] FROM [dbo].AnalogDiagnostics;
				END
				ELSE
				BEGIN
					SELECT [Id], [DiagnosticRunId], [Excitation], [ExcitationStatus], [Offset], [OffsetStatus], [ActualRange], [ActualRangeStatus], [Noise], [NoiseStatus], [Shunt], [ShuntStatus], [SensorId], [SensorSerialNumber], [DASId], [DASSerialNumber], [DASChannelIdx], [UserCode], [UserChannelName], [IsoCode], [IsoChannelName], [ScaleFactor], [CalibrationRecordId], [CalibrationRecordXML], [Timestamp] FROM [dbo].AnalogDiagnostics WHERE [SensorSerialNumber]=@SensorSerialNumber;
				END					
			END
			ELSE
			BEGIN
				SELECT [Id], [DiagnosticRunId], [Excitation], [ExcitationStatus], [Offset], [OffsetStatus], [ActualRange], [ActualRangeStatus], [Noise], [NoiseStatus], [Shunt], [ShuntStatus], [SensorId], [SensorSerialNumber], [DASId], [DASSerialNumber], [DASChannelIdx], [UserCode], [UserChannelName], [IsoCode], [IsoChannelName], [ScaleFactor], [CalibrationRecordId], [CalibrationRecordXML], [Timestamp] FROM [dbo].AnalogDiagnostics WHERE [SensorId]=@SensorId;					
			END
		END			
		ELSE
		BEGIN
			SELECT [Id], [DiagnosticRunId], [Excitation], [ExcitationStatus], [Offset], [OffsetStatus], [ActualRange], [ActualRangeStatus], [Noise], [NoiseStatus], [Shunt], [ShuntStatus], [SensorId], [SensorSerialNumber], [DASId], [DASSerialNumber], [DASChannelIdx], [UserCode], [UserChannelName], [IsoCode], [IsoChannelName], [ScaleFactor], [CalibrationRecordId], [CalibrationRecordXML], [Timestamp] FROM [dbo].AnalogDiagnostics WHERE [DiagnosticRunId]=@DiagnosticRunId;
		END
	END
	ELSE
	BEGIN
		SELECT [Id], [DiagnosticRunId], [Excitation], [ExcitationStatus], [Offset], [OffsetStatus], [ActualRange], [ActualRangeStatus], [Noise], [NoiseStatus], [Shunt], [ShuntStatus], [SensorId], [SensorSerialNumber], [DASId], [DASSerialNumber], [DASChannelIdx], [UserCode], [UserChannelName], [IsoCode], [IsoChannelName], [ScaleFactor], [CalibrationRecordId], [CalibrationRecordXML], [Timestamp] FROM [dbo].AnalogDiagnostics WHERE [Id] = @Id;
	END
END