Table-valued functions

A table-valued function is a function that returns a table instead of a single value.

Database Explorer lets you perform operations on table-valued functions using the shortcut menu. Right-click a function in Database Explorer to access options for tasks such as creating, managing, or debugging functions. You can also generate scripts for them.

Create a table-valued function

1. In Database Explorer, right-click a table-valued function or Table-valued Functions node and select New Table-valued Function.

2. In a SQL document, fill in the CREATE FUNCTION statement template.

3. At the bottom of the SQL document, click Apply Changes.

Edit a function

You can edit a function in one of these ways.

Edit using Modify via SQL

1. In Database Explorer, right-click the table-valued function you want to edit and select Modify via SQL.

2. In a SQL document, edit the function.

3. In the top menu, select File > Save or press Ctrl+S.

Edit using Open Editor

1. In Database Explorer, right-click the table-valued function you want to edit and select Open Editor.

2. In a SQL document, edit the function.

3. At the bottom of the SQL document, click Apply Changes.

Alternatively, in the top menu, select File > Save or press Ctrl+S.

Delete a table-valued function

1. In Database Explorer, right-click the table-valued function you want to delete and select Delete, or press Delete.

2. Click Yes to confirm the deletion.

Note

You cannot undo the drop function operation, as this action permanently deletes all its data and structure. It is recommended that you back up the database before deleting it.

Execute a table-valued function

1. In Database Explorer, right-click the table-valued function you want to execute and select Execute.

2. In the View output parameters dialog, enter the parameters.

3. Click Close.

Debug a table-valued function

To start debugging a table-valued function, in Database Explorer, select Step Into from the shortcut menu and follow the steps from How to debug a function.

Duplicate a table-valued function

1. In Database Explorer, right-click a required table-valued function and select Duplicate Object.

2. In the Duplicate Object dialog, configure the object settings:

  • In Destination connection, select the target server.
  • In Destination database, select the target database.
  • In Destination schema, select the target schema.
  • In New object name, specify the object name.
  • Select Drop destination object to drop the destination object if it already exists.
  • Optional: Select Script Changes, then select To New SQL Window to open the generated script in a new SQL document, or To Clipboard to copy it to the clipboard.

3. Click OK.

Generate a script for a table-valued function

1. In Database Explorer, right-click a required table-valued function and select Generate Script As, then select the statement type.

2. Select the output option:

  • To New SQL Window – Opens the script in a new SQL document.
  • To File – Generates the script to a .sql file.
  • To Clipboard – Copies the script to the clipboard.

For more information, see Generate the DDL and DML statements for database objects.

Add a table-valued function to the database diagram

To add a table-valued function to the database diagram, in Database Explorer, right-click a required table-valued function and select Send To > Database Diagram.

View function properties

To view function properties, in Database Explorer, right-click the table-valued function or Table-valued Functions node and select Show Details.

Object Viewer opens, displaying detailed information about the table-valued function.

Refresh a table-valued function

To update the table-valued function to reflect the latest changes, in Database Explorer, right-click the table-valued function or Table-valued Functions node and select Refresh, or press F5.

Open the Properties window

To open the Properties window, in Database Explorer, right-click the table-valued function and select Properties, or press F4.

Filter functions

1. In Database Explorer, right-click the Table-valued Functions node and select Filter.

2. In the Filter Settings dialog, configure the filter parameters.

3. Click OK to apply the filter.

Note

When you open the Table-valued Functions node, the number of objects it contains appears in parentheses next to the node name. This number changes based on your filter settings.

Shortcut menu options for a specific table-valued function

The following table describes the shortcut menu options, which appear when you right-click a specific table-valued function in Database Explorer.

Name Description
New Table-valued Function Opens a CREATE FUNCTION template in a new SQL document to create a table-valued function.
Modify via SQL Opens the existing function in a new SQL document containing an ALTER FUNCTION statement to update it.
Open Editor Opens the selected function in a new SQL document containing a CREATE FUNCTION statement to create the function or change its code.
Execute Executes the function.
Step Into Starts debugging the function.
Shortcut: F11
Duplicate Object Opens the Duplicate Object dialog to duplicate the function.
Generate Script As Generates a script of the selected object using the CREATE, CREATE OR ALTER, DROP, DROP and CREATE, ALTER, and SELECT statements. The script can be saved to a new SQL document or to a file, or copied to the clipboard.
Send to > Database Diagram Adds the selected function to a database diagram.
Show Details Opens Object Viewer to view object details.
Delete Drops the selected function from the database.
Shortcut: Delete
Refresh Updates the object to reflect the latest changes.
Shortcut: F5
Properties Opens the Properties pane to view the object information.
Shortcut: F4

Shortcut menu options for the Table-valued Functions node

The following table describes the shortcut menu options, which appear when you right-click the Table-valued Functions node in Database Explorer.

Name Description
New Table-valued Function Opens a new SQL document containing the CREATE FUNCTION statement.
Filter Opens the Filter Settings dialog to control which table-valued functions should appear in Database Explorer.
Show Details Opens Object Viewer to view object details.
Refresh Updates the object to reflect the latest changes.
Shortcut: F5