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 intcreate function AddNumbers_Scalar(@a int, @b int) returns int as begin return (@a + @b)endgo-- A function to add two integers and return the -- result in a table variablecreate function AddNumbers_Table(@a int, @b int) returns @fnord table(foo int) as begin insert into @fnord(foo) values(@a + @b) returnendgodeclare @w intdeclare @x intdeclare @y intdeclare @z intset @w = 1set @x = 1set @y = 1set @z = 1declare @m intdeclare @n intset @m = @w + @xset @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