我在SQL Server 2012 Express中创建了一个缩放器UDF(称为sCurrentAppUser()),我想在定义表时使用此UDF作为默认值。但每次我尝试时,都会收到“'sCurrentAppUser'不是公认的内置函数名称的错误。”
由于我还不能发布两个以上的链接(声誉),我会在评论中链接到我的研究和参考文献。
这是我的UDF:
ALTER FUNCTION [dbo].[sCurrentAppUser] ()
RETURNS nVarChar(128)
AS BEGIN
DECLARE @CurrentAppUser nVarChar(128)
IF EXISTS (SELECT 1 FROM ActiveConnections WHERE ComputerName = host_name ()) BEGIN
SELECT @CurrentAppUser = CONVERT (nVarChar(128), LoginUser)
FROM ActiveConnections
WHERE ComputerName = host_name ()
END ELSE BEGIN
SELECT @CurrentAppUser = Convert (nVarChar(128), suser_sname ())
WHERE NOT EXISTS (
SELECT 1
FROM ActiveConnections
WHERE ComputerName = host_name ()
)
END
RETURN @CurrentAppUser
END
我尝试使用第一列的默认约束创建表:
CREATE TABLE [dbo].[Clients](
[ModifyingUser] [nvarchar](128) NOT NULL DEFAULT sCurrentAppUser (),
[Modification] [char](1) NULL DEFAULT 'A',
[ModifyingHost] [nvarchar](128) NOT NULL DEFAULT host_name (),
[ClientID] [uniqueidentifier] NOT NULL,
[Label] [nvarchar](1024) NULL,
CONSTRAINT [PK_Clients] PRIMARY KEY (
[ClientID] ASC
)
)
答案 0 :(得分:6)
您应该为该功能添加架构名称:
CREATE TABLE [dbo].[Clients](
[ModifyingUser] [nvarchar](128) NOT NULL DEFAULT dbo.sCurrentAppUser (),
[Modification] [char](1) NULL DEFAULT 'A',
[ModifyingHost] [nvarchar](128) NOT NULL DEFAULT host_name (),
[ClientID] [uniqueidentifier] NOT NULL,
[Label] [nvarchar](1024) NULL,
CONSTRAINT [PK_Clients] PRIMARY KEY (
[ClientID] ASC
)
)