IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DBImportGroupChannelSetting]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[sp_DBImportGroupChannelSetting] 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_DBImportGroupChannelSetting]') AND type in (N'P', N'PC')) BEGIN EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[sp_DBImportGroupChannelSetting] AS' END GO ALTER PROCEDURE [dbo].[sp_DBImportGroupChannelSetting] @TestObjectId int ,@TestObjectName varchar(50) ,@ChannelSettings xml ,@errorNumber int output ,@errorMessage nvarchar(250) output AS BEGIN set @errorNumber = 0 set @errorMessage = space(0) begin try begin transaction tGroupChannelSetting declare @tGroupChannelSettings table ( TestObjectId int --,ISOChannel bit --,ISOChannelId int ,ChannelId nvarchar(255) ,Setting nvarchar(255) ,SensorId int) insert into @tGroupChannelSettings select @TestObjectId --, left(replace(z.ChannelID, @TestObjectName+ '_', ''), charindex('_', replace(z.ChannelID, @TestObjectName + '_', ''))-1) --, right(replace(z.ChannelID, @TestObjectName + '_', ''), LEN(replace(ChannelID, @TestObjectName + '_', '')) - charindex('_', replace(ChannelID, @TestObjectName + '_', ''))) , z.ChannelID , z.Setting ,dbo.foo_IdGetSensor(SensorId) from (select t.x.value('(@ChId)', 'nvarchar(50)') as ChannelID , t.x.value('(@SId)', 'nvarchar(50)') as SensorId , t.x.value('(@Setting)', 'nvarchar(255)') as Setting from @ChannelSettings.nodes('/ChannelSettings/ChannelSetting') t(x)) z insert into TestObjectChannelSettings (TestObjectId ,ChannelId --,ISOChannel --,ISOChannelId ,Setting ,SensorId) select TestObjectId ,ChannelId --,ISOChannel --,ISOChannelId ,Setting ,SensorId from @tGroupChannelSettings where ltrim(rtrim(isnull(ChannelId, space(0)))) != space(0) /* or any othe restrictions/validations*/ commit transaction tGroupChannelSetting end try begin catch set @errorNumber = error_number() set @errorMessage = error_message() rollback transaction tGroupChannelSetting end catch; END GO