DECLARE @command nvarchar(max);
SET @command = 
	'IF OBJECT_ID(''[dbo].[fPA_UserGetCurrentUserId]'',N''FN'') IS NULL'
	+CHAR(13) + CHAR(10)+	'BEGIN'
	+CHAR(13) + CHAR(10)+	N'EXEC('''	
	+	N'CREATE FUNCTION [dbo].[fPA_UserGetCurrentUserId]()
		RETURNS INT
		WITH SCHEMABINDING
		AS
		BEGIN
		   RETURN(ISNULL((SELECT TOP 1 [dbo].[tPA_User].[anUserId] FROM [dbo].[tPA_User] WHERE [dbo].[tPA_User].[acUserId] = ORIGINAL_LOGIN()), 0));
		END;'
	+	''')'
	+ CHAR(13) + CHAR(10) +	'END'
	;

EXEC(@command);