简体   繁体   English

Reporting Services:在报表查询中创建用户定义的函数。 这是真的吗?

[英]Reporting Services: Creating user defined functions in report query. Is it real?

Is it possible to create and use a user defined function in a report query? 是否可以在报表查询中创建和使用用户定义的函数?

Something like this: 像这样:

if OBJECT_ID('userFunc', 'TF') is not null drop function userFunc
go

create function userFunc(@id int)
returns @tbl table
([id] int)
begin
insert into @tbl (id) values(@id)
return
end
go

select * from userFunc(1)

if OBJECT_ID('userFunc', 'TF') is not null drop function userFunc

When i run this query in SQL Server 2008 Management Studio it returns me 'id':1, but when i put this query into Reporting Query designer - i get the next error: 当我在SQL Server 2008 Management Studio中运行此查询时,它返回我'id':1,但是当我将此查询放入报表查询设计器中时-我得到下一个错误:

TITLE: Microsoft Report Designer 标题:Microsoft报表设计器

An error occurred while the query design method was being saved. 保存查询设计方法时发生错误。 Incorrect syntax near 'go'. 'go'附近的语法不正确。

'CREATE FUNCTION' must be the first statement in a query batch. “ CREATE FUNCTION”必须是查询批处理中的第一条语句。

Incorrect syntax near 'go'. 'go'附近的语法不正确。

Incorrect syntax near 'userFunc'. 'userFunc'附近的语法不正确。

Any suggestions? 有什么建议么? How to create and use udf in Reporting Services queries? 如何在Reporting Services查询中创建和使用udf?

"GO" will only be recognised by SQL tools : not the DB engine nor SSRS parser “ GO”将仅由SQL工具识别 :数据库引擎或SSRS解析器均不识别

This might if you really want to 如果您真的想这样做

EXEC 'if OBJECT_ID(''userFunc'') is not null drop function userFunc'
EXEC 'create function userFunc(@id int)
returns @tbl table
([id] int)
begin
insert into @tbl (id) values(@id)
return
end'
select * from userFunc(1)

However, you'll need ddl_admin or db_owner to run it. 但是,您将需要ddl_admin或db_owner来运行它。

Have you considered a CTE or derived table? 您是否考虑过CTE或派生表? A table valued udf like this can be replaced by a CTE 像这样的udf值表可以由CTE替换

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM