I've used the following split function:
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
and i used this function in a query and it was executed
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
but when i excute my store procedure giving values say (1,2) i got the error
Cannot find either column "dbo" or the user-defined
function or aggregate "dbo.Splitfn", or the name is ambiguous.
I've checked my tablevalued functions the function 'splitfn' was there but I don't know what is going wrong? Any suggestions..
5条答案
按热度按时间e5nszbig1#
It's a table-valued function, but you're using it as a scalar function.
Try:
But... also consider changing your function into an inline TVF, as it'll perform better.
rdlzhqv92#
You need to treat a table valued udf like a table, eg JOIN it
qnakjoqk3#
Works
Doesn't Work
SELECT * FROM [dbo].[SplitString]('1,2',',')
: This query asks the database to give us all the pieces (like rows in a table) that come from using the[dbo].[SplitString]
tool with the input '1,2' and ',' (comma) as instructions. It works because we're using the tool the right way, expecting a bunch of results.SELECT [dbo].[SplitString]('1,2',',')
: This query also uses the[dbo].[SplitString]
tool, but here, we're asking it for just one piece, not many. However, if the tool is built to give us many pieces (like a list), it gets confused and says, "I can't give you just one, I have a whole list!" So, it doesn't work because we're using the tool the wrong way for what we want.t3irkdon4#
Since people will be coming from Google, make sure you're in the right database.
Running SQL in the 'master' database will often return this error.
b1payxdu5#