SQL Server - using table variables in a function

Asked By Daniel Schaffer on 24-Feb-05 03:15 PM
I'm writing a table-valued function in SQL Server 2000. 
I'm trying to declare and populate a table variable within the function, and then use that table in a JOIN statement, also inside the function. 
Is this possible? Here is the code I have now: 
BEGIN 
    DECLARE @dateTable TABLE 
    ( 
        [Date] datetime 
    ) 
    DECLARE @cDate datetime 
    SET @cDate = @StartDate 
    WHILE @cDate <= @EndDate 
    BEGIN 
        INSERT INTO @dateTable ([Date]) VALUES (@cDate) 
        SET @cDate = DATEADD(dd, 1, @cDate) 
    END 
    INSERT INTO @table 
        SELECT TOP 100 PERCENT Departments.ID AS [Department.ID], Departments.Name AS [Department.Name], Shifts.ID AS [Shift.ID], Shifts.Name AS [Shift.Name], 
            Shifts.StartTime AS [Shift.StartTime], Shifts.EndTime AS [Shift.EndTime], ShiftDays.DayOfWeek AS [ShiftDay.DayOfWeek], 
            Workdays.ID AS [WorkDay.ID], Workdays.Budget AS [WorkDay.Budget], Workdays.HoursNeeded AS [WorkDay.HoursNeeded], 
            Workdays.[Date] AS [WorkDay.Date] 
        FROM ShiftDays INNER JOIN 
            Shifts ON ShiftDays.ShiftID = Shifts.ID INNER JOIN 
            Departments ON Shifts.DepartmentID = Departments.ID LEFT OUTER JOIN 
            Workdays ON Departments.ID = Workdays.DepartmentID AND ShiftDays.DayOfWeek = Workdays.DayOfWeek RIGHT OUTER JOIN 
            @dateTable ON Workdays.[Date] = @dateTable.[Date] 
        WHERE (Departments.EmployerID = @EmployerID) 
RETURN 
END 
However, I get the error "ADO error: Must declare the variable '@dateTable'"  ...  even though it is clearly already declared and populated.

I'm missing something

Asked By Robbe Morris on 24-Feb-05 04:35 PM
Yes, you can do this sort of thing.  However, what would be the point of doing an outer join to the table variable @dateTable when it only has one column and that one column is not returned in your result set?
Since it is an OUTER JOIN, wouldn't you always get the same resultset whether the data was found or not?  And, if you aren't getting any data from @dateTable, why bother with an OUTER JOIN?
BTW, did you leave the creation of this table out of your post?
    INSERT INTO @table 
 I don't see where @table is created...

Me too!

Asked By Peter Bromberg on 24-Feb-05 07:37 PM
A RIGHT OUTER JOIN would  preserve the unmatched rows from the second (right) table, joining them with  NULLs in the shape of the first (left) table. So since you have nothing but a list of sequential dates in your table variable, you'd get a list of all the days, with nothing in the rows where there was no match, for your inserts.
Is that what you really want?
Regarding the actual table variable, just from eyeballing the SQL it doesn't seem to have gone out of scope, although that is a possible cause.

using table variable in a join

Ross Mehlman replied to Daniel Schaffer on 26-May-09 09:07 AM

Daniel-

I just came across your post today. I don't know if the issue is still live, but here is the solution-

Table variables must be aliased in a join, so where you say:

RIGHT OUTER JOIN @dateTable ON Workdays.[Date] = @dateTable.[Date]

this the the correct syntax:

RIGHT OUTER JOIN @dateTable dt ON Workdays.[Date] = dt.[Date]

Hope this helps!

Raul replied to Ross Mehlman on 03-Aug-10 08:29 PM
Hi Ross, I have the same problem but no in a join, my problem is in declare table var, this is the code:

CREATE FUNCTION
(   
    -- Add the parameters for the function here
    @param1 int
)myfunction
RETURNS TABLE
AS
RETURN
(
      DECLARE @mytable TABLE(id int) ;


.....

Only with this instruction the function display the error:
Msg 156, Level 15, State 1, Procedure myfunction, Line 17
Incorrect syntax near the keyword 'DECLARE'.

What's the problem?? Do you have an idea?
Thank you


Raul replied to Raul on 03-Aug-10 08:31 PM
error in copy paste :D

CREATE FUNCTION  myfunction
(   
    -- Add the parameters for the function here
    @param1 int
)
RETURNS TABLE
AS
RETURN
(
      DECLARE @mytable TABLE(id int) ;


.....