Based on this question, I've created a function and added it to my database. Now I want to use that function as a calculated column:
-- Based on https://mcmap.net/q/118137/-how-to-strip-all-non-alphabetic-characters-from-string-in-sql-server (work by Even Mien)
CREATE FUNCTION [dbo].[StripCharacters]
(
@String NVARCHAR(MAX),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
SET @MatchExpression = '%['+@MatchExpression+']%'
WHILE PatIndex(@MatchExpression, @String) > 0
SET @String = Stuff(@String, PatIndex(@MatchExpression, @String), 1, '')
RETURN @String
END
GO
-- Table
CREATE TABLE [dbo].[Trailer]
(
[ID] INT NOT NULL PRIMARY KEY IDENTITY,
[ID_Hauler] INT NULL,
[RegistrationNumber] NCHAR(9) NOT NULL,
[RegistrationNumberSimplified] AS [dbo].StripCharacters([RegistrationNumber], '^A-Z0-9'),
[MaxLoad] FLOAT NULL,
CONSTRAINT [FK_Hauler_Trailer] FOREIGN KEY ([ID_Hauler]) REFERENCES [Hauler]([ID]),
CONSTRAINT [UC_RegistrationNumber] UNIQUE ([RegistrationNumberSimplified])
)
However, the line where I reference the function throws an error:
"Berechnete Spalte: [dbo].[Trailer].[RegistrationNumberSimplified]" enthält einen nicht aufgelösten Verweis auf ein Objekt. Entweder ist das Objekt nicht vorhanden, oder der Verweis ist mehrdeutig, da er auf die folgenden Objekte verweisen könnte: [dbo].[StripCharacters] oder [dbo].[Trailer].[dbo]::[StripCharacters].
"Either the object doesn't exist, or the reference is ambigious, because it could mean either [dbo].[StripCharacter] or [dbo].[Trailer].[dbo]::[StripCharacter]."
I've also tried to let the server guess the namespace of the function. In that case, the database throws an error:
(57,1): SQL72014: .Net SqlClient Data Provider: Meldung 195, Ebene 15, Status 10, Zeile 11 'StripCharacters' is not a recognized built-in function name.
What's the problem here?
I also found this question, but the answer doesn't help me, because I don't use database references.
fn_StripCharacters
, notStripCharacters
– Achernarnvarchar(MAX)
. Change that and you get this error (which I'd already warned you about in your other question). – AchernarCONSTRAINT
as the value is not deterministic. You can't fix that with the function you have – Achernar[RegistrationNumberSimplified] NCHAR(9) NOT NULL DEFAULT [dbo].StripCharacters([RegistrationNumber], '^A-Z0-9'),
. – Arrowooddbo.StripCharacters
? – ReeducateWHILE
can be deterministic, for using a tally is not, @MartinSmith :/ – AchernarWITH SCHEMABINDING
is considered to be deterministic.SELECT OBJECTPROPERTY(OBJECT_ID('[dbo].[StripCharacters]'), 'IsDeterministic')
returns 1. – Arrowood