【问题标题】:Cannot find either column "dbo" or the user-defined function or aggregate "dbo.Splitfn", or the name is ambiguous找不到列“dbo”或用户定义的函数或聚合“dbo.Splitfn”,或者名称不明确
【发布时间】:2011-01-06 16:52:21
【问题描述】:

大家好,

我使用了以下拆分功能,

CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1))       
returns @temptable TABLE (items varchar(8000))       
 as       
begin       
declare @idx int       
declare @slice varchar(8000)       

select @idx = 1       
    if len(@String)<1 or @String is null  return       

while @idx!= 0       
begin       
    set @idx = charindex(@Delimiter,@String)       
    if @idx!=0       
        set @slice = left(@String,@idx - 1)       
    else       
        set @slice = @String       

    if(len(@slice)>0)  
        insert into @temptable(Items) values(@slice)       

    set @String = right(@String,len(@String) - @idx)       
    if len(@String) = 0 break       
end   
return      

end  

我在查询中使用了这个函数,它被执行了

ALTER PROCEDURE [dbo].[Employees_Delete] 
-- Add the parameters for the stored procedure here
@Id varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here

 if exists( select Emp_Id from Employee where Emp_Id=dbo.Splitfn(@Id,','))
begin
    update Employee set Is_Deleted=1 where Emp_Id=dbo.Splitfn(@Id,',')
    select 'deleted' as message
end 
END

但是当我执行我的存储过程给出值说 (1,2) 我得到了错误

Cannot find either column "dbo" or the user-defined 
function or aggregate "dbo.Splitfn", or the name is ambiguous.

我检查了我的表值函数,函数“splitfn”在那里,但我不知道出了什么问题?任何建议..

【问题讨论】:

    标签: sql-server sql-server-2005 tsql user-defined-functions


    【解决方案1】:

    这是一个表值函数,但您将其用作标量函数。

    试试:

    where Emp_Id IN (SELECT i.items FROM dbo.Splitfn(@Id,',') AS i)
    

    但是...也可以考虑将您的函数更改为内联 TVF,因为它的性能会更好。

    【讨论】:

    【解决方案2】:

    你需要把一个值为 udf 的表当作一个表来对待,例如 JOIN it

    select Emp_Id 
    from Employee E JOIN dbo.Splitfn(@Id,',') CSV ON E.Emp_Id = CSV.items 
    

    【讨论】:

      【解决方案3】:

      一般答案

      select * from [dbo].[SplitString]('1,2',',') -- Will work 
      

      但是

      select [dbo].[SplitString]('1,2',',')  -- will **not** work and throws this error
      

      【讨论】:

        【解决方案4】:

        由于人们将来自 Google,因此请确保您在正确的数据库中。

        在 'master' 数据库中运行 SQL 经常会返回此错误。

        【讨论】:

        • 对于那些因复制错误而苦苦挣扎的人。这应该可以解决它!
        【解决方案5】:
        Database -> Tables -> Functions -> Scalar Valued Functions - dbo.funcName 
        rightClick => Properties -> Search UserRoles + Add user access
        

        【讨论】:

          猜你喜欢
          • 2015-05-26
          • 2020-12-07
          • 2014-01-06
          • 2019-08-01
          • 2019-02-09
          • 1970-01-01
          • 1970-01-01
          • 2016-01-10
          • 2011-07-13
          相关资源
          最近更新 更多