SQL Server scalar value function instance and cannot invoke cause analysis
--scalar value function
Set ANSI_NULLS on
Go
SET QUOTED_IDENTIFIER on
Go
-================================ =============
--Author: <author,,name>
--Create Date: <create date,,>
--Description: <de Scription,,>
--=============================================
Create function <scalar_function_name, sysname, functionname>
(
--Add the parameters for the function here
< @param1, sysname, @p1 > <d ATA_TYPE_FOR_PARAM1,, int>
)
returns <function_data_type,,int>
as
begin
--Declare the Return variable
Declare < @resultvar, sysname, @result > <function_data_type, Int>
--Add t He T-SQL statements to compute "return value" here
Select < @resultvar, sysname, @result > = < @param1, Sysna Me, @p1
--Return the result's function
return < @resultvar, sysname, @result
end
With the MS SQL scalar value function, you should precede the function with "dbo.", otherwise the "not recognized built-in function name" error will be reported. For example
declare @whichdb tinyint;
Select @whichdb = user_getwhichdb (1);--to see which database tutorial
=================================================
--Scalar value function
alter function [dbo]. [USER_GETWHICHDB]
(
@userid int = 0
)
Returns tinyint
With EXECUTE AS caller
As
Begin
declare @whichdb tinyint;
Set @whichdb = 1;
If @userid >= 115098
Set @whichdb = 2;
return (@whichdb);
End