找不到列“dbo”或用户定义的函数或聚合“dbo.Splitfn”,或者名称不明确

问题描述 投票:46回答:4

嗨伙计们,

我使用了以下分割功能,

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.

我检查了我的tablevalued函数函数'splitfn'在那里,但我不知道出了什么问题?有什么建议..

sql-server sql-server-2005 tsql user-defined-functions
4个回答
77
投票

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

尝试:

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

但是......也考虑将你的功能改成内联TVF,因为它会表现得更好。


11
投票

你需要像表一样处理一个值为udf的表,例如JOIN它

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

3
投票

由于人们将来自Google,请确保您使用的是正确的数据库。

在“主”数据库中运行SQL通常会返回此错误。


3
投票

一般答案

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

select [dbo].[SplitString]('1,2',',')  -- will not work and throws this error
© www.soinside.com 2019 - 2024. All rights reserved.