Files
DP44/DataPRO/Modules/Database/DatabaseMigrationScripts/.svn/pristine/c8/c8a3e039e6f192d68719ce0eb3419b9366dd079d.svn-base
2026-04-17 14:55:32 -04:00

200 lines
11 KiB
Plaintext


ALTER PROCEDURE [dbo].[sp_DBImportSensorsDigitalOut]
@DigitalOutSensors xml
,@errorNumber int output
,@errorMessage nvarchar(250) output
AS
BEGIN
set @errorNumber = 0
set @errorMessage = ''
declare @tSensorsDigitalOut table (
[SerialNumber] [nvarchar](50),
[DelayMS] [float],
[DurationMS] [smallint],
[OutputMode] [smallint],
[LimitDuration] [bit],
[LastModified] [datetime],
[LastModifiedBy] [nvarchar](50),
[Version] [int],
[LocalOnly] [bit],
[DurationMSFloat] [float],
/*[UserTags] [varbinary](max),*/
[UserTagsText] [nvarchar](4000),
[Processed] [bit])
insert into @tSensorsDigitalOut
(SerialNumber
, DelayMS
, DurationMS
, OutputMode
, LimitDuration
, LastModified
, LastModifiedBy
, [Version]
, LocalOnly
, DurationMSFloat
/*, UserTags*/
, UserTagsText
,Processed)
select
t.x.value('SerialNumber[1]', 'nvarchar(50)') as SerialNumber
, t.x.value('DigitalOutputDelayMS[1]', 'float') as DelayMS
, t.x.value('DigitalOutputDurationMS[1]', 'smallint') as DurationMS
, dbo.foo_DigitalOutputOutputModeConverterToInt(t.x.value('DigitalOutputOutputMode[1]', 'nvarchar(50)')) as OutputMode
, t.x.value('DigitalOutputLimitDuration[1]', 'bit') as LimitDuration
, getdate() as LastModified /* t.x.value('@LastModified', 'datetime') */
, 'DBImport' as ModifiedBy /* t.x.value('@ModifiedBy', 'varchar(50)') */
, t.x.value('Version[1]', 'int') as Version
, t.x.value('LocalOnly[1]', 'bit') as LocalOnly
, t.x.value('DigitalOutputDurationMS[1]', 'float') as DurationMSFloat /* ???? */
/*, 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 @DigitalOutSensors.nodes('/Sensors/SensorData') t(x) OPTION (OPTIMIZE FOR ( @DigitalOutSensors = NULL ))
while (Select Count(*) From @tSensorsDigitalOut Where Processed = 0) > 0
Begin
begin try
begin transaction tSensorsDigitalOut
declare @SerialNumber nvarchar(50) = null
,@DelayMS float
,@DurationMS smallint
,@DurationMSFloat float
,@OutputMode smallint
,@LimitDuration bit
,@LastModified datetime
,@LastModifiedBy nvarchar(50)
,@Version int
,@UserTags varbinary(max)
,@UserTagsText nvarchar(4000)
,@new_id int
select top 1 @SerialNumber = SerialNumber
,@DelayMS = DelayMS
,@DurationMS = DurationMS
,@DurationMSFloat = DurationMSFloat
,@OutputMode = OutputMode
,@LimitDuration = LimitDuration
,@LastModified = LastModified
,@LastModifiedBy = LastModifiedBy
,@Version = [Version]
/*,@UserTags = UserTags*/
,@UserTagsText = UserTagsText
from @tSensorsDigitalOut 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_SensorsDigitalOutUpdateInsert @SerialNumber
,@DelayMS
,@DurationMS
,@DurationMSFloat
,@OutputMode
,@LimitDuration
,@LastModified
,@LastModifiedBy
,@Version
,@UserTags
,@new_id output
,@errorNumber output
,@errorMessage output
update @tSensorsDigitalOut set Processed = 1 where SerialNumber = @SerialNumber
commit transaction tSensorsDigitalOut
end try
begin catch
set @errorNumber = error_number()
set @errorMessage = error_message()
rollback transaction tSensorsDigitalOut
end catch;
end
END