J’ai décidément mis longtemps à trouver pourquoi une fonction toute simple que j’utilisait pour remplir un colonne calculée sous SQL Server (histoire de poser un index dessus), n’était pas déterministe !
Et bien la preuve avec le Script ci dessous, le déterminisme d’une fonction n’est possible que si elle est “schemabound” :
use TEST
GO
create function IsNotDeterministic()
returns bit
AS
Begin
return 0
End
GO
select 'Une fonction non shemabound ne peut être déterminisme :',
OBJECTPROPERTY ( OBJECT_ID(N'IsNotDeterministic') , N'IsSchemaBound') as 'ShemaBound',
OBJECTPROPERTY ( OBJECT_ID(N'IsNotDeterministic') , N'IsDeterministic') as 'Deterministic'
GO
drop function IsNotDeterministic
GO
GO
create function IsDeterministic()
returns bit
with schemabinding
AS
Begin
return 1
End
GO
select 'Une fonction schemabound a plus de chance :',
OBJECTPROPERTY ( OBJECT_ID(N'IsDeterministic') , N'IsSchemaBound') as 'ShemaBound',
OBJECTPROPERTY ( OBJECT_ID(N'IsDeterministic') , N'IsDeterministic') as 'Deterministic'
GO
drop function IsDeterministic
GO
L’option shemabinding permet à SQL Server d’être ‘rassurer’ quand à la disponibilité des objets liés, ils ne sont pas modifiables, ni supprimables, ils sont là …. ouff.
Résultat du script :
ShemaBound Deterministic
------------------------------------------------------- ----------- -------------
Une fonction non shemabound ne peut être déterminisme : 0 0
(1 ligne(s) affectée(s))
ShemaBound Deterministic
------------------------------------------- ----------- -------------
Une fonction schemabound a plus de chance : 1 1
(1 ligne(s) affectée(s))
Aucun commentaire:
Publier un commentaire