问题
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.
回答1:
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'
回答2:
https://msdn.microsoft.com/en-us/library/bb386954(v=vs.110).aspx
The following SQL function explicitly states that it returns a TABLE. Therefore, the returned rowset structure is implicitly defined.
sample
CREATE FUNCTION ProductsCostingMoreThan(@cost money)
RETURNS TABLE
AS
RETURN
SELECT ProductID, UnitPrice
FROM Products
WHERE UnitPrice > @cost
来源:https://stackoverflow.com/questions/43131121/table-valued-function-refresh