Reordering columns with SQL ALTER FUNCTION creates weird results in dependent functions

It appears that when you reorder the columns of a function using ALTER FUNCTION, SQL does not update the dependent functions and may result in the return of completely incorrect results.

Is there a way to force dependent functions to be updated without requiring each one to be executed manually?

Example:

create function dbo.Test1 ()
returns table
AS
return
(
    select 1 as [One], 2 as [Two]   
)

GO

create function dbo.Test2 ()
returns table
AS
return
(
    select * from Test1()
)

GO

It works as expected:

select * from Test1()
select * from Test2()

Both are returning:

One         Two
----------- -----------
1           2

Now, if we change the function:

alter function dbo.Test1 ()
returns table
AS
return
(
    select 2 as [Two], 1 as [One]   
)

GO

And request the results:

select * from Test1()

Return:

Two         One
----------- -----------
2           1

as expected, but:

select * from Test2()

Return:

One         Two
----------- -----------
2           1

So, now we have the old column order, but with the values ​​in the new positions - this means that the values ​​in this example are transposed.

+4
source share
1 answer

sp_refreshsqlmodule UDF. .

EXEC sp_refreshsqlmodule 'dbo.Test2'

sp_refreshsqlmodule UDF. UDF (, ) - - , . , , .

sp_refershsqlmodule . .

, , SELECT * UDF - SELECT.

+3

All Articles