I created a scaling UDF (called sCurrentAppUser ()) in SQL Server 2012 Express, and I would like to use this UDF as the default value when defining a table. But every time I try, I get the error message "sCurrentAppUser" is not a recognized built-in function name.
Since I cannot post more than two links (reputation), I will refer to my research and links in the comments.
Here is my 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
And my attempt to create a table with a default constraint in the first column:
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
)
)
Jason source
share