User Defined Function Concepts Reusable blocks of SQL code Accepts parameters Possible return types Tables Scalar Values User Defined Function Concepts Advantages Modular programming Table valued functions can be used in FROM clauses Similar to a view that accepts parameters Disadvantages Performance Avoid using functions in WHERE clauses Hidden cursor Read only Advantages and Disadvantages Creating User Defined Functions Returns a single value
CREATE FUNCTION <name> (<parameters>) RETURNS <dataType> AS BEGIN -- Logic RETURN <variable> END; Scalar Functions Single SELECT statement to return values Similar to a view that accepts parameters
CREATE FUNCTION <name> (<parameters>) RETURNS TABLE AS RETURN -- SELECT statement Inline Table Valued Functions Returns a table, but can use complex logic
CREATE FUNCTION <name> (<parameters>) RETURNS <variableName> TABLE (<tableDefinition>) AS BEGIN -- Logic -- INSERT INTO <variableName> RETURN END; Multi-Statement Table Valued Function Apply Operators Join tables to functions Pass column as parameter into function Options CROSS APPLY Similar to an inner join OUTER APPLY Similar to an inner join
Excel In 7 Days : Master Excel Features & Formulas. Become A Pro From Scratch In Just 7 Days With Step-By-Step Instructions, Clear Illustrations, And Practical Examples