Writting function which should compatibile with Babelfish and SQL Server as well.
The following function is having update statement with from and joins clause.
CREATE FUNCTION fn_update_from_test()
RETURNS @ListOWeekDays TABLE
(
DyNumber INT,
DayAbb VARCHAR(40),
WeekName VARCHAR(40)
)
AS BEGIN
INSERT INTO @ListOWeekDays
VALUES
(1,'Mon','Monday') ,
(2,'Tue','Tuesday') ,
(3,'Wed','Wednesday') ,
(4,'Thu','Thursday'),
(5,'Fri','Friday'),
(6,'Sat','Saturday'),
(7,'Sun','Sunday')
UPDATE lwd
SET DayAbb = COALESCE( lwd1.DayAbb, lwd2.DayAbb ) + '--',
WeekName = COALESCE( lwd3.WeekName, lwd2.WeekName ) + '-^-'
FROM @ListOWeekDays lwd
LEFT JOIN @ListOWeekDays lwd1 ON lwd1.DyNumber = lwd.DyNumber
LEFT JOIN @ListOWeekDays lwd2 ON lwd2.DyNumber = lwd.DyNumber
LEFT JOIN @ListOWeekDays lwd3 ON lwd3.DyNumber = lwd.DyNumber;
RETURN;
END
GO
Works fine in SQL Server but getting an error in Babelfish:
'UPDATE' cannot be used within a function
When I tried it with following syntax it works in Babelfish but not for SQL Server.
--Works in Babelfish
UPDATE ListOWeekDays
SET DayAbb = COALESCE( lwd1.DayAbb, lwd2.DayAbb ) + '--',
WeekName = COALESCE( lwd3.WeekName, lwd2.WeekName ) + '-^-'
FROM @ListOWeekDays lwd
LEFT JOIN @ListOWeekDays lwd1 ON lwd1.DyNumber = lwd.DyNumber
LEFT JOIN @ListOWeekDays lwd2 ON lwd2.DyNumber = lwd.DyNumber
LEFT JOIN @ListOWeekDays lwd3 ON lwd3.DyNumber = lwd.DyNumber;
--Error in SQL Server:
Msg 8154, Level 16, State 1, Line 52 The table '@ListOWeekDays' is ambiguous.
Note: The logic of update statement is different in original code, I have just added the sample code for understanding of issue.
This is a current bug in Babelfish: while you cannot modify an actual table in a SQL function, you can do anything to a table variable in a SQL function. Unfortunately, the syntax
UPDATE t...FROM @tabvar
as t is currently interpreted by Babelfish as that a normal table is updated. This bug is expected to be fixed in the next release, which is v.3.2.0. The workaround is to do a directUPDATE @tabvar
, so not using the correlation name for the UPDATE.