Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Table-valued functions - incorrect syntax?

Author  Topic 

dylanbeattie
Starting Member

2 Posts

Posted - 2006-03-30 : 08:31:45
I encountered some odd behaviour while building some recursive table-valued functions, that I've managed to isolate to the following test case:


-- A function to add two ints and return an int
create function AddNumbers_Scalar(@a int, @b int)
returns int
as begin
return (@a + @b)
end
go
-- A function to add two integers and return the
-- result in a table variable
create function AddNumbers_Table(@a int, @b int)
returns @fnord table(foo int)
as begin
insert into @fnord(foo) values(@a + @b)
return
end
go
declare @w int
declare @x int
declare @y int
declare @z int

set @w = 1
set @x = 1
set @y = 1
set @z = 1

declare @m int
declare @n int

set @m = @w + @x
set @n = @y + @z

-- These both return 4 - so far, so good!
select dbo.AddNumbers_Scalar(@m, @n)
select * from dbo.AddNumbers_Table(@m, @n)

-- This returns 4...
select dbo.AddNumbers_Scalar(@w + @y, @y + @z)
-- ...but this says "Incorrect syntax near '+'."
select * from dbo.AddNumbers_Table(@w + @x, @y + @z)

Anyone know why you apparently can't pass an expression to a table-valued function call? It just seems rather odd that this one particular context requires you to do your expression evaluation in advance and pass in the result as a single variable...

Thanks in advance,

Dylan

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-03-30 : 09:20:03
That is the way it is for any table valued function.

CODO ERGO SUM
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-03-30 : 09:20:15
Yeah, I happened upon the same realization. there is not a lot in books online that would indicate why this isn't ok but the behavior is consistant with invoking stored procedure calls. ie you can't call an SP like: exec mySP @a+@b

If you're looking for an alternative approach to recursive table-value function calls, feel free to post your objectives.

Be One with the Optimizer
TG
Go to Top of Page

dylanbeattie
Starting Member

2 Posts

Posted - 2006-03-30 : 10:32:07
Nope, everything else works fine, it's just that syntax introduces an unnecessary variable declaration + assignment into an already-complex function and I'm trying to keep things as straightforward as possible.

It was more the lack of clear documentation that was confusing me... but if that's how it is, then that's how it is, I guess

Go to Top of Page
   

- Advertisement -