O'Reilly logo

Microsoft® SQL Server® 2008 Step by Step by Mike Hotek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Function

Although functions are used to perform calculations, a function can’t change the state of a database or instance. Functions can’t:

  • Perform an action that changes the state of an instance or database.

  • Modify data in a table.

  • Call a function that has an external effect such as the RAND() function.

  • Create or access temporary tables.

  • Dynamically execute code.

Functions can either return a scalar-value or a table. Table-valued functions can be of two different types: inline and multi-statement.

The general syntax for a scalar function is:

CREATE FUNCTION [ schema_name. ] function_name ( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type [ = default ] [ READONLY ] } [ ,...n ] ]) RETURNS return_data_type [ WITH <function_option> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required