1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| CREATE PROCEDURE [dbo].[InsertHistoricalValue2]
@date datetime,
@type nvarchar(60),
@equipement varchar(60),
@variable varchar(120),
@valeur varchar(70),
@etat int
AS
SET NOCOUNT ON;
declare @verbose bit
set @verbose=0
IF (@etat=0)
begin
DECLARE @query varchar(max)
DECLARE @boolean varchar(5)
-- valeur OK
IF (@type='bool')
SET @boolean='1'
ELSE
SET @boolean='0'
-- suppression postfix _H ou _AH de la variable
if upper(right(@variable,2))='_H'
set @variable=left(@variable,len(@variable)-2)
if upper(right(@variable,3))='_AH'
set @variable=left(@variable,len(@variable)-3)
DECLARE @centraleid int
select @centraleid=id from centrale where name=@equipement
if @centraleid is null
begin
-- creer centrale si inexistante
insert into dbo.centrale (name) values(@equipement)
set @centraleid=@@IDENTITY
end
DECLARE @variableID int;
SELECT @variableid=id from dbo.variable where centraleid=@centraleid and name=@variable
-- ajout variable dans table variable si première occurence
IF @variableID is null
BEGIN
insert into dbo.variable (centraleid,name,Boolean)
values(@centraleid,@variable,@boolean)
set @variableID=@@IDENTITY
END
-- conversion valeur en numeric
DECLARE @value Real
set @value=0
if isnumeric(@valeur)=1
set @value=cast(@valeur as real)
-- insertion valeur dans table histo si valeur non existante
DECLARE @tempid int;
select @tempid=id from histo where date=@date and id=@variableid
if (@tempid is null)
begin
insert into dbo.histo (id,date,value) values(@variableid,@date,@value)
declare @rtdate datetime
select @rtdate=date from dbo.rtvalue where id=@variableid
if (@rtdate<@date)
update dbo.rtvalue set date=@date,value=@value where id=@variableid
end
end
GO |
Partager