【问题标题】:How to Call Function with pass select to param如何通过将选择传递给参数来调用函数
【发布时间】:2015-07-21 21:10:43
【问题描述】:

我有一个带有参数varchar的函数。

我想调用函数并将选择结果传递给函数

Select * from dbo.myfunc(select name from users)

但我收到此错误:

')' 附近的语法不正确

我能做什么?

【问题讨论】:

  • 您的错误是因为函数调用和子查询一样需要括号。你只有一套。但是,将子查询传递给具有 varchar 参数的函数无论如何都是错误的。 @Parado 的回答对我来说看起来不错。

标签: sql sql-server


【解决方案1】:

可能是这样的:

select dbo.myfunc(name) from users

【讨论】:

  • 是否会为用户表中的每条记录调用该函数?
  • @knkarthick24 会的
  • @Parado 我得到了这个错误:
  • Cannot find either column "dbo" or the user-defined function or aggregate "dbo.myfunc", or the name is ambiguous.
  • @PsarTak 没问题 :) 你能运行我在预览评论中放的代码吗?
【解决方案2】:

使用apply:

Select f.*
from users u cross apply
     dbo.myfunc(u.name) as f;

【讨论】:

  • @PsarTak 。 . .这命名了函数的结果。 f`` is the table alias and the 。 . .` 是列。
猜你喜欢
  • 2012-08-30
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多