我在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
Run Code Online (Sandbox Code Playgroud)
我试图在第一列创建具有默认约束的表:
CREATE TABLE [dbo].[Clients](
[ModifyingUser] [nvarchar](128) NOT NULL DEFAULT sCurrentAppUser …Run Code Online (Sandbox Code Playgroud)