按整数获取字符串

时间:2012-12-23 10:13:54

标签: sql sql-server sql-server-2008

我在db存储Entityid和其他一个存储Entityname中有一个列,我需要一个存储过程,当用户插入id(按列名称)时获取名称,我有一个正在处理,我该如何改进它.plz解释..

ALTER   procedure [dbo].[Emp_CompanyHirarchy]
  @FK_CompanyId varchar(50),
  @FK_EntityId varchar(50)
AS
  SELECT * FROM dbo.RetrieveEntityParent
    WHERE (EntityId =@FK_EntityId)

当我执行此操作时,错误会一直显示:消息216,级别16,状态1,过程Emp_CompanyHirarchy,第5行 没有为函数'dbo.RetrieveEntityParent'提供参数。

函数dbo.RetrieveEntityParent:

ALTER FUNCTION [dbo].[RetrieveEntityParent] (@FK_EntityId int)
 RETURNS TABLE
 AS RETURN

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

  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 

2 个答案:

答案 0 :(得分:1)

我在Employee中找不到Employee.FK_EntityId引用的RetrieveEntityParent 你的意思是OrgEntity.FK_EntityId

在编辑之后,要获得Emp_CompanyHirarchy工作,您必须将@FK_EntityId作为参数传输

ALTER   procedure [dbo].[Emp_CompanyHirarchy]
  @FK_CompanyId varchar(50),
  @FK_EntityId varchar(50)
AS
  SELECT * FROM dbo.RetrieveEntityParent(@FK_EntityId)
   -- WHERE mybe some other condition

顺便说一句:有一些varchar和整数

的参数类错配

答案 1 :(得分:1)

link中有一个您需要的示例,但在link中直接使用参数有一种更简单的方法。

DECLARE @FK_EntityId INT

SET @FK_EntityId = 1

SELECT EntityId, FK_ParentId , EntityName ,EntityArabicName 
FROM OrgEntity
WHERE OrgEntity.EntityId = @FK_EntityId 

UNION ALL

SELECT EntityId, FK_ParentId , EntityName ,EntityArabicName 
FROM OrgEntity
WHERE FK_ParentId = @FK_EntityId