是否可以在TSQL查询中定义本地函数?

Fra*_*ois 23 t-sql sql-server user-defined-functions

我有一个复杂的表达式来计算我必须在多个日期列上使用的日期的值.
我可以在查询中定义临时本地函数,以避免复制和粘贴此表达式.?

喜欢:

create MyLocalFunc(@ADate datetime) 
returns int as
begin
  blablabla
end

select
  MyLocalFunc(col1), col2, MyLocalFunc(col3), col4, MyLocalFunc(col5)
from
  mytable
Run Code Online (Sandbox Code Playgroud)

作为一种解决方法,我知道我可以做一个CREATE FUNCTION // DROP FUNCTION,但我宁愿避免它.

Ben*_*n M 7

不,没有办法 - 创建/删除是唯一的选择.