SQL Functions

Scalar functions return a data type such as int, money, varchar, real, etc. They can be used anywhere a built-in SQL function is allowed.


 

Voorbeeld:

 
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO 
-- ============================================= 
-- Author: -- Create date: 27-aug-2008 -- Description: Validates the user login and returns the user ID -- ============================================= 
ALTER FUNCTION [dbo].[GetUserID] ( 

-- Add the parameters for the function here 
@login nvarchar(10), 
@password nchar(40) ) 
RETURNS int AS BEGIN 

-- Declare the return variable here 
DECLARE @Result int 

-- Add the T-SQL statements to compute the return value here 
SELECT @Result = User_ID FROM [Plod].[dbo].[Login] WHERE [login] = @login AND [Password] = @password; 

-- Return the result of the function 
RETURN @Result END 

 

Voorbeeld gebruik vanuit een stored procedure:

 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
SET NOCOUNT ON; 
Declare @userId int ; 
SELECT @userId = dbo.GetUserID(@login, @password); 
IF @userID is null 
Begin PRINT 'Invalid login...' 
Print @userID 
REturn; END; 

{{opmerking.Naam}}:

{{opmerking.OpmerkingText}}

            

Saving your comment....

Naam is verplicht!
Email is verplicht!
Opmerking is verplicht!