Files
DP44/DataPRO/Modules/Database/DatabaseMigrationScripts/.svn/pristine/a0/a034d6ae1380a9bdade13220f2e002a3365b53a1.svn-base

261 lines
16 KiB
Plaintext
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
<EFBFBD><EFBFBD>
ALTER PROCEDURE [dbo].[sp_DBImportSensorsSquib]
@SquibSensors xml
,@errorNumber int output
,@errorMessage nvarchar(250) output
AS
BEGIN
set @errorNumber = 0
set @errorMessage = ''
declare @tSensorsSquib table
([SerialNumber] [nvarchar](50),
[SquibBypassCurrentFilter] [bit],
[SquibBypassVoltageFilter] [bit],
[SquibDelayMS] [float],
[SquibDurationMS] [float],
[SquibFireMode] [smallint],
[ISOCode] [nvarchar](50),
[SquibMeasurementType] [smallint],
[SquibOutputCurrent] [float],
[SquibToleranceLow] [float],
[SquibToleranceHigh] [float],
[LimitDuration] [bit],
[ArticleId] [nvarchar](50),
[LocalOnly] [bit],
[Version] [int],
[LastModified] [datetime],
[LastModifiedBy] [nvarchar](50),
[UserValue1] [nvarchar](255),
[UserValue2] [nvarchar](255),
[UserValue3] [nvarchar](255),
/*[UserTags] [varbinary](max),*/
[UserTagsText] [nvarchar](4000),
[Processed] [bit])
insert into @tSensorsSquib
(SerialNumber
, SquibBypassCurrentFilter
, SquibBypassVoltageFilter
, SquibDelayMS
, SquibDurationMS
, SquibFireMode
, ISOCode
, SquibMeasurementType
, SquibOutputCurrent
, SquibToleranceLow
, SquibToleranceHigh
, LimitDuration
, ArticleId
, LocalOnly
, [Version]
, LastModified
, LastModifiedBy
, UserValue1
, UserValue2
, UserValue3
/*, UserTags*/
, UserTagsText
,Processed)
select
t.x.value('SerialNumber[1]', 'nvarchar(50)') as SerialNumber
, t.x.value('SquibBypassCurrentFilter[1]', 'bit') as SquibBypassCurrentFilter
, t.x.value('SquibBypassVoltageFilter[1]', 'bit') as SquibBypassVoltageFilter
, t.x.value('SquibDelayMS[1]', 'float') as SquibDelayMS
, t.x.value('SquibDurationMS[1]', 'float') as SquibDurationMS
, dbo.foo_SquibFireModeConverterToInt(t.x.value('SquibFireMode[1]', 'nvarchar(50)')) as SquibFireMode
, t.x.value('IsoCode[1]', 'varchar(50)') as ISOCode
, dbo.foo_SquibMeasurementTypeConverterToInt(t.x.value('SquibMeasurementType[1]', 'varchar(50)')) as SquibMeasurementType
, t.x.value('SquibSquibOutputCurrent[1]', 'float') as SquibOutputCurrent
, t.x.value('SquibSquibToleranceLow[1]', 'float') as SquibToleranceLow
, t.x.value('SquibSquibToleranceHigh[1]', 'float') as SquibToleranceHigh
, t.x.value('SquibLimitDuration[1]', 'bit') as LimitDuration
, t.x.value('SquibArticleId[1]', 'varchar(max)') as SquibArticleId
, t.x.value('LocalOnly[1]', 'bit') as LocalOnly
, t.x.value('Version[1]', 'int') as Version
, getdate() as LastModified /* t.x.value('@LastModified', 'datetime') */
, 'DBImport' as ModifiedBy /* t.x.value('@ModifiedBy', 'varchar(50)') */
, t.x.value('UserValue1[1]', 'nvarchar(50)') as UserValue1
, t.x.value('UserValue2[1]', 'nvarchar(50)') as UserValue2
, t.x.value('UserValue3[1]', 'nvarchar(50)') as UserValue3
/*, CASE WHEN t.x.value('UserTags[1]', 'varbinary(max)') IS NULL THEN 0
ELSE t.x.value('UserTags[1]', 'varbinary(max)') end as UserTags */
, CASE WHEN t.x.value('UserTags[1]', 'nvarchar(4000)') IS NULL THEN ''
ELSE t.x.value('UserTags[1]', 'nvarchar(4000)') end as UserTagsText
, 0
from @SquibSensors.nodes('/Sensors/SensorData') t(x) OPTION (OPTIMIZE FOR ( @SquibSensors = NULL ))
while (Select Count(*) From @tSensorsSquib Where Processed = 0) > 0
Begin
begin try
begin transaction tSensorsSquib
declare @SerialNumber nvarchar(50)
,@SquibBypassCurrentFilter bit
,@SquibBypassVoltageFilter bit
,@SquibDelayMS float
,@SquibDurationMS float
,@SquibFireMode smallint
,@ISOCode nvarchar(50)
,@SquibMeasurementType smallint
,@SquibOutputCurrent float
,@SquibToleranceLow float
,@SquibToleranceHigh float
,@LimitDuration bit
,@ArticleId nvarchar(50)
,@LocalOnly bit
,@Version int
,@LastModified datetime
,@LastModifiedBy nvarchar(50)
,@UserValue1 nvarchar(255)
,@UserValue2 nvarchar(255)
,@UserValue3 nvarchar(255)
,@UserTags varbinary(max)
,@UserTagsText nvarchar(4000)
,@new_id int
select top 1
@SerialNumber = SerialNumber
,@SquibBypassCurrentFilter = SquibBypassCurrentFilter
,@SquibBypassVoltageFilter = SquibBypassVoltageFilter
,@SquibDelayMS = SquibDelayMS
,@SquibDurationMS = SquibDurationMS
,@SquibFireMode = SquibFireMode
,@ISOCode = ISOCode
,@SquibMeasurementType = SquibMeasurementType
,@SquibOutputCurrent = SquibOutputCurrent
,@SquibToleranceLow = SquibToleranceLow
,@SquibToleranceHigh = SquibToleranceHigh
,@LimitDuration = LimitDuration
,@ArticleId = ArticleId
,@LocalOnly = LocalOnly
,@Version = Version
,@LastModified = LastModified
,@LastModifiedBy = LastModifiedBy
,@UserValue1 = UserValue1
,@UserValue2 = UserValue2
,@UserValue3 = UserValue3
/*,@UserTags = UserTags*/
,@UserTagsText = UserTagsText
from @tSensorsSquib where Processed = 0
declare @tagTable TABLE (Element nvarchar(4000), Processed bit)
declare @tagIdTable TABLE (Id int, Processed bit)
insert into @tagTable (Element, Processed) select * from dbo.foo_SplitDelimitedString(@UserTagsText, ',')
/*call foo_IdGetTag to get the tag id */
while (Select Count(*) From @tagTable Where Processed = 0) > 0
Begin
begin try
declare @tagName nvarchar(4000)
declare @tagIdTemp int
select top 1 @tagName = Element from @tagTable where Processed = 0
set @tagIdTemp = dbo.foo_IdGetTag(@tagName)
insert into @tagIdTable (Id, Processed) VALUES (@tagIdTemp, 0)
end try
begin catch
set @errorNumber = error_number()
set @errorMessage = error_message()
rollback transaction tSensorsAnalog
end catch;
update @tagTable set Processed = 1 where Element = @tagName
End
/*store in a byte array*/
while (Select Count(*) From @tagIdTable Where Processed = 0) > 0
Begin
begin try
declare @tagId int
declare @tagIdByte varbinary(4)
select top 1 @tagId = Id from @tagIdTable where Processed = 0
--set @tagIdByte = dbo.foo_ConvertIntToBytes(@tagId)
set @tagIdByte = CONVERT(VARBINARY(MAX), REVERSE(CONVERT(VARBINARY(4), @tagId)))
set @UserTags = CONVERT(varbinary(max), CONCAT(@UserTags, @tagIdByte))
end try
begin catch
set @errorNumber = error_number()
set @errorMessage = error_message()
rollback transaction tSensorsAnalog
end catch;
update @tagIdTable set Processed = 1 where Id = @tagId
End
exec dbo.sp_SensorsSquibUpdateInsert @SerialNumber
,@SquibBypassCurrentFilter
,@SquibBypassVoltageFilter
,@SquibDelayMS
,@SquibDurationMS
,@SquibFireMode
,@ISOCode
,@SquibMeasurementType
,@SquibOutputCurrent
,@SquibToleranceLow
,@SquibToleranceHigh
,@LimitDuration
,@ArticleId
,@LocalOnly
,@Version
,@LastModified
,@LastModifiedBy
,@UserValue1
,@UserValue2
,@UserValue3
,@UserTags
,@new_id output
,@errorNumber output
,@errorMessage output
update @tSensorsSquib set Processed = 1 where SerialNumber = @SerialNumber
commit transaction tSensorsSquib
end try
begin catch
set @errorNumber = error_number()
set @errorMessage = error_message()
rollback transaction tSensorsSquib
end catch;
end
END