Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL用户定义函数中使用公共表表达式时出错_Sql_Sql Server_Sql Function - Fatal编程技术网

在SQL用户定义函数中使用公共表表达式时出错

在SQL用户定义函数中使用公共表表达式时出错,sql,sql-server,sql-function,Sql,Sql Server,Sql Function,SQL不是我的强项,但我不知道我在这里做错了什么。我希望函数从两个表的并集返回最大的实体_id。运行脚本会出现以下错误: CREATE FUNCTION [dbo].[udfGetNextEntityID] () RETURNS INT AS BEGIN ;WITH allIDs AS ( SELECT entity_id FROM Entity UNION SELECT entity_id FROM Reserved_Entity )

SQL不是我的强项,但我不知道我在这里做错了什么。我希望函数从两个表的并集返回最大的实体_id。运行脚本会出现以下错误:

CREATE FUNCTION [dbo].[udfGetNextEntityID]
()
RETURNS INT
AS
BEGIN
    ;WITH allIDs AS
    (
    SELECT entity_id FROM Entity 
    UNION SELECT entity_id FROM Reserved_Entity
    )       
  RETURN (SELECT (MAX(entity_id) FROM allIDs )

END
GO

我查看了函数中使用CTE是否有一些限制,但找不到任何相关内容。如何更正此问题?

您不能从函数返回您正在执行的操作

 Incorrect syntax near the keyword 'RETURN'.
使用局部变量并返回相同的值

CREATE FUNCTION [dbo].[udfGetNextEntityID]()
RETURNS INT
AS
BEGIN
  DECLARE @result INT;

  WITH allIDs AS
  (
    SELECT entity_id FROM Entity 
    UNION SELECT entity_id FROM Reserved_Entity
  )       
  SELECT @result = MAX(entity_id) FROM allIDs;

  RETURN @result;

END
GO

既然你能做到,为什么你需要一个CTE呢

 CREATE FUNCTION [dbo].[udfGetNextEntityID]()
    RETURNS INT
    AS
    BEGIN
      DECLARE @MaxEntityId INT;

      WITH allIDs AS
      (
        SELECT entity_id FROM Entity 
        UNION SELECT entity_id FROM Reserved_Entity
      )       
      SELECT @MaxEntityId = MAX(entity_id) FROM allIDs;

      RETURN @MaxEntityId ;

    END
 GO

此外,没有理由使用UNION而不是UNIONALL,因为这几乎总是会引入昂贵的独特排序操作。和。

+1同意。此外,对于Union Vs Union All,在本例中,您需要最大值,而不关心行是否不同。所以,不要使用UNION,因为UNION会给出不同的行,但会影响性能。使用UNIONALL代替。谢谢@Ashish,但这不是我说的吗-当然,艾伦。因为问这个问题的人说SQL不是我的强项。所以,我想用UNION Vs UNION ALL的链接来补充你的答案,以供参考。就是这样。但是添加一个局部变量意味着它不再是一个内联函数-速度较慢。根本没有说你错了——只是在寻找CTE和内嵌UDF的优点。请参阅,仅提供代码不是正确答案。请详细说明。
  RETURN
  (
    SELECT MAX(entity_id) FROM
    (
      SELECT entity_id FROM dbo.Entity 
      UNION ALL
      SELECT entity_id FROM dbo.Reserved_Entity
    ) AS allIDs
  );
create function tvfFormatstring (@string varchar(100))
returns @fn_table table
(id int identity(1,1),
item int)
as
begin

insert into @fn_table(item)
declare @result int 
set @string = @string+'-'
;with cte (start,number)
as
(

select 1 as start , CHARINDEX('-',@string,1) as number
union all
select number+1 as start , CHARINDEX('-',@string,number+1) as number from cte 
where number <= LEN(@string)

)

select @result = SUBSTRING(@string,start,number-start) from cte ;
return @result;

end



select * from tvfFormatstring ('12321-13542-15634')