简体   繁体   English

重新定义从属对象后,如何刷新T-SQL用户定义函数的定义?

[英]How to refresh the definition of a T-SQL user-defined function after a dependent object is redefined?

Consider the following sequence of events: 请考虑以下事件序列:

  1. A view v_Foo is defined 视图v_Foo已定义
  2. A user-defined function GetFoo() is defined that includes all columns from v_Foo (using 'Select * ...') 定义了一个用户定义的函数GetFoo(),该函数包括v_Foo中的所有列(使用“选择* ...”)
  3. The definition of v_Foo changes and now includes more columns v_Foo的定义更改,现在包含更多列
  4. I now want GetFoo() to include the new columns in v_Foo, but it still references the old definition 我现在希望GetFoo()在v_Foo中包括新列,但它仍引用旧定义

I can just re-run the script that created GetFoo in the first place and all will be well; 我可以重新运行最初创建GetFoo的脚本,一切都会好起来。 but that's problematic for reasons I won't go into. 但这是有问题的,原因是我不愿讨论。 Is there any other way to refresh the definition of a user-defined function so that it's in sync with its dependent objects? 还有其他方法可以刷新用户定义函数的定义,使其与其相关对象保持同步吗?

Short, easy answer is No . 简短而简单的答案是“ 否”

You have to redefine the RETURN TABLE statement in Tabular UDF, GetFoo() 您必须在表格UDF中重新定义RETURN TABLE语句GetFoo()
whenever the definition of v_Foo changes. 每当v_Foo的定义更改时。

But there is a way to get around it (translated as not practical). 但是有一种解决方法(翻译成不实际的方法)。

  1. Create a DDL trigger on ALTER_VIEW event. ALTER_VIEW事件上创建DDL触发器。
  2. Then use a dynamic SQL to create the GetFoo() . 然后使用动态SQL创建GetFoo()

It would be nice to see the definition of the function. 很高兴看到函数的定义。 All you've said is it is using SELECT *. 您所说的只是使用SELECT *。 Can you be more specific? 你可以说得更详细点吗?

You also forgot to tell us what version of SQL Server you are using. 您还忘了告诉我们您使用的SQL Server版本。 If >= 2005, have you looked at sp_refreshsqlmodule ? 如果> = 2005,您是否看过sp_refreshsqlmodule

Curious what your reasons are for insisting on SELECT *. 好奇您坚持SELECT *的原因是什么。 Lots of discussion about it here, but the cons still outweigh the pros by a large margin, IMHO: 在这里有很多讨论,但是缺点还是很大,IMHO:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在T-SQL中将CASE语句转换为用户定义的函数 - How to convert a CASE Statement into a User-defined Function in T-SQL T-SQL:如何通过其ID获取列的用户定义数据类型? - T-SQL: How can I get user-defined data-type of the column by its ID? T-SQL用户定义函数重载? - T-SQL User defined function overloading? 如何将 Postgres plpgsql 用户定义的 function 转换为 LANGUAGE SQL 用户定义的 function? - How to convert Postgres plpgsql user-defined function to LANGUAGE SQL user-defined function? 将用户定义的T-SQL查询限制为仅SELECT类型 - Restrict user-defined T-SQL query to SELECT type only 在T-Sql中使用用户定义的数据类型识别所有存储过程和表 - Identifying all stored procedures and tables using a User-Defined Data Type in T-Sql 如何从SQL Server用户定义的函数报告错误 - How to report an error from a SQL Server user-defined function 大写第一个字母,每个单词,修复可能的字符串拆分顺序问题,T-SQL 2017 中的多个分隔符,而不使用用户定义的函数 - Capitalize first letter, every word, fix possible string split order issues, multiple delimiters in T-SQL 2017 without using a user-defined function SQL用户定义函数,找不到dbo或用户定义函数 - SQL User defined function, can't find dbo or user-defined function SQL 服务器中用户定义的 function 出现问题 - Problem with user-defined function in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM