IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_TestObjectSensorsInsert]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sp_TestObjectSensorsInsert] 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_TestObjectSensorsInsert]') AND type in (N'P', N'PC')) BEGIN EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sp_TestObjectSensorsInsert] AS' END GO ALTER PROCEDURE [dbo].[sp_TestObjectSensorsInsert] @TestObjectId int ,@TestObjectName nvarchar(255) = null ,@UIChannelID nvarchar(255) ,@SensorSerialNumber nvarchar(50) ,@LocalOnly bit ,@HardwareId nvarchar(255) ,@ChannelIdx int ,@new_id int output ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorNumber = 0; set @errorMessage = space(0); if(@TestObjectName is null) begin set @errorMessage = 'An invalid parameter or option was specified for procedure' set @errorNumber = 15600 end else begin begin try declare @SensorId int declare @DASId int declare @ISOChannelId int declare @ISOChannel bit declare @ChannelId int set @TestObjectId = dbo.foo_IdGetTestObject(@TestObjectName) set @SensorId = dbo.foo_IdGetSensor(@SensorSerialNumber) set @DASId = dbo.foo_IdGetDAS(@HardwareId) set @ISOChannelId = right(replace(@UIChannelID, @TestObjectName + '_', ''), LEN(replace(@UIChannelID, @TestObjectName + '_', '')) - charindex('_', replace(@UIChannelID, @TestObjectName + '_', ''))) set @ISOChannel = left(replace(@UIChannelID, @TestObjectName + '_', ''), charindex('_', replace(@UIChannelID, @TestObjectName + '_', ''))-1) set @ChannelId = right(@HardwareId, LEN(@HardwareId) - charindex('x', @HardwareId)) insert into [dbo].[TestObjectSensors] ([SensorId] ,[DASId] ,[TestObjectId] ,[ISOChannel] ,[ISOChannelID] ,[ChannelId] ,[LocalOnly] ,[ChannelIdx]) values (@SensorId ,@DASId ,@TestObjectId ,@ISOChannel ,@ISOChannelId ,@ChannelId ,@LocalOnly ,@ChannelIdx) set @new_id = scope_identity(); end try begin catch set @errorMessage = error_message() set @errorNumber = error_number() end catch; end END GO