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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
/*Creation de la table*/
CREATE TABLE [dbo].[newcategory] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[nc_lb] [int] NULL ,
[nc_rb] [int] NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
/*Procedure d'insertion dans la table*/
CREATE PROCEDURE newcategory_insert
@name varchar(50),
@father_id int
AS
BEGIN
declare @father_rb int
set @father_rb=(select nc_rb from newcategory where id=@father_id) ;
Update newcategory set nc_rb=nc_rb+2 where nc_rb>=@father_rb;
Update newcategory set nc_lb=nc_lb+2 where nc_lb>=@father_rb;
Insert into newcategory (nc_lb,nc_rb,[name]) values (@father_rb,@father_rb+1,@name);
END
GO
/*Procedure d'update de la table*/
CREATE PROCEDURE newcategory_update
@id int,
@name varchar(50),
@newfather_id int
AS
BEGIN
declare @rb int
declare @lb int
declare @flb int
declare @frb int
set @lb=(select nc_lb from newcategory where id=@id)
set @rb=(select nc_rb from newcategory where id=@id)
update newcategory set nc_lb=nc_lb-2 where nc_lb>@rb
update newcategory set nc_rb=nc_rb-2 where nc_rb>@rb
update newcategory set nc_lb = nc_lb-1 where nc_lb>@lb and nc_lb<@rb
update newcategory set nc_rb = nc_rb-1 where nc_rb>@lb and nc_rb<@rb
set @frb=(select nc_rb from newcategory where id=@newfather_id)
set @flb=(select nc_lb from newcategory where id=@newfather_id)
update newcategory set nc_rb= nc_rb+2 where nc_rb>=@frb
update newcategory set nc_lb= nc_lb+2 where nc_lb>=@frb
update newcategory set nc_lb=@frb, nc_rb=@frb+1, name=@name where id=@id
END
GO
/*Procedure de suppression dans la table*/
CREATE PROCEDURE newcategory_delete
@id int
AS
BEGIN
Declare @lb int
declare @rb int
declare @idc int;
set @idc=(select id from newcategory where id=@id)
if @idc>1
begin
set @rb=(select nc_rb from newcategory where id=@id)
delete from newcategory where id=@id
Update newcategory set nc_rb=nc_rb-2 where nc_rb>@rb
Update newcategory set nc_lb=nc_lb-2 where nc_lb>@rb
end
DBCC CheckIdent(newcategory,reseed,1)
DBCC CheckIdent(newcategory,reseed)
END
GO
/*Fonction permettant de retrouver la mere d'une categorie*/
CREATE FUNCTION uppernewcategory (@id int)
RETURNS int AS
BEGIN
declare @lb int
declare @rb int
declare @upr int
declare @ret int
set @lb=(Select nc_lb from newcategory where id=@id)
set @rb=(Select nc_rb from newcategory where id=@id)
set @upr=(select min (nc_rb) from newcategory where nc_rb>@rb and nc_lb<@lb)
set @ret=(select id from newcategory where nc_rb=@upr)
return @ret
END
GO |
Partager