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
| CREATE TABLE TbPompe
(
IDPompe INT IDENTITY(1,1) CONSTRAINT PK_TbPompe PRIMARY KEY (IDPompe),
NomPompe VARCHAR(64) NOT NULL
);
INSERT INTO TbPompe VALUES ('Pompe 1');
INSERT INTO TbPompe VALUES ('Pompe 2');
CREATE TABLE TbPompeLog
(
IDPompe INT NOT NULL CONSTRAINT FK_TbPompeLog_IDPompe REFERENCES TbPompe (IDPompe),
DateDemarrage DATETIME,
DateArret DATETIME
);
----------------------------------------------------
CREATE PROCEDURE psTbLogPompe_Ajoute_DateDemarrage
@_IDPompe INT
AS
BEGIN
IF EXISTS
(
SELECT 1
FROM dbo.TbPompeLog
WHERE IDPompe = @_IDPompe
AND DateDemarrage IS NOT NULL
)
BEGIN
RAISERROR('La pompe %d a déjà démarré', 16, 1, @_IDPompe);
END;
ELSE
BEGIN
INSERT INTO dbo.TbPompeLog (IDPompe, DateDemarrage)
VALUES (@_IDPompe, GETDATE());
END;
END;
----------------------------------------------------
CREATE PROCEDURE psTbLogPompe_Ajoute_DateArret
@_IDPompe INT
AS
BEGIN
UPDATE dbo.TbPompeLog
SET DateArret = GETDATE()
WHERE IDPompe = @_IDPompe;
IF @@ROWCOUNT = 0
BEGIN
RAISERROR('La pompe %d n''est pas démarrée', 16, 1, @_IDPompe);
END;
END;
---------------------------------------------------
EXEC psTbLogPompe_Ajoute_DateDemarrage 1
EXEC psTbLogPompe_Ajoute_DateDemarrage 2
EXEC psTbLogPompe_Ajoute_DateArret 1
EXEC psTbLogPompe_Ajoute_DateArret 2
---------------------------------------------------
SELECT * FROM TbPompeLog
IDPompe DateDemarrage DateArret
----------- ----------------------- -----------------------
1 2008-02-12 22:58:07.700 2008-02-12 22:58:09.840
2 2008-02-12 22:58:08.733 2008-02-12 22:58:10.950 |
Partager