SQL 服务器中的函数名称不明确
Function name is ambigious in SQL server
根据 this 问题,我创建了一个函数并将其添加到我的数据库中。现在我想将该函数用作计算列:
-- Based on (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])
)
但是,我引用函数的那一行抛出错误:
"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]."
我也试过让服务器猜测函数的命名空间。在这种情况下,数据库会抛出一个错误:
(57,1): SQL72014: .Net SqlClient Data Provider: Meldung 195, Ebene 15,
Status 10, Zeile 11 'StripCharacters' is not a recognized built-in
function name.
这里有什么问题?
我也发现了 问题,但答案对我没有帮助,因为我不使用数据库引用。
您遇到的问题是函数返回的值太大而不能用作键列。最大值为 NVARCHAR(850) 或 VARCHAR(1700)。这会起作用:
-- Based on (work by Even Mien)
ALTER FUNCTION [dbo].[StripCharacters]
(
@String NVARCHAR(850),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(850) WITH SCHEMABINDING
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])
);
好奇怪的问题!将函数的源代码复制到我的数据库项目中新建的scalar function
文件中,然后删除原文件,之后重命名新建的文件,终于成功了。因为我根本没有更改任何代码,所以我猜这是 Visual Studio.
的一些非常奇怪的错误
根据 this 问题,我创建了一个函数并将其添加到我的数据库中。现在我想将该函数用作计算列:
-- Based on (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])
)
但是,我引用函数的那一行抛出错误:
"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]."
我也试过让服务器猜测函数的命名空间。在这种情况下,数据库会抛出一个错误:
(57,1): SQL72014: .Net SqlClient Data Provider: Meldung 195, Ebene 15, Status 10, Zeile 11 'StripCharacters' is not a recognized built-in function name.
这里有什么问题?
我也发现了
您遇到的问题是函数返回的值太大而不能用作键列。最大值为 NVARCHAR(850) 或 VARCHAR(1700)。这会起作用:
-- Based on (work by Even Mien)
ALTER FUNCTION [dbo].[StripCharacters]
(
@String NVARCHAR(850),
@MatchExpression VARCHAR(255)
)
RETURNS NVARCHAR(850) WITH SCHEMABINDING
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])
);
好奇怪的问题!将函数的源代码复制到我的数据库项目中新建的scalar function
文件中,然后删除原文件,之后重命名新建的文件,终于成功了。因为我根本没有更改任何代码,所以我猜这是 Visual Studio.