Arjuna Aircraft Ident.: F-MBSD | PS = Procédure Stockée
C'est une procédure/fonction crée directement dans la base de données, en utilisant un langage spécifique, tel que PL/SQL, T-SQL ou autre.
Exemple d'une PS :
Code :
- CREATE procedure icsd_GetOrgUsersList
- (
- @orgid int,
- @onlyInternal int,
- @onlyActive int
- )
- AS
- if @onlyInternal = 0
- begin
- select UserOrg.UserId
- from Users, UserOrg
- where UserOrg.OrgId = @OrgId
- and Users.UserId = UserOrg.UserId
- and Users.Type = 'U'
- and (Users.Active = 'A' or @onlyActive = 0)
- end
- else
- begin
- select UserOrg.UserId
- from zLoginProfile, Users, UserOrg
- where UserOrg.OrgId = @OrgId
- and Users.UserId = UserOrg.UserId
- and Users.Type = 'U'
- and (Users.Active = 'A' or @onlyActive = 0)
- and zLoginProfile.UserId = Users.UserId
- and zLoginProfile.EventsCommentInt = 1
- end
- GO
|
Les avantages des PS sont divers :
1/ Plan d'éxécution déjà compilé. Ainsi, le moteur de données ne s'amuse pas à reparser la requête et chercher les index les plus intéressants pour ta requête. Ainsi, sur un bon SGBD, tu peux diminuer le temps d'éxécution des requêtes par 5 facilement.
2/ Pas d'échange de données avec un process externe à la base. Ainsi, l'occupation mémoire est très faible, et surtout, à nouveau on gagne énormément en temps, puisque les données n'ont pas à être baladées d'un programme à l'autre.
3/ Code très clair : lors de l'appel, tu fais, pour ma procédure par exemple, simplement "exec icsd_GetOrgUsersList 1, 0, 1" et ça te retourne tous les utilisateurs de l'organisation "1", internes et externes confondus, et seulement les actifs. Ca peut très rapidement devenir intéressant quand t'as des requêtes extrêment complexes, et différentes selon des conditions (exemple en fin de post)
Y'a d'autres avantages, mais ce sont les plus flagrants.
Exemple d'une procédure stockée qu'on préfère ne pas avoir à taper dans une page PHP
Code :
- CREATE procedure icsd_ARTContractList
- (
- @orgid int,
- @userid int,
- @leasescheduleid int = 0,
- @leasecontractid int = 0,
- @duration int = 0,
- @date1 datetime = null,
- @date2 datetime = null,
- @orderby varchar(255) = 'org'
- )
- AS
- BEGIN
- select name, leasescheduleid, schedule, leasecontractid, contract, leasestopdate, duration, RentalAmount, billfreq, tax2, nbProducts, nbAssets
- from (
- select o.name,
- ls.leasescheduleid,
- ls.schedule,
- lc.leasecontractid,
- lc.contract,
- lc.leasestopdate,
- isnull(datediff(m, lc.leasestartdate, lc.leasestopdate), -1) duration,
- sum(zf.prixFacture) rentalAmount,
- lc.billfreq,
- cl.tax2,
- count(distinct a.ProductCode) nbProducts,
- count(distinct a.assetid) nbAssets
- from asset a, zloyer zl, zFactureAsset zf, contractline cl, leasecontract lc, leaseschedule ls, zUserContractPerimeter zu, UserOrg uo, organization o
- where (o.orgid = @orgid or @orgid = 0)
- and o.type = 'C'
- and o.active = 'A'
- and uo.orgid = o.orgid
- and uo.userid = @userid
- and zu.userid = uo.userid
- and zu.orgid = uo.orgid
- and (zu.scheduleid = @leasescheduleid or @leasescheduleid = 0)
- and ls.orgid = o.orgid
- and ls.leasescheduleid = zu.scheduleid
- and ls.active = 'A'
- and ls.userights = 1
- and lc.orgid = ls.orgid
- and lc.leasescheduleid = ls.leasescheduleid
- and (lc.leasecontractid = @leasecontractid or @leasecontractid = 0)
- and (lc.leasecontractid = zu.contractid or zu.contractid = -1)
- and lc.active = 'A'
- and (lc.leasestopdate between @date1 and @date2 or @date1 is null or @date2 is null or @date1 > @date2)
- and (datediff(m, lc.leasestartdate, lc.leasestopdate) = @duration or @duration = 0)
- and cl.contractlineid = (select max(cl2.contractlineid)
- from contractline cl2
- where cl2.leasecontractid = lc.leasecontractid
- and cl2.active = 'A')
- and zf.leaseScheduleId = ls.leaseScheduleId
- and zf.leaseContractId = lc.leaseContractId
- and zf.ContractLineId = cl.ContractLineID
- and zl.leasecontractid = cl.leasecontractid
- and zl.contractlineid = cl.ContractLineID
- and a.orgid = o.orgid
- and a.linetype in ('B', 'S')
- and a.assetid = zl.assetid
- group by
- o.name,
- ls.leasescheduleid,
- ls.schedule,
- lc.leasecontractid,
- lc.contract,
- lc.leasestopdate,
- isnull(datediff(m, lc.leasestartdate, lc.leasestopdate), -1),
- lc.billfreq,
- cl.tax2
- union all
- select o.name,
- ls.leasescheduleid,
- ls.schedule,
- lc.leasecontractid,
- lc.contract,
- lc.leasestopdate,
- isnull(datediff(m, lc.leasestartdate, lc.leasestopdate), -1) duration,
- sum(zf.prixFacture) rentalAmount,
- lc.billfreq,
- cl.tax2,
- count(distinct a.ProductCode) nbProducts,
- count(distinct a.assetid) nbAssets
- from asset a, zloyer zl, zFactureAsset zf, contractline cl, leasecontract lc, leaseschedule ls, UserOrg uo, organization o
- where (o.orgid = @orgid or @orgid = 0)
- and o.type = 'C'
- and o.active = 'A'
- and uo.orgid = o.orgid
- and uo.userid = @userid
- and ls.orgid = o.orgid
- and (ls.leasescheduleid = @leasescheduleid or @leasescheduleid = 0)
- and ls.active = 'A'
- and ls.userights = 0
- and lc.orgid = ls.orgid
- and lc.leasescheduleid = ls.leasescheduleid
- and lc.active = 'A'
- and (lc.leasecontractid = @leasecontractid or @leasecontractid = 0)
- and (lc.leasestopdate between @date1 and @date2 or @date1 is null or @date2 is null or @date1 > @date2)
- and (datediff(m, lc.leasestartdate, lc.leasestopdate) = @duration or @duration = 0)
- and cl.contractlineid = (select max(cl2.contractlineid)
- from contractline cl2
- where cl2.leasecontractid = lc.leasecontractid
- and cl2.active = 'A')
- and zf.leaseScheduleId = ls.leaseScheduleId
- and zf.leaseContractId = lc.leaseContractId
- and zf.ContractLineId = cl.ContractLineID
- and zl.leasecontractid = cl.leasecontractid
- and zl.contractlineid = cl.ContractLineID
- and a.orgid = o.orgid
- and a.linetype in ('B', 'S')
- and a.assetid = zl.assetid
- group by
- o.name,
- ls.leasescheduleid,
- ls.schedule,
- lc.leasecontractid,
- lc.contract,
- lc.leasestopdate,
- isnull(datediff(m, lc.leasestartdate, lc.leasestopdate), -1),
- lc.billfreq,
- cl.tax2
- ) tmp
- order by case @orderby
- when 'org' then name
- when 'contract' then contract
- when 'stopdate' then convert(char(8), leasestopdate, 112)
- when 'duration' then right('0000000000' + duration, 10)
- when 'rentalamount' then right('0000000000' + rentalamount, 10)
- when 'billfreq' then billfreq
- when 'tax2' then right('0000000000' + tax2, 10)
- when 'nbproducts' then right('0000000000' + nbproducts, 10)
- when 'nbassets' then right('0000000000' + nbassets, 10)
- end,
- case @orderby
- when 'org' then schedule
- when 'contract' then contract
- when 'stopdate' then convert(char(8), leasestopdate, 112)
- when 'duration' then right('0000000000' + duration, 10)
- when 'rentalamount' then right('0000000000' + rentalamount, 10)
- when 'billfreq' then billfreq
- when 'tax2' then right('0000000000' + tax2, 10)
- when 'nbproducts' then right('0000000000' + nbproducts, 10)
- when 'nbassets' then right('0000000000' + nbassets, 10)
- end
- END
- GO
|
|