Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.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 Server中创建数据库函数_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

在SQL Server中创建数据库函数

在SQL Server中创建数据库函数,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,请解释我的错误,需要创建一个表值函数 CREATE FUNCTION [dbo].[RetrieveEntityParent] (@FK_EntityId int (max)) with p as (SELECT EntityId, FK_ParentId , EntityName ,EntityArabicName FROM OrgEntity WHERE OrgEntity.EntityId=14 UNION ALL

请解释我的错误,需要创建一个表值函数

CREATE FUNCTION [dbo].[RetrieveEntityParent] (@FK_EntityId int (max))

   with p as
    (SELECT     EntityId, FK_ParentId , EntityName ,EntityArabicName 
     FROM         OrgEntity
     WHERE OrgEntity.EntityId=14 

   UNION ALL      
    SELECT       PA.EntityId, PA.FK_ParentId, PA.EntityName,PA.EntityArabicName 
    FROM         OrgEntity as PA 
    inner join p
    ON p.FK_ParentId = PA.EntityId)


   SELECT * from p 
的语法是:


您尝试执行该操作时抛出了什么错误?@JW。Msg 156,级别15,状态1,过程检索EntityParent,第3行关键字“with”附近语法不正确。Msg 319,15级,状态1,过程检索EntityParent,第3行关键字“with”附近语法不正确。如果此语句是公共表表达式、xmlnamespaces子句或更改跟踪上下文子句,则前一条语句必须以分号终止。执行错误消息2716,级别16,状态1,过程RetrieveEntityParent,第1行,列,参数,或变量#1:无法在数据类型int上指定列宽。我已将该类型更改为varchar,并且有效,如果可以,需要解释。(@FK_EntityId varchar(max))对,
int(max)
不存在!改为使用
int
。使用示例代码和错误消息提出新问题。在sqlfiddle.com上进行一次有效的演示总是很受欢迎的。
CREATE FUNCTION [dbo].[RetrieveEntityParent] (@FK_EntityId int (max))
RETURNS TABLE
AS RETURN
    ... your query here ...