Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table-valued function refresh

I have a table-valued function (TVF) in SQL Server that looks like this:

CREATE FUNCTION TVF_xyz(@AuditKey INT)
RETURNS TABLE
AS
    RETURN
        SELECT *
        FROM xyz 
        WHERE AUDIT_KEY = @AuditKey
GO

Now, I added a new column to the xyz table.

When I query using TVF_xyz, it doesn't show me the new column (shows all other columns except newly added).

Query:

SELECT TOP 10 * 
FROM TVF_xyz (1543)

I would like to know, how to refresh TVF to show new column.

PS: Select * used in TVF to fetch all columns.

like image 887
Ash Avatar asked Mar 31 '17 01:03

Ash


People also ask

Can we perform update operation in table valued function?

You cannot update a table-valued function.

Can SQL function update the record of the table?

An UPDATE statement is used to update the quantity of the item specified by itemNo in table Inventory , by the amount specified by amount . A result set containing the product name and the new quantity of the item is returned. Note that the MODIFIES SQL DATA clause is used because function updates table data.

What is table valued function?

A table function, also called a table-valued function (TVF), is a user-defined function that returns a table. You can use a table function anywhere that you can use a table. Table functions behave similarly to views, but a table function can take parameters.


1 Answers

After bit of searching, I found sp_refreshsqlmodule (Transact-SQL), its common behavior of TVF.

In order to refresh TVF, following SP needs to be executed:

EXEC sys.sp_refreshsqlmodule 'TVF_xyz'
like image 156
Ash Avatar answered Oct 17 '22 07:10

Ash