I am building a function that returns table result
ALTER FUNCTION [brm].[fnComputeScores_NEW]
(
@var1 TINYINT
)
RETURNS
@ret TABLE
(
[producerid] INT
,[CityId] INT
, CityName VARCHAR(100)
)
AS
BEGIN
INSERT INTO @ret
SELECT [producerid], [CityId] from producers
--placeholder
RETURN
END
everything is fine to this point
but code that I want to put in placeholder
UPDATE @ret
SET
CityName = Cities.Name
FROM
@ret JOIN Cities
ON @ret.CityId= Cities.CityId
generates compilation error
Must declare the scalar variable “@ret”.
Why? How to fix it?
You can’t reference the table variable outside of
FROM. This is not exclusive toUPDATE… from http://msdn.microsoft.com/en-us/library/ms175010.aspx:…so you can try: